Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/typeschema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,49 @@ export const mkTypeSchemaIndex = (
};
};

/** Walk fields recursively and collect every nested type they reference. Profile-local
* nested types (already in `into`) take precedence over inherited ones — their URLs
* are seeded first, so the inherited lookup is skipped for them. Identities (URLs) are
* preserved; this is a closure-by-reference, not a re-rooting. */
const collectReferencedNested = (
fields: Record<string, Field> | undefined,
into: Map<string, NestedTypeSchema>,
): void => {
if (!fields) return;
for (const field of Object.values(fields)) {
if (isChoiceDeclarationField(field)) continue;
const t = field.type;
if (!t || !isNestedIdentifier(t) || into.has(t.url)) continue;
const nested = resolveType(t);
if (!isNestedTypeSchema(nested)) continue;
into.set(t.url, nested);
collectReferencedNested(nested.fields, into);
}
};

const buildProfileSnapshot = (schema: ProfileTypeSchema): SnapshotProfileTypeSchema => {
const flat = flatProfile(schema);
const collected = new Map<string, NestedTypeSchema>();
// Profile-local nested types take precedence by URL — they may carry constraints
// not present on the inherited copy.
for (const n of flat.nested ?? []) collected.set(n.identifier.url, n);
for (const n of flat.nested ?? []) collectReferencedNested(n.fields, collected);
collectReferencedNested(flat.fields, collected);
// Profile fields only carry constraints; consumers also access inherited base fields,
// which may reference nested types invisible from the profile's own constraints.
const baseSchema = resolveType(flat.base);
if (baseSchema && "fields" in baseSchema) {
collectReferencedNested(baseSchema.fields, collected);
}
const nested = collected.size > 0 ? Array.from(collected.values()) : undefined;
return {
identifier: snapshotIdentifier(flat.identifier),
base: flat.base,
description: flat.description,
fields: flat.fields ?? {},
extensions: flat.extensions,
dependencies: flat.dependencies,
nested: flat.nested,
nested,
};
};

Expand Down
109 changes: 109 additions & 0 deletions test/unit/typeschema/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { describe, expect, it } from "bun:test";
import type {
CanonicalUrl,
Name,
NestedTypeSchema,
ProfileTypeSchema,
RegularField,
SpecializationTypeSchema,
TypeIdentifier,
} from "@typeschema/types";
import { snapshotIdentifier } from "@typeschema/types";
import { mkTypeSchemaIndex } from "@typeschema/utils";

const stringType: TypeIdentifier = {
Expand Down Expand Up @@ -604,4 +606,111 @@ describe("TypeSchema Index", () => {
expect(result.identifier).toEqual(constraintSchema.identifier);
});
});

describe("snapshot nested collection", () => {
it("inherits nested types referenced by inherited fields from the base", () => {
const nestedId = {
name: "Patient_contact" as Name,
package: "test",
kind: "nested" as const,
version: "1.0.0",
url: "http://example.org/StructureDefinition/Patient#contact" as CanonicalUrl,
};

const nestedSchema: NestedTypeSchema = {
identifier: nestedId,
base: stringType,
fields: {
name: { type: stringType, required: false, array: false } as RegularField,
},
};

const baseSchema: SpecializationTypeSchema = {
identifier: {
name: "Patient" as Name,
package: "test",
kind: "resource",
version: "1.0.0",
url: "http://example.org/StructureDefinition/Patient" as CanonicalUrl,
},
fields: {
contact: { type: nestedId, required: false, array: true } as RegularField,
},
nested: [nestedSchema],
};

const profileSchema: ProfileTypeSchema = {
identifier: {
name: "MyPatient" as Name,
package: "test",
kind: "profile",
version: "1.0.0",
url: "http://example.org/StructureDefinition/MyPatient" as CanonicalUrl,
},
base: baseSchema.identifier,
fields: {},
};

const index = mkTypeSchemaIndex([baseSchema, profileSchema], {});
const snapshot = index.resolve(snapshotIdentifier(profileSchema.identifier));

expect(snapshot).toBeDefined();
expect(snapshot?.nested?.map((n) => n.identifier.url)).toEqual([nestedId.url]);
});

it("prefers profile-local nested over inherited when URLs match", () => {
const nestedUrl = "http://example.org/StructureDefinition/Patient#contact" as CanonicalUrl;
const baseNested: NestedTypeSchema = {
identifier: {
name: "Patient_contact" as Name,
package: "test",
kind: "nested",
version: "1.0.0",
url: nestedUrl,
},
base: stringType,
fields: { name: { type: stringType } as RegularField },
};
const profileNested: NestedTypeSchema = {
identifier: baseNested.identifier,
base: stringType,
// Profile-local version constrains the field as required.
fields: { name: { type: stringType, required: true } as RegularField },
};

const baseSchema: SpecializationTypeSchema = {
identifier: {
name: "Patient" as Name,
package: "test",
kind: "resource",
version: "1.0.0",
url: "http://example.org/StructureDefinition/Patient" as CanonicalUrl,
},
fields: {
contact: { type: baseNested.identifier, required: false, array: true } as RegularField,
},
nested: [baseNested],
};

const profileSchema: ProfileTypeSchema = {
identifier: {
name: "MyPatient" as Name,
package: "test",
kind: "profile",
version: "1.0.0",
url: "http://example.org/StructureDefinition/MyPatient" as CanonicalUrl,
},
base: baseSchema.identifier,
fields: {},
nested: [profileNested],
};

const index = mkTypeSchemaIndex([baseSchema, profileSchema], {});
const snapshot = index.resolve(snapshotIdentifier(profileSchema.identifier));

expect(snapshot?.nested).toHaveLength(1);
const collected = snapshot?.nested?.[0];
expect((collected?.fields?.name as RegularField).required).toBe(true);
});
});
});
Loading