@@ -12,9 +12,10 @@ import {
1212 concatIdentifiers ,
1313 extractExtensionDeps ,
1414 type Field ,
15+ type Identifier ,
1516 isNestedIdentifier ,
16- isProfileIdentifier ,
1717 type NestedTypeSchema ,
18+ type ProfileIdentifier ,
1819 packageMetaToFhir ,
1920 type RichFHIRSchema ,
2021 type RichValueSet ,
@@ -92,26 +93,45 @@ export async function transformValueSet(
9293 } ;
9394}
9495
95- export function extractDependencies (
96- identifier : TypeIdentifier ,
96+ function collectRawDeps (
9797 base : TypeIdentifier | undefined ,
9898 fields : Record < string , Field > | undefined ,
9999 nestedTypes : NestedTypeSchema [ ] | undefined ,
100- ) : TypeIdentifier [ ] | undefined {
101- const deps = [ ] ;
100+ ) : TypeIdentifier [ ] {
101+ const deps : TypeIdentifier [ ] = [ ] ;
102102 if ( base ) deps . push ( base ) ;
103103 if ( fields ) deps . push ( ...extractFieldDependencies ( fields ) ) ;
104104 if ( nestedTypes ) deps . push ( ...extractNestedDependencies ( nestedTypes ) ) ;
105+ return deps ;
106+ }
105107
106- const localNestedTypeUrls = new Set ( nestedTypes ?. map ( ( nt ) => nt . identifier . url ) ) ;
108+ export function extractDependencies (
109+ identifier : TypeIdentifier ,
110+ base : TypeIdentifier | undefined ,
111+ fields : Record < string , Field > | undefined ,
112+ nestedTypes : NestedTypeSchema [ ] | undefined ,
113+ ) : Identifier [ ] | undefined {
114+ const deps = collectRawDeps ( base , fields , nestedTypes ) ;
107115
108- const filtered = deps . filter ( ( dep ) => {
116+ const filtered = deps . filter ( ( dep ) : dep is Identifier => {
109117 if ( dep . url === identifier . url ) return false ;
110- if ( isProfileIdentifier ( identifier ) ) return true ;
111- if ( ! isNestedIdentifier ( dep ) ) return true ;
112- return ! localNestedTypeUrls . has ( dep . url ) ;
118+ if ( isNestedIdentifier ( dep ) ) return false ;
119+ return true ;
113120 } ) ;
114121
122+ return concatIdentifiers ( filtered ) as Identifier [ ] | undefined ;
123+ }
124+
125+ export function extractProfileDependencies (
126+ identifier : TypeIdentifier ,
127+ base : TypeIdentifier | undefined ,
128+ fields : Record < string , Field > | undefined ,
129+ nestedTypes : NestedTypeSchema [ ] | undefined ,
130+ ) : TypeIdentifier [ ] | undefined {
131+ const deps = collectRawDeps ( base , fields , nestedTypes ) ;
132+
133+ const filtered = deps . filter ( ( dep ) => dep . url !== identifier . url ) ;
134+
115135 return concatIdentifiers ( filtered ) ;
116136}
117137
@@ -134,21 +154,33 @@ export function transformFhirSchema(register: Register, fhirSchema: RichFHIRSche
134154 const fields = mkFields ( register , fhirSchema , [ ] , fhirSchema . elements , logger ) ;
135155 const nested = mkNestedTypes ( register , fhirSchema , logger ) ;
136156
137- const extensions =
138- fhirSchema . derivation === "constraint" ? extractProfileExtensions ( register , fhirSchema , logger ) : undefined ;
139- const extensionDeps = extensions ?. flatMap ( extractExtensionDeps ) ;
140- const dependencies = concatIdentifiers ( extractDependencies ( identifier , base , fields , nested ) , extensionDeps ) ;
141-
142- const typeSchema : TypeSchema = {
143- identifier,
144- base,
145- fields,
146- nested,
147- description : fhirSchema . description ,
148- dependencies,
149- extensions,
150- typeFamily : undefined , // NOTE: should be populateTypeFamily later.
151- } ;
157+ let typeSchema : TypeSchema ;
158+ if ( fhirSchema . derivation === "constraint" ) {
159+ if ( ! base ) throw new Error ( `Profile ${ fhirSchema . url } must have a base type` ) ;
160+ const extensions = extractProfileExtensions ( register , fhirSchema , logger ) ;
161+ const extensionDeps = extensions ?. flatMap ( extractExtensionDeps ) ;
162+ const rawDeps = extractProfileDependencies ( identifier , base , fields , nested ) ;
163+ typeSchema = {
164+ identifier : identifier as ProfileIdentifier ,
165+ base,
166+ fields,
167+ nested,
168+ description : fhirSchema . description ,
169+ dependencies : concatIdentifiers ( rawDeps , extensionDeps ) ,
170+ extensions,
171+ } ;
172+ } else {
173+ const rawDeps = extractDependencies ( identifier , base , fields , nested ) ;
174+ typeSchema = {
175+ identifier,
176+ base,
177+ fields,
178+ nested,
179+ description : fhirSchema . description ,
180+ dependencies : rawDeps ,
181+ typeFamily : undefined , // NOTE: should be populateTypeFamily later.
182+ } as TypeSchema ;
183+ }
152184
153185 const bindingSchemas = collectBindingSchemas ( register , fhirSchema , logger ) ;
154186 return [ typeSchema , ...bindingSchemas ] ;
0 commit comments