1
1
using System . Collections . Concurrent ;
2
2
using System . Reflection ;
3
3
using System . Runtime . CompilerServices ;
4
+ using System . Text . Json ;
5
+ using System . Text . Json . Nodes ;
4
6
using JsonApiDotNetCore . Configuration ;
5
7
using JsonApiDotNetCore . OpenApi . Swashbuckle . JsonApiMetadata ;
6
8
using JsonApiDotNetCore . OpenApi . Swashbuckle . JsonApiObjects . ResourceObjects ;
7
9
using JsonApiDotNetCore . OpenApi . Swashbuckle . SwaggerComponents ;
8
10
using Microsoft . OpenApi . Any ;
9
11
using Microsoft . OpenApi . Models ;
12
+ using Microsoft . OpenApi . Models . Interfaces ;
13
+ using Microsoft . OpenApi . Models . References ;
10
14
using Swashbuckle . AspNetCore . SwaggerGen ;
11
15
12
16
namespace JsonApiDotNetCore . OpenApi . Swashbuckle . SchemaGenerators . Components ;
@@ -79,7 +83,7 @@ public DataSchemaGenerator(SchemaGenerationTracer schemaGenerationTracer, Schema
79
83
_resourceDocumentationReader = resourceDocumentationReader ;
80
84
}
81
85
82
- public OpenApiSchema GenerateSchema ( Type dataSchemaType , bool forRequestSchema , SchemaRepository schemaRepository )
86
+ public OpenApiSchemaReference GenerateSchema ( Type dataSchemaType , bool forRequestSchema , SchemaRepository schemaRepository )
83
87
{
84
88
// For a given resource (identifier) type, we always generate the full type hierarchy. Discriminator mappings
85
89
// 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,
114
118
115
119
using var traceScope = _schemaGenerationTracer . TraceStart ( this , dataSchemaType ) ;
116
120
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 ] ;
119
123
fullSchemaForData . AdditionalPropertiesAllowed = false ;
120
124
121
- var inlineSchemaForData = fullSchemaForData . UnwrapLastExtendedSchema ( ) ;
125
+ var inlineSchemaForData = ( OpenApiSchema ) fullSchemaForData . UnwrapLastExtendedSchema ( ) ;
122
126
123
127
SetAbstract ( inlineSchemaForData , resourceSchemaType ) ;
124
128
SetResourceType ( inlineSchemaForData , resourceType , schemaRepository ) ;
@@ -142,7 +146,7 @@ public OpenApiSchema GenerateSchema(Type dataSchemaType, bool forRequestSchema,
142
146
143
147
if ( RequiresRootObjectTypeInDataSchema ( resourceSchemaType , forRequestSchema ) )
144
148
{
145
- fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiBoolean ( true ) ;
149
+ fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiAny ( true ) ;
146
150
}
147
151
148
152
traceScope . TraceSucceeded ( referenceSchemaForData . Reference . Id ) ;
@@ -202,7 +206,7 @@ public OpenApiSchema GenerateSchema(Type dataSchemaType, bool forRequestSchema,
202
206
return boxedSchemaType . Value ;
203
207
}
204
208
205
- public OpenApiSchema GenerateSchemaForCommonData ( Type commonDataSchemaType , SchemaRepository schemaRepository )
209
+ public OpenApiSchemaReference GenerateSchemaForCommonData ( Type commonDataSchemaType , SchemaRepository schemaRepository )
206
210
{
207
211
ArgumentNullException . ThrowIfNull ( commonDataSchemaType ) ;
208
212
ArgumentNullException . ThrowIfNull ( schemaRepository ) ;
@@ -219,9 +223,9 @@ public OpenApiSchema GenerateSchemaForCommonData(Type commonDataSchemaType, Sche
219
223
220
224
var fullSchema = new OpenApiSchema
221
225
{
222
- Type = "object" ,
226
+ Type = JsonSchemaType . Object ,
223
227
Required = new SortedSet < string > ( [ JsonApiPropertyName . Type ] ) ,
224
- Properties = new Dictionary < string , OpenApiSchema >
228
+ Properties = new Dictionary < string , IOpenApiSchema >
225
229
{
226
230
[ JsonApiPropertyName . Type ] = referenceSchemaForResourceType . WrapInExtendedSchema ( ) ,
227
231
[ referenceSchemaForMeta . Reference . Id ] = referenceSchemaForMeta . WrapInExtendedSchema ( )
@@ -234,7 +238,7 @@ public OpenApiSchema GenerateSchemaForCommonData(Type commonDataSchemaType, Sche
234
238
} ,
235
239
Extensions =
236
240
{
237
- [ "x-abstract" ] = new OpenApiBoolean ( true )
241
+ [ "x-abstract" ] = new OpenApiAny ( true )
238
242
}
239
243
} ;
240
244
@@ -272,7 +276,7 @@ private static void SetAbstract(OpenApiSchema fullSchema, ResourceSchemaType res
272
276
{
273
277
if ( resourceSchemaType . ResourceType . ClrType . IsAbstract && resourceSchemaType . SchemaOpenType != typeof ( IdentifierInRequest < > ) )
274
278
{
275
- fullSchema . Extensions [ "x-abstract" ] = new OpenApiBoolean ( true ) ;
279
+ fullSchema . Extensions [ "x-abstract" ] = new OpenApiAny ( true ) ;
276
280
}
277
281
}
278
282
@@ -367,8 +371,8 @@ private void SetFieldSchemaMembers(OpenApiSchema fullSchemaForData, ResourceSche
367
371
{
368
372
var propertyNameInSchema = forAttributes ? JsonApiPropertyName . Attributes : JsonApiPropertyName . Relationships ;
369
373
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 ] ;
372
376
fullSchemaForFields . AdditionalPropertiesAllowed = false ;
373
377
374
378
SetAbstract ( fullSchemaForFields , resourceSchemaTypeForData ) ;
@@ -437,7 +441,7 @@ private ResourceSchemaType GetResourceSchemaTypeForFieldsProperty(ResourceSchema
437
441
return ResourceSchemaType . Create ( fieldsConstructedType , _resourceGraph ) ;
438
442
}
439
443
440
- private OpenApiSchema GenerateSchemaForCommonFields ( Type commonFieldsSchemaType , SchemaRepository schemaRepository )
444
+ private OpenApiSchemaReference GenerateSchemaForCommonFields ( Type commonFieldsSchemaType , SchemaRepository schemaRepository )
441
445
{
442
446
if ( schemaRepository . TryLookupByType ( commonFieldsSchemaType , out var referenceSchema ) )
443
447
{
@@ -450,9 +454,9 @@ private OpenApiSchema GenerateSchemaForCommonFields(Type commonFieldsSchemaType,
450
454
451
455
var fullSchema = new OpenApiSchema
452
456
{
453
- Type = "object" ,
457
+ Type = JsonSchemaType . Object ,
454
458
Required = new SortedSet < string > ( [ OpenApiMediaTypeExtension . FullyQualifiedOpenApiDiscriminatorPropertyName ] ) ,
455
- Properties = new Dictionary < string , OpenApiSchema >
459
+ Properties = new Dictionary < string , IOpenApiSchema >
456
460
{
457
461
[ OpenApiMediaTypeExtension . FullyQualifiedOpenApiDiscriminatorPropertyName ] = referenceSchemaForResourceType . WrapInExtendedSchema ( )
458
462
} ,
@@ -464,7 +468,7 @@ private OpenApiSchema GenerateSchemaForCommonFields(Type commonFieldsSchemaType,
464
468
} ,
465
469
Extensions =
466
470
{
467
- [ "x-abstract" ] = new OpenApiBoolean ( true )
471
+ [ "x-abstract" ] = new OpenApiAny ( true )
468
472
}
469
473
} ;
470
474
@@ -489,7 +493,7 @@ private void MapInDiscriminator(ResourceSchemaType resourceSchemaType, bool forR
489
493
: resourceSchemaType . ChangeResourceType ( baseResourceType ) . SchemaConstructedType ;
490
494
491
495
var referenceSchemaForBase = schemaRepository . LookupByType ( baseSchemaType ) ;
492
- var inlineSchemaForBase = schemaRepository . Schemas [ referenceSchemaForBase . Reference . Id ] . UnwrapLastExtendedSchema ( ) ;
496
+ var inlineSchemaForBase = ( OpenApiSchema ) schemaRepository . Schemas [ referenceSchemaForBase . Reference . Id ] . UnwrapLastExtendedSchema ( ) ;
493
497
494
498
inlineSchemaForBase . Discriminator ??= new OpenApiDiscriminator
495
499
{
@@ -545,9 +549,9 @@ private void MapResourceTypeInEnum(string publicName, SchemaRepository schemaRep
545
549
var schemaId = _schemaIdSelector . GetResourceTypeSchemaId ( null ) ;
546
550
var fullSchema = schemaRepository . Schemas [ schemaId ] ;
547
551
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 ) )
549
553
{
550
- fullSchema . Enum . Add ( new OpenApiString ( publicName ) ) ;
554
+ fullSchema . Enum . Add ( publicName ) ;
551
555
}
552
556
}
553
557
@@ -572,11 +576,11 @@ private void GenerateDataSchemasForDirectlyDerivedTypes(ResourceSchemaType resou
572
576
573
577
using var traceScope = _schemaGenerationTracer . TraceStart ( this , resourceSchemaTypeForDerived . SchemaConstructedType ) ;
574
578
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 ] ;
577
581
fullSchemaForDerived . AdditionalPropertiesAllowed = false ;
578
582
579
- var inlineSchemaForDerived = fullSchemaForDerived . UnwrapLastExtendedSchema ( ) ;
583
+ var inlineSchemaForDerived = ( OpenApiSchema ) fullSchemaForDerived . UnwrapLastExtendedSchema ( ) ;
580
584
SetResourceFields ( inlineSchemaForDerived , resourceSchemaTypeForDerived , forRequestSchema , schemaRepository ) ;
581
585
582
586
SetAbstract ( inlineSchemaForDerived , resourceSchemaTypeForDerived ) ;
@@ -605,7 +609,7 @@ private void GenerateDataSchemasForDirectlyDerivedTypes(ResourceSchemaType resou
605
609
if ( RequiresRootObjectTypeInDataSchema ( resourceSchemaTypeForDerived , forRequestSchema ) )
606
610
{
607
611
var fullSchemaForData = schemaRepository . Schemas [ referenceSchemaForDerived . Reference . Id ] ;
608
- fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiBoolean ( true ) ;
612
+ fullSchemaForData . Extensions [ SetSchemaTypeToObjectDocumentFilter . RequiresRootObjectTypeKey ] = new OpenApiAny ( true ) ;
609
613
}
610
614
611
615
GenerateDataSchemasForDirectlyDerivedTypes ( resourceSchemaTypeForDerived , forRequestSchema , schemaRepository ) ;
0 commit comments