@@ -116,7 +116,7 @@ internal sealed class OpenApiSchemaService(
116116 {
117117 schema . ApplyDefaultValue ( defaultValueAttribute . Value , context . TypeInfo ) ;
118118 }
119- var isInlinedSchema = schema [ OpenApiConstants . SchemaId ] is null ;
119+ var isInlinedSchema = ! schema . WillBeComponentized ( ) ;
120120 if ( isInlinedSchema )
121121 {
122122 if ( propertyAttributes . OfType < DescriptionAttribute > ( ) . LastOrDefault ( ) is { } descriptionAttribute )
@@ -265,29 +265,26 @@ internal static IOpenApiSchema ResolveReferenceForSchema(OpenApiDocument documen
265265 {
266266 var schema = UnwrapOpenApiSchema ( inputSchema ) ;
267267
268- if ( inputSchema is OpenApiSchema && schema . Metadata is not null &&
269- ! schema . Metadata . ContainsKey ( OpenApiConstants . RefId ) &&
270- schema . Metadata . TryGetValue ( OpenApiConstants . SchemaId , out var referenceId ) &&
271- referenceId is string referenceIdString )
268+ var isComponentizedSchema = schema . IsComponentizedSchema ( out var schemaId ) ;
269+
270+ // When we register it, this will be the resulting reference
271+ IOpenApiSchema ? resultSchemaReference = null ;
272+ if ( inputSchema is OpenApiSchema && isComponentizedSchema )
272273 {
273274 var targetReferenceId = baseSchemaId is not null
274- ? $ "{ baseSchemaId } { referenceIdString } "
275- : referenceIdString ;
275+ ? $ "{ baseSchemaId } { schemaId } "
276+ : schemaId ;
276277 if ( ! string . IsNullOrEmpty ( targetReferenceId ) )
277278 {
278- document . AddOpenApiSchemaByReference ( targetReferenceId , schema ) ;
279+ resultSchemaReference = document . AddOpenApiSchemaByReference ( targetReferenceId , schema ) ;
279280 }
280281 }
281282
282- if ( schema . Metadata is not null &&
283- schema . Metadata . TryGetValue ( OpenApiConstants . SchemaId , out var resolvedBaseSchemaId ) )
283+ if ( schema . AnyOf is { Count : > 0 } )
284284 {
285- if ( schema . AnyOf is { Count : > 0 } )
285+ for ( var i = 0 ; i < schema . AnyOf . Count ; i ++ )
286286 {
287- for ( var i = 0 ; i < schema . AnyOf . Count ; i ++ )
288- {
289- schema . AnyOf [ i ] = ResolveReferenceForSchema ( document , schema . AnyOf [ i ] , rootSchemaId , resolvedBaseSchemaId ? . ToString ( ) ) ;
290- }
287+ schema . AnyOf [ i ] = ResolveReferenceForSchema ( document , schema . AnyOf [ i ] , rootSchemaId , schemaId ) ;
291288 }
292289 }
293290
@@ -340,39 +337,9 @@ internal static IOpenApiSchema ResolveReferenceForSchema(OpenApiDocument documen
340337 schema . Not = ResolveReferenceForSchema ( document , schema . Not , rootSchemaId ) ;
341338 }
342339
343- // Handle schemas where the references have been inlined by the JsonSchemaExporter. In this case,
344- // the `#` ID is generated by the exporter since it has no base document to baseline against. In this
345- // case we we want to replace the reference ID with the schema ID that was generated by the
346- // `CreateSchemaReferenceId` method in the OpenApiSchemaService.
347- if ( schema . Metadata is not null &&
348- schema . Metadata . TryGetValue ( OpenApiConstants . RefId , out var refId ) &&
349- refId is string refIdString )
350- {
351- if ( schema . Metadata . TryGetValue ( OpenApiConstants . SchemaId , out var schemaId ) &&
352- schemaId is string schemaIdString )
353- {
354- return new OpenApiSchemaReference ( schemaIdString , document ) ;
355- }
356- var relativeSchemaId = $ "#/components/schemas/{ rootSchemaId } { refIdString . Replace ( "#" , string . Empty ) } ";
357- return new OpenApiSchemaReference ( relativeSchemaId , document ) ;
358- }
359-
360- // If we're resolving schemas for a top-level schema being referenced in the `components.schema` property
361- // we don't want to replace the top-level inline schema with a reference to itself. We want to replace
362- // inline schemas to reference schemas for all schemas referenced in the top-level schema though (such as
363- // `allOf`, `oneOf`, `anyOf`, `items`, `properties`, etc.) which is why `isTopLevel` is only set once.
364- if ( inputSchema is OpenApiSchema && schema . Metadata is not null &&
365- ! schema . Metadata . ContainsKey ( OpenApiConstants . RefId ) &&
366- schema . Metadata . TryGetValue ( OpenApiConstants . SchemaId , out var referenceId2 ) &&
367- referenceId2 is string referenceIdString2 )
340+ if ( resultSchemaReference is not null )
368341 {
369- var targetReferenceId = baseSchemaId is not null
370- ? $ "{ baseSchemaId } { referenceIdString2 } "
371- : referenceIdString2 ;
372- if ( ! string . IsNullOrEmpty ( targetReferenceId ) )
373- {
374- return document . AddOpenApiSchemaByReference ( targetReferenceId , schema ) ;
375- }
342+ return resultSchemaReference ;
376343 }
377344
378345 return schema ;
0 commit comments