Skip to content

Commit 86af528

Browse files
authored
Merge pull request #1365 from json-api-dotnet/jsonapi-object
OpenAPI: Include jsonapi element or not, depending on options
2 parents 0709ea4 + 2c1869f commit 86af528

File tree

18 files changed

+41
-98
lines changed

18 files changed

+41
-98
lines changed

src/JsonApiDotNetCore.OpenApi/JsonApiActionDescriptorCollectionProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public JsonApiActionDescriptorCollectionProvider(IControllerResourceMapping cont
2727
{
2828
ArgumentGuard.NotNull(controllerResourceMapping);
2929
ArgumentGuard.NotNull(defaultProvider);
30+
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);
3031

3132
_defaultProvider = defaultProvider;
3233
_jsonApiEndpointMetadataProvider = new JsonApiEndpointMetadataProvider(controllerResourceMapping, resourceFieldValidationMetadataProvider);

src/JsonApiDotNetCore.OpenApi/JsonApiMetadata/JsonApiEndpointMetadataProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public JsonApiEndpointMetadataProvider(IControllerResourceMapping controllerReso
2020
ResourceFieldValidationMetadataProvider resourceFieldValidationMetadataProvider)
2121
{
2222
ArgumentGuard.NotNull(controllerResourceMapping);
23+
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);
24+
2325
_nonPrimaryDocumentTypeFactory = new NonPrimaryDocumentTypeFactory(resourceFieldValidationMetadataProvider);
2426
_controllerResourceMapping = controllerResourceMapping;
2527
}

src/JsonApiDotNetCore.OpenApi/JsonApiMetadata/RelationshipTypeFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ internal sealed class RelationshipTypeFactory
1010

1111
public RelationshipTypeFactory(ResourceFieldValidationMetadataProvider resourceFieldValidationMetadataProvider)
1212
{
13+
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);
14+
1315
_nonPrimaryDocumentTypeFactory = new NonPrimaryDocumentTypeFactory(resourceFieldValidationMetadataProvider);
1416
_resourceFieldValidationMetadataProvider = resourceFieldValidationMetadataProvider;
1517
}

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/CachingSwaggerGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal sealed class CachingSwaggerGenerator : ISwaggerProvider
1717
public CachingSwaggerGenerator(SwaggerGenerator defaultSwaggerGenerator)
1818
{
1919
ArgumentGuard.NotNull(defaultSwaggerGenerator);
20+
2021
_defaultSwaggerGenerator = defaultSwaggerGenerator;
2122
}
2223

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiSchemaGenerator.cs

Lines changed: 22 additions & 6 deletions
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();
@@ -55,8 +56,10 @@ public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceG
5556
ArgumentGuard.NotNull(defaultSchemaGenerator);
5657
ArgumentGuard.NotNull(resourceGraph);
5758
ArgumentGuard.NotNull(options);
59+
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);
5860

5961
_defaultSchemaGenerator = defaultSchemaGenerator;
62+
_options = options;
6063
_nullableReferenceSchemaGenerator = new NullableReferenceSchemaGenerator(_schemaRepositoryAccessor, options.SerializerOptions.PropertyNamingPolicy);
6164

6265
_resourceObjectSchemaGenerator = new ResourceObjectSchemaGenerator(defaultSchemaGenerator, resourceGraph, options, _schemaRepositoryAccessor,
@@ -84,6 +87,11 @@ public OpenApiSchema GenerateSchema(Type modelType, SchemaRepository schemaRepos
8487
{
8588
SetDataObjectSchemaToNullable(schema);
8689
}
90+
91+
if (!_options.IncludeJsonApiVersion)
92+
{
93+
RemoveJsonApiObject(schema);
94+
}
8795
}
8896

8997
return _defaultSchemaGenerator.GenerateSchema(modelType, schemaRepository, memberInfo, parameterInfo, routeInfo);
@@ -129,19 +137,27 @@ private static bool IsDataPropertyNullableInDocument(Type documentType)
129137
return JsonApiDocumentWithNullableDataOpenTypes.Contains(documentOpenType);
130138
}
131139

140+
private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSchemaForResourceObject)
141+
{
142+
return new OpenApiSchema
143+
{
144+
Items = referenceSchemaForResourceObject,
145+
Type = "array"
146+
};
147+
}
148+
132149
private void SetDataObjectSchemaToNullable(OpenApiSchema referenceSchemaForDocument)
133150
{
134151
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
135152
OpenApiSchema referenceSchemaForData = fullSchemaForDocument.Properties[JsonApiPropertyName.Data];
136153
fullSchemaForDocument.Properties[JsonApiPropertyName.Data] = _nullableReferenceSchemaGenerator.GenerateSchema(referenceSchemaForData);
137154
}
138155

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

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/NullableReferenceSchemaGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public NullableReferenceSchemaGenerator(ISchemaRepositoryAccessor schemaReposito
2020
ArgumentGuard.NotNull(schemaRepositoryAccessor);
2121

2222
_schemaRepositoryAccessor = schemaRepositoryAccessor;
23-
2423
_nullableSchemaReferenceId = namingPolicy != null ? namingPolicy.ConvertName(PascalCaseNullableSchemaReferenceId) : PascalCaseNullableSchemaReferenceId;
2524
}
2625

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/OpenApiSchemaExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ internal static class OpenApiSchemaExtensions
66
{
77
public static void ReorderProperties(this OpenApiSchema fullSchemaForResourceObject, IEnumerable<string> propertyNamesInOrder)
88
{
9+
ArgumentGuard.NotNull(fullSchemaForResourceObject);
10+
ArgumentGuard.NotNull(propertyNamesInOrder);
11+
912
var propertiesInOrder = new Dictionary<string, OpenApiSchema>();
1013

1114
foreach (string propertyName in propertyNamesInOrder)

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceFieldObjectSchemaBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ private IDictionary<string, OpenApiSchema> GetFieldSchemas()
7474

7575
public void SetMembersOfAttributesObject(OpenApiSchema fullSchemaForAttributesObject)
7676
{
77+
ArgumentGuard.NotNull(fullSchemaForAttributesObject);
78+
7779
AttrCapabilities requiredCapability = GetRequiredCapabilityForAttributes(_resourceTypeInfo.ResourceObjectOpenType);
7880

7981
foreach ((string fieldName, OpenApiSchema resourceFieldSchema) in _schemasForResourceFields)
@@ -127,6 +129,8 @@ private bool IsFieldRequired(ResourceFieldAttribute field)
127129

128130
public void SetMembersOfRelationshipsObject(OpenApiSchema fullSchemaForRelationshipsObject)
129131
{
132+
ArgumentGuard.NotNull(fullSchemaForRelationshipsObject);
133+
130134
foreach (string fieldName in _schemasForResourceFields.Keys)
131135
{
132136
RelationshipAttribute? matchingRelationship = _resourceTypeInfo.ResourceType.FindRelationshipByPublicName(fieldName);

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/ResourceObjectSchemaGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IRe
3131
ArgumentGuard.NotNull(resourceGraph);
3232
ArgumentGuard.NotNull(options);
3333
ArgumentGuard.NotNull(schemaRepositoryAccessor);
34+
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);
3435

3536
_defaultSchemaGenerator = defaultSchemaGenerator;
3637
_resourceGraph = resourceGraph;

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/SchemaRepositoryAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public SchemaRepository Current
1212
{
1313
if (_schemaRepository == null)
1414
{
15-
throw new InvalidOperationException("SchemaRepository unavailable.");
15+
throw new InvalidOperationException("SchemaRepository is unavailable.");
1616
}
1717

1818
return _schemaRepository;

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

Lines changed: 0 additions & 18 deletions
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

Lines changed: 0 additions & 24 deletions
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

Lines changed: 0 additions & 24 deletions
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
},

0 commit comments

Comments
 (0)