11using System . Collections . Concurrent ;
22using System . Reflection ;
33using System . Runtime . CompilerServices ;
4+ using System . Text . Json ;
5+ using System . Text . Json . Nodes ;
46using JsonApiDotNetCore . Configuration ;
57using JsonApiDotNetCore . OpenApi . Swashbuckle . JsonApiMetadata ;
68using JsonApiDotNetCore . OpenApi . Swashbuckle . JsonApiObjects . ResourceObjects ;
79using JsonApiDotNetCore . OpenApi . Swashbuckle . SwaggerComponents ;
810using Microsoft . OpenApi . Any ;
911using Microsoft . OpenApi . Models ;
12+ using Microsoft . OpenApi . Models . Interfaces ;
13+ using Microsoft . OpenApi . Models . References ;
1014using Swashbuckle . AspNetCore . SwaggerGen ;
1115
1216namespace JsonApiDotNetCore . OpenApi . Swashbuckle . SchemaGenerators . Components ;
@@ -79,7 +83,7 @@ public DataSchemaGenerator(SchemaGenerationTracer schemaGenerationTracer, Schema
7983 _resourceDocumentationReader = resourceDocumentationReader ;
8084 }
8185
82- public OpenApiSchema GenerateSchema ( Type dataSchemaType , bool forRequestSchema , SchemaRepository schemaRepository )
86+ public OpenApiSchemaReference GenerateSchema ( Type dataSchemaType , bool forRequestSchema , SchemaRepository schemaRepository )
8387 {
8488 // For a given resource (identifier) type, we always generate the full type hierarchy. Discriminator mappings
8589 // are managed manually, because there's no way to intercept in the Swashbuckle recursive component schema generation.
@@ -114,11 +118,11 @@ public OpenApiSchema GenerateSchema(Type dataSchemaType, bool forRequestSchema,
114118
115119 using var traceScope = _schemaGenerationTracer . TraceStart ( this , dataSchemaType ) ;
116120
117- referenceSchemaForData = _defaultSchemaGenerator . GenerateSchema ( dataSchemaType , schemaRepository ) ;
118- var fullSchemaForData = schemaRepository . Schemas [ referenceSchemaForData . Reference . Id ] ;
121+ referenceSchemaForData = ( OpenApiSchemaReference ) _defaultSchemaGenerator . GenerateSchema ( dataSchemaType , schemaRepository ) ;
122+ var fullSchemaForData = ( OpenApiSchema ) schemaRepository . Schemas [ referenceSchemaForData . Reference . Id ] ;
119123 fullSchemaForData . AdditionalPropertiesAllowed = false ;
120124
121- var inlineSchemaForData = fullSchemaForData . UnwrapLastExtendedSchema ( ) ;
125+ var inlineSchemaForData = ( OpenApiSchema ) fullSchemaForData . UnwrapLastExtendedSchema ( ) ;
122126
123127 SetAbstract ( inlineSchemaForData , resourceSchemaType ) ;
124128 SetResourceType ( inlineSchemaForData , resourceType , schemaRepository ) ;
@@ -142,7 +146,7 @@ public OpenApiSchema GenerateSchema(Type dataSchemaType, bool forRequestSchema,
142146
143147 if ( RequiresRootObjectTypeInDataSchema ( resourceSchemaType , forRequestSchema ) )
144148 {
145- fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiBoolean ( true ) ;
149+ fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiAny ( true ) ;
146150 }
147151
148152 traceScope . TraceSucceeded ( referenceSchemaForData . Reference . Id ) ;
@@ -202,7 +206,7 @@ public OpenApiSchema GenerateSchema(Type dataSchemaType, bool forRequestSchema,
202206 return boxedSchemaType . Value ;
203207 }
204208
205- public OpenApiSchema GenerateSchemaForCommonData ( Type commonDataSchemaType , SchemaRepository schemaRepository )
209+ public OpenApiSchemaReference GenerateSchemaForCommonData ( Type commonDataSchemaType , SchemaRepository schemaRepository )
206210 {
207211 ArgumentNullException . ThrowIfNull ( commonDataSchemaType ) ;
208212 ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
@@ -219,9 +223,9 @@ public OpenApiSchema GenerateSchemaForCommonData(Type commonDataSchemaType, Sche
219223
220224 var fullSchema = new OpenApiSchema
221225 {
222- Type = "object" ,
226+ Type = JsonSchemaType . Object ,
223227 Required = new SortedSet < string > ( [ JsonApiPropertyName . Type ] ) ,
224- Properties = new Dictionary < string , OpenApiSchema >
228+ Properties = new Dictionary < string , IOpenApiSchema >
225229 {
226230 [ JsonApiPropertyName . Type ] = referenceSchemaForResourceType . WrapInExtendedSchema ( ) ,
227231 [ referenceSchemaForMeta . Reference . Id ] = referenceSchemaForMeta . WrapInExtendedSchema ( )
@@ -234,7 +238,7 @@ public OpenApiSchema GenerateSchemaForCommonData(Type commonDataSchemaType, Sche
234238 } ,
235239 Extensions =
236240 {
237- [ "x-abstract" ] = new OpenApiBoolean ( true )
241+ [ "x-abstract" ] = new OpenApiAny ( true )
238242 }
239243 } ;
240244
@@ -272,7 +276,7 @@ private static void SetAbstract(OpenApiSchema fullSchema, ResourceSchemaType res
272276 {
273277 if ( resourceSchemaType . ResourceType . ClrType . IsAbstract && resourceSchemaType . SchemaOpenType != typeof ( IdentifierInRequest < > ) )
274278 {
275- fullSchema . Extensions [ "x-abstract" ] = new OpenApiBoolean ( true ) ;
279+ fullSchema . Extensions [ "x-abstract" ] = new OpenApiAny ( true ) ;
276280 }
277281 }
278282
@@ -367,8 +371,8 @@ private void SetFieldSchemaMembers(OpenApiSchema fullSchemaForData, ResourceSche
367371 {
368372 var propertyNameInSchema = forAttributes ? JsonApiPropertyName . Attributes : JsonApiPropertyName . Relationships ;
369373
370- var referenceSchemaForFields = fullSchemaForData . Properties [ propertyNameInSchema ] . UnwrapLastExtendedSchema ( ) ;
371- var fullSchemaForFields = schemaRepository . Schemas [ referenceSchemaForFields . Reference . Id ] ;
374+ var referenceSchemaForFields = ( OpenApiSchemaReference ) fullSchemaForData . Properties [ propertyNameInSchema ] . UnwrapLastExtendedSchema ( ) ;
375+ var fullSchemaForFields = ( OpenApiSchema ) schemaRepository . Schemas [ referenceSchemaForFields . Reference . Id ] ;
372376 fullSchemaForFields . AdditionalPropertiesAllowed = false ;
373377
374378 SetAbstract ( fullSchemaForFields , resourceSchemaTypeForData ) ;
@@ -437,7 +441,7 @@ private ResourceSchemaType GetResourceSchemaTypeForFieldsProperty(ResourceSchema
437441 return ResourceSchemaType . Create ( fieldsConstructedType , _resourceGraph ) ;
438442 }
439443
440- private OpenApiSchema GenerateSchemaForCommonFields ( Type commonFieldsSchemaType , SchemaRepository schemaRepository )
444+ private OpenApiSchemaReference GenerateSchemaForCommonFields ( Type commonFieldsSchemaType , SchemaRepository schemaRepository )
441445 {
442446 if ( schemaRepository . TryLookupByType ( commonFieldsSchemaType , out var referenceSchema ) )
443447 {
@@ -450,9 +454,9 @@ private OpenApiSchema GenerateSchemaForCommonFields(Type commonFieldsSchemaType,
450454
451455 var fullSchema = new OpenApiSchema
452456 {
453- Type = "object" ,
457+ Type = JsonSchemaType . Object ,
454458 Required = new SortedSet < string > ( [ OpenApiMediaTypeExtension . FullyQualifiedOpenApiDiscriminatorPropertyName ] ) ,
455- Properties = new Dictionary < string , OpenApiSchema >
459+ Properties = new Dictionary < string , IOpenApiSchema >
456460 {
457461 [ OpenApiMediaTypeExtension . FullyQualifiedOpenApiDiscriminatorPropertyName ] = referenceSchemaForResourceType . WrapInExtendedSchema ( )
458462 } ,
@@ -464,7 +468,7 @@ private OpenApiSchema GenerateSchemaForCommonFields(Type commonFieldsSchemaType,
464468 } ,
465469 Extensions =
466470 {
467- [ "x-abstract" ] = new OpenApiBoolean ( true )
471+ [ "x-abstract" ] = new OpenApiAny ( true )
468472 }
469473 } ;
470474
@@ -489,7 +493,7 @@ private void MapInDiscriminator(ResourceSchemaType resourceSchemaType, bool forR
489493 : resourceSchemaType . ChangeResourceType ( baseResourceType ) . SchemaConstructedType ;
490494
491495 var referenceSchemaForBase = schemaRepository . LookupByType ( baseSchemaType ) ;
492- var inlineSchemaForBase = schemaRepository . Schemas [ referenceSchemaForBase . Reference . Id ] . UnwrapLastExtendedSchema ( ) ;
496+ var inlineSchemaForBase = ( OpenApiSchema ) schemaRepository . Schemas [ referenceSchemaForBase . Reference . Id ] . UnwrapLastExtendedSchema ( ) ;
493497
494498 inlineSchemaForBase . Discriminator ??= new OpenApiDiscriminator
495499 {
@@ -545,9 +549,9 @@ private void MapResourceTypeInEnum(string publicName, SchemaRepository schemaRep
545549 var schemaId = _schemaIdSelector . GetResourceTypeSchemaId ( null ) ;
546550 var fullSchema = schemaRepository . Schemas [ schemaId ] ;
547551
548- if ( ! fullSchema . Enum . Any ( openApiAny => openApiAny is OpenApiString openApiString && openApiString . Value == publicName ) )
552+ if ( ! fullSchema . Enum . Any ( openApiAny => openApiAny is JsonValue openApiString && openApiString . GetValueKind ( ) == JsonValueKind . String && openApiString . GetValue < string > ( ) == publicName ) )
549553 {
550- fullSchema . Enum . Add ( new OpenApiString ( publicName ) ) ;
554+ fullSchema . Enum . Add ( publicName ) ;
551555 }
552556 }
553557
@@ -572,11 +576,11 @@ private void GenerateDataSchemasForDirectlyDerivedTypes(ResourceSchemaType resou
572576
573577 using var traceScope = _schemaGenerationTracer . TraceStart ( this , resourceSchemaTypeForDerived . SchemaConstructedType ) ;
574578
575- var referenceSchemaForDerived = _defaultSchemaGenerator . GenerateSchema ( derivedSchemaType , schemaRepository ) ;
576- var fullSchemaForDerived = schemaRepository . Schemas [ referenceSchemaForDerived . Reference . Id ] ;
579+ var referenceSchemaForDerived = ( OpenApiSchemaReference ) _defaultSchemaGenerator . GenerateSchema ( derivedSchemaType , schemaRepository ) ;
580+ var fullSchemaForDerived = ( OpenApiSchema ) schemaRepository . Schemas [ referenceSchemaForDerived . Reference . Id ] ;
577581 fullSchemaForDerived . AdditionalPropertiesAllowed = false ;
578582
579- var inlineSchemaForDerived = fullSchemaForDerived . UnwrapLastExtendedSchema ( ) ;
583+ var inlineSchemaForDerived = ( OpenApiSchema ) fullSchemaForDerived . UnwrapLastExtendedSchema ( ) ;
580584 SetResourceFields ( inlineSchemaForDerived , resourceSchemaTypeForDerived , forRequestSchema , schemaRepository ) ;
581585
582586 SetAbstract ( inlineSchemaForDerived , resourceSchemaTypeForDerived ) ;
@@ -605,7 +609,7 @@ private void GenerateDataSchemasForDirectlyDerivedTypes(ResourceSchemaType resou
605609 if ( RequiresRootObjectTypeInDataSchema ( resourceSchemaTypeForDerived , forRequestSchema ) )
606610 {
607611 var fullSchemaForData = schemaRepository . Schemas [ referenceSchemaForDerived . Reference . Id ] ;
608- fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiBoolean ( true ) ;
612+ fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiAny ( true ) ;
609613 }
610614
611615 GenerateDataSchemasForDirectlyDerivedTypes ( resourceSchemaTypeForDerived , forRequestSchema , schemaRepository ) ;
0 commit comments