Skip to content

Commit 2c1869f

Browse files
committed
Include jsonapi element or not, depending on options
1 parent 500638e commit 2c1869f

File tree

9 files changed

+25
-96
lines changed

9 files changed

+25
-96
lines changed

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiSchemaGenerator.cs

+21-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal sealed class JsonApiSchemaGenerator : ISchemaGenerator
4545
};
4646

4747
private readonly ISchemaGenerator _defaultSchemaGenerator;
48+
private readonly IJsonApiOptions _options;
4849
private readonly ResourceObjectSchemaGenerator _resourceObjectSchemaGenerator;
4950
private readonly NullableReferenceSchemaGenerator _nullableReferenceSchemaGenerator;
5051
private readonly SchemaRepositoryAccessor _schemaRepositoryAccessor = new();
@@ -58,6 +59,7 @@ public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceG
5859
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);
5960

6061
_defaultSchemaGenerator = defaultSchemaGenerator;
62+
_options = options;
6163
_nullableReferenceSchemaGenerator = new NullableReferenceSchemaGenerator(_schemaRepositoryAccessor, options.SerializerOptions.PropertyNamingPolicy);
6264

6365
_resourceObjectSchemaGenerator = new ResourceObjectSchemaGenerator(defaultSchemaGenerator, resourceGraph, options, _schemaRepositoryAccessor,
@@ -85,6 +87,11 @@ public OpenApiSchema GenerateSchema(Type modelType, SchemaRepository schemaRepos
8587
{
8688
SetDataObjectSchemaToNullable(schema);
8789
}
90+
91+
if (!_options.IncludeJsonApiVersion)
92+
{
93+
RemoveJsonApiObject(schema);
94+
}
8895
}
8996

9097
return _defaultSchemaGenerator.GenerateSchema(modelType, schemaRepository, memberInfo, parameterInfo, routeInfo);
@@ -130,19 +137,27 @@ private static bool IsDataPropertyNullableInDocument(Type documentType)
130137
return JsonApiDocumentWithNullableDataOpenTypes.Contains(documentOpenType);
131138
}
132139

140+
private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSchemaForResourceObject)
141+
{
142+
return new OpenApiSchema
143+
{
144+
Items = referenceSchemaForResourceObject,
145+
Type = "array"
146+
};
147+
}
148+
133149
private void SetDataObjectSchemaToNullable(OpenApiSchema referenceSchemaForDocument)
134150
{
135151
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
136152
OpenApiSchema referenceSchemaForData = fullSchemaForDocument.Properties[JsonApiPropertyName.Data];
137153
fullSchemaForDocument.Properties[JsonApiPropertyName.Data] = _nullableReferenceSchemaGenerator.GenerateSchema(referenceSchemaForData);
138154
}
139155

140-
private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSchemaForResourceObject)
156+
private void RemoveJsonApiObject(OpenApiSchema referenceSchemaForDocument)
141157
{
142-
return new OpenApiSchema
143-
{
144-
Items = referenceSchemaForResourceObject,
145-
Type = "array"
146-
};
158+
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
159+
fullSchemaForDocument.Properties.Remove(JsonApiPropertyName.Jsonapi);
160+
161+
_schemaRepositoryAccessor.Current.Schemas.Remove("jsonapi-object");
147162
}
148163
}

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOff/ModelStateValidationOff/swagger.g.json

-18
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,6 @@
934934
],
935935
"type": "object",
936936
"properties": {
937-
"jsonapi": {
938-
"$ref": "#/components/schemas/jsonapiObject"
939-
},
940937
"links": {
941938
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
942939
},
@@ -1002,9 +999,6 @@
1002999
],
10031000
"type": "object",
10041001
"properties": {
1005-
"jsonapi": {
1006-
"$ref": "#/components/schemas/jsonapiObject"
1007-
},
10081002
"links": {
10091003
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
10101004
},
@@ -1215,9 +1209,6 @@
12151209
],
12161210
"type": "object",
12171211
"properties": {
1218-
"jsonapi": {
1219-
"$ref": "#/components/schemas/jsonapiObject"
1220-
},
12211212
"links": {
12221213
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
12231214
},
@@ -1245,9 +1236,6 @@
12451236
],
12461237
"type": "object",
12471238
"properties": {
1248-
"jsonapi": {
1249-
"$ref": "#/components/schemas/jsonapiObject"
1250-
},
12511239
"links": {
12521240
"$ref": "#/components/schemas/linksInResourceDocument"
12531241
},
@@ -1429,9 +1417,6 @@
14291417
],
14301418
"type": "object",
14311419
"properties": {
1432-
"jsonapi": {
1433-
"$ref": "#/components/schemas/jsonapiObject"
1434-
},
14351420
"links": {
14361421
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
14371422
},
@@ -1551,9 +1536,6 @@
15511536
],
15521537
"type": "object",
15531538
"properties": {
1554-
"jsonapi": {
1555-
"$ref": "#/components/schemas/jsonapiObject"
1556-
},
15571539
"links": {
15581540
"$ref": "#/components/schemas/linksInResourceDocument"
15591541
},

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOff/ModelStateValidationOn/swagger.g.json

-24
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,6 @@
934934
],
935935
"type": "object",
936936
"properties": {
937-
"jsonapi": {
938-
"$ref": "#/components/schemas/jsonapiObject"
939-
},
940937
"links": {
941938
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
942939
},
@@ -1002,9 +999,6 @@
1002999
],
10031000
"type": "object",
10041001
"properties": {
1005-
"jsonapi": {
1006-
"$ref": "#/components/schemas/jsonapiObject"
1007-
},
10081002
"links": {
10091003
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
10101004
},
@@ -1028,9 +1022,6 @@
10281022
],
10291023
"type": "object",
10301024
"properties": {
1031-
"jsonapi": {
1032-
"$ref": "#/components/schemas/jsonapiObject"
1033-
},
10341025
"links": {
10351026
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
10361027
},
@@ -1057,9 +1048,6 @@
10571048
],
10581049
"type": "object",
10591050
"properties": {
1060-
"jsonapi": {
1061-
"$ref": "#/components/schemas/jsonapiObject"
1062-
},
10631051
"links": {
10641052
"$ref": "#/components/schemas/linksInResourceDocument"
10651053
},
@@ -1261,9 +1249,6 @@
12611249
],
12621250
"type": "object",
12631251
"properties": {
1264-
"jsonapi": {
1265-
"$ref": "#/components/schemas/jsonapiObject"
1266-
},
12671252
"links": {
12681253
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
12691254
},
@@ -1291,9 +1276,6 @@
12911276
],
12921277
"type": "object",
12931278
"properties": {
1294-
"jsonapi": {
1295-
"$ref": "#/components/schemas/jsonapiObject"
1296-
},
12971279
"links": {
12981280
"$ref": "#/components/schemas/linksInResourceDocument"
12991281
},
@@ -1467,9 +1449,6 @@
14671449
],
14681450
"type": "object",
14691451
"properties": {
1470-
"jsonapi": {
1471-
"$ref": "#/components/schemas/jsonapiObject"
1472-
},
14731452
"links": {
14741453
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
14751454
},
@@ -1589,9 +1568,6 @@
15891568
],
15901569
"type": "object",
15911570
"properties": {
1592-
"jsonapi": {
1593-
"$ref": "#/components/schemas/jsonapiObject"
1594-
},
15951571
"links": {
15961572
"$ref": "#/components/schemas/linksInResourceDocument"
15971573
},

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOn/ModelStateValidationOff/swagger.g.json

-24
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,6 @@
12361236
],
12371237
"type": "object",
12381238
"properties": {
1239-
"jsonapi": {
1240-
"$ref": "#/components/schemas/jsonapiObject"
1241-
},
12421239
"links": {
12431240
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
12441241
},
@@ -1304,9 +1301,6 @@
13041301
],
13051302
"type": "object",
13061303
"properties": {
1307-
"jsonapi": {
1308-
"$ref": "#/components/schemas/jsonapiObject"
1309-
},
13101304
"links": {
13111305
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
13121306
},
@@ -1330,9 +1324,6 @@
13301324
],
13311325
"type": "object",
13321326
"properties": {
1333-
"jsonapi": {
1334-
"$ref": "#/components/schemas/jsonapiObject"
1335-
},
13361327
"links": {
13371328
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
13381329
},
@@ -1359,9 +1350,6 @@
13591350
],
13601351
"type": "object",
13611352
"properties": {
1362-
"jsonapi": {
1363-
"$ref": "#/components/schemas/jsonapiObject"
1364-
},
13651353
"links": {
13661354
"$ref": "#/components/schemas/linksInResourceDocument"
13671355
},
@@ -1563,9 +1551,6 @@
15631551
],
15641552
"type": "object",
15651553
"properties": {
1566-
"jsonapi": {
1567-
"$ref": "#/components/schemas/jsonapiObject"
1568-
},
15691554
"links": {
15701555
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
15711556
},
@@ -1593,9 +1578,6 @@
15931578
],
15941579
"type": "object",
15951580
"properties": {
1596-
"jsonapi": {
1597-
"$ref": "#/components/schemas/jsonapiObject"
1598-
},
15991581
"links": {
16001582
"$ref": "#/components/schemas/linksInResourceDocument"
16011583
},
@@ -1800,9 +1782,6 @@
18001782
],
18011783
"type": "object",
18021784
"properties": {
1803-
"jsonapi": {
1804-
"$ref": "#/components/schemas/jsonapiObject"
1805-
},
18061785
"links": {
18071786
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
18081787
},
@@ -1922,9 +1901,6 @@
19221901
],
19231902
"type": "object",
19241903
"properties": {
1925-
"jsonapi": {
1926-
"$ref": "#/components/schemas/jsonapiObject"
1927-
},
19281904
"links": {
19291905
"$ref": "#/components/schemas/linksInResourceDocument"
19301906
},

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesOn/ModelStateValidationOn/swagger.g.json

-24
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,6 @@
12361236
],
12371237
"type": "object",
12381238
"properties": {
1239-
"jsonapi": {
1240-
"$ref": "#/components/schemas/jsonapiObject"
1241-
},
12421239
"links": {
12431240
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
12441241
},
@@ -1304,9 +1301,6 @@
13041301
],
13051302
"type": "object",
13061303
"properties": {
1307-
"jsonapi": {
1308-
"$ref": "#/components/schemas/jsonapiObject"
1309-
},
13101304
"links": {
13111305
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
13121306
},
@@ -1330,9 +1324,6 @@
13301324
],
13311325
"type": "object",
13321326
"properties": {
1333-
"jsonapi": {
1334-
"$ref": "#/components/schemas/jsonapiObject"
1335-
},
13361327
"links": {
13371328
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
13381329
},
@@ -1359,9 +1350,6 @@
13591350
],
13601351
"type": "object",
13611352
"properties": {
1362-
"jsonapi": {
1363-
"$ref": "#/components/schemas/jsonapiObject"
1364-
},
13651353
"links": {
13661354
"$ref": "#/components/schemas/linksInResourceDocument"
13671355
},
@@ -1563,9 +1551,6 @@
15631551
],
15641552
"type": "object",
15651553
"properties": {
1566-
"jsonapi": {
1567-
"$ref": "#/components/schemas/jsonapiObject"
1568-
},
15691554
"links": {
15701555
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
15711556
},
@@ -1593,9 +1578,6 @@
15931578
],
15941579
"type": "object",
15951580
"properties": {
1596-
"jsonapi": {
1597-
"$ref": "#/components/schemas/jsonapiObject"
1598-
},
15991581
"links": {
16001582
"$ref": "#/components/schemas/linksInResourceDocument"
16011583
},
@@ -1794,9 +1776,6 @@
17941776
],
17951777
"type": "object",
17961778
"properties": {
1797-
"jsonapi": {
1798-
"$ref": "#/components/schemas/jsonapiObject"
1799-
},
18001779
"links": {
18011780
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
18021781
},
@@ -1916,9 +1895,6 @@
19161895
],
19171896
"type": "object",
19181897
"properties": {
1919-
"jsonapi": {
1920-
"$ref": "#/components/schemas/jsonapiObject"
1921-
},
19221898
"links": {
19231899
"$ref": "#/components/schemas/linksInResourceDocument"
19241900
},

test/OpenApiTests/LegacyOpenApiIntegration/LegacyOpenApiIntegrationStartup.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ protected override void SetJsonApiOptions(JsonApiOptions options)
1616

1717
options.Namespace = "api";
1818
options.DefaultAttrCapabilities = AttrCapabilities.AllowView;
19+
options.IncludeJsonApiVersion = true;
1920
options.SerializerOptions.PropertyNamingPolicy = JsonKebabCaseNamingPolicy.Instance;
2021
options.SerializerOptions.DictionaryKeyPolicy = JsonKebabCaseNamingPolicy.Instance;
2122
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());

test/OpenApiTests/NamingConventions/CamelCase/CamelCaseNamingConventionStartup.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected override void SetJsonApiOptions(JsonApiOptions options)
1414
{
1515
base.SetJsonApiOptions(options);
1616

17+
options.IncludeJsonApiVersion = true;
1718
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
1819
options.SerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
1920
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());

test/OpenApiTests/NamingConventions/KebabCase/KebabCaseNamingConventionStartup.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected override void SetJsonApiOptions(JsonApiOptions options)
1414
{
1515
base.SetJsonApiOptions(options);
1616

17+
options.IncludeJsonApiVersion = true;
1718
options.SerializerOptions.PropertyNamingPolicy = JsonKebabCaseNamingPolicy.Instance;
1819
options.SerializerOptions.DictionaryKeyPolicy = JsonKebabCaseNamingPolicy.Instance;
1920
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());

test/OpenApiTests/NamingConventions/PascalCase/PascalCaseNamingConventionStartup.cs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ protected override void SetJsonApiOptions(JsonApiOptions options)
1313
{
1414
base.SetJsonApiOptions(options);
1515

16+
options.IncludeJsonApiVersion = true;
1617
options.SerializerOptions.PropertyNamingPolicy = null;
1718
options.SerializerOptions.DictionaryKeyPolicy = null;
1819
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());

0 commit comments

Comments
 (0)