@@ -221,38 +221,38 @@ function extractExtensions(fhirSchema: RichFHIRSchema, logger?: CodegenLogger):
221221
222222 const extensions : ProfileExtension [ ] = [ ] ;
223223
224- // Extensions are elements with path containing "extension:" or "modifierExtension:"
224+ // Look for extension elements with slicing
225225 for ( const [ path , element ] of Object . entries ( fhirSchema . elements ) ) {
226- if ( path . includes ( "extension:" ) || path . includes ( "modifierExtension:" ) ) {
227- // Extract extension slice name
228- const sliceName = path . split ( ":" ) [ 1 ] ;
229- if ( ! sliceName ) continue ;
230-
231- // Get extension URL from type profile
232- let extensionUrl : string | string [ ] | undefined ;
233- if ( element . type ) {
234- const types = Array . isArray ( element . type ) ? element . type : [ element . type ] ;
235- for ( const t of types ) {
236- const typeObj = typeof t === "string" ? { code : t } : t ;
237- if ( typeObj . code === "Extension" && typeObj . profile ) {
238- extensionUrl = typeObj . profile ;
239- break ;
240- }
226+ // Check if this is an extension or modifierExtension element with slices
227+ if (
228+ ( path === "extension" ||
229+ path === "modifierExtension" ||
230+ path . endsWith ( ".extension" ) ||
231+ path . endsWith ( ".modifierExtension" ) ) &&
232+ element . slicing ?. slices
233+ ) {
234+ // Extract each slice as an extension
235+ for ( const [ sliceName , slice ] of Object . entries ( element . slicing . slices ) ) {
236+ // Slice contains {match, schema} - we need the schema part
237+ const sliceSchema = ( slice as any ) . schema ;
238+ if ( ! sliceSchema ) continue ;
239+
240+ // Get extension URL from slice schema
241+ const extensionUrl = sliceSchema . url ;
242+
243+ if ( ! extensionUrl ) {
244+ logger ?. warn ( `Cannot determine URL for extension slice '${ sliceName } ' in ${ fhirSchema . url } ` ) ;
245+ continue ;
241246 }
242- }
243247
244- if ( ! extensionUrl ) {
245- logger ?. warn ( `Cannot determine URL for extension ${ sliceName } in ${ fhirSchema . url } ` ) ;
246- continue ;
248+ extensions . push ( {
249+ path : sliceName ,
250+ profile : extensionUrl ,
251+ min : sliceSchema . min ,
252+ max : sliceSchema . max !== undefined ? String ( sliceSchema . max ) : undefined ,
253+ mustSupport : sliceSchema . mustSupport ,
254+ } ) ;
247255 }
248-
249- extensions . push ( {
250- path : sliceName ,
251- profile : extensionUrl ,
252- min : element . min ,
253- max : element . max !== undefined ? String ( element . max ) : undefined ,
254- mustSupport : element . mustSupport ,
255- } ) ;
256256 }
257257 }
258258
@@ -298,9 +298,31 @@ function transformProfile(register: Register, fhirSchema: RichFHIRSchema, logger
298298
299299 // Debug extension extraction for us-core-patient
300300 if ( logger && fhirSchema . url ?. includes ( "us-core-patient" ) ) {
301- const extPaths = Object . keys ( fhirSchema . elements || { } ) . filter ( p => p . toLowerCase ( ) . includes ( "extension" ) ) ;
301+ const extPaths = Object . keys ( fhirSchema . elements || { } ) . filter ( ( p ) => p . toLowerCase ( ) . includes ( "extension" ) ) ;
302302 logger . debug ( ` [DEBUG] Extension-related paths in elements: ${ extPaths . slice ( 0 , 10 ) . join ( ", " ) } ` ) ;
303303 logger . debug ( ` [DEBUG] Total extension paths: ${ extPaths . length } ` ) ;
304+
305+ // Check the extension element structure
306+ const extElement = fhirSchema . elements ?. extension ;
307+ if ( extElement ) {
308+ logger . debug ( ` [DEBUG] extension element has slicing: ${ extElement . slicing ? "YES" : "NO" } ` ) ;
309+ logger . debug ( ` [DEBUG] extension element type: ${ JSON . stringify ( extElement . type ) . substring ( 0 , 100 ) } ` ) ;
310+ if ( extElement . slicing && extElement . slicing . slices ) {
311+ logger . debug ( ` [DEBUG] slicing discriminator: ${ JSON . stringify ( extElement . slicing . discriminator ) } ` ) ;
312+ logger . debug (
313+ ` [DEBUG] slicing slices: ${ JSON . stringify ( Object . keys ( extElement . slicing . slices || { } ) ) } ` ,
314+ ) ;
315+
316+ // Log first slice structure
317+ const firstSliceKey = Object . keys ( extElement . slicing . slices ) [ 0 ] ;
318+ if ( firstSliceKey ) {
319+ const firstSlice = extElement . slicing . slices [ firstSliceKey ] ;
320+ logger . debug (
321+ ` [DEBUG] First slice (${ firstSliceKey } ): ${ JSON . stringify ( firstSlice ) . substring ( 0 , 200 ) } ` ,
322+ ) ;
323+ }
324+ }
325+ }
304326 }
305327
306328 // Build nested types
0 commit comments