Skip to content

OpenAPI: Include jsonapi element or not, depending on options #1365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public JsonApiActionDescriptorCollectionProvider(IControllerResourceMapping cont
{
ArgumentGuard.NotNull(controllerResourceMapping);
ArgumentGuard.NotNull(defaultProvider);
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);

_defaultProvider = defaultProvider;
_jsonApiEndpointMetadataProvider = new JsonApiEndpointMetadataProvider(controllerResourceMapping, resourceFieldValidationMetadataProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public JsonApiEndpointMetadataProvider(IControllerResourceMapping controllerReso
ResourceFieldValidationMetadataProvider resourceFieldValidationMetadataProvider)
{
ArgumentGuard.NotNull(controllerResourceMapping);
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);

_nonPrimaryDocumentTypeFactory = new NonPrimaryDocumentTypeFactory(resourceFieldValidationMetadataProvider);
_controllerResourceMapping = controllerResourceMapping;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal sealed class RelationshipTypeFactory

public RelationshipTypeFactory(ResourceFieldValidationMetadataProvider resourceFieldValidationMetadataProvider)
{
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);

_nonPrimaryDocumentTypeFactory = new NonPrimaryDocumentTypeFactory(resourceFieldValidationMetadataProvider);
_resourceFieldValidationMetadataProvider = resourceFieldValidationMetadataProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal sealed class CachingSwaggerGenerator : ISwaggerProvider
public CachingSwaggerGenerator(SwaggerGenerator defaultSwaggerGenerator)
{
ArgumentGuard.NotNull(defaultSwaggerGenerator);

_defaultSwaggerGenerator = defaultSwaggerGenerator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal sealed class JsonApiSchemaGenerator : ISchemaGenerator
};

private readonly ISchemaGenerator _defaultSchemaGenerator;
private readonly IJsonApiOptions _options;
private readonly ResourceObjectSchemaGenerator _resourceObjectSchemaGenerator;
private readonly NullableReferenceSchemaGenerator _nullableReferenceSchemaGenerator;
private readonly SchemaRepositoryAccessor _schemaRepositoryAccessor = new();
Expand All @@ -55,8 +56,10 @@ public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceG
ArgumentGuard.NotNull(defaultSchemaGenerator);
ArgumentGuard.NotNull(resourceGraph);
ArgumentGuard.NotNull(options);
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);

_defaultSchemaGenerator = defaultSchemaGenerator;
_options = options;
_nullableReferenceSchemaGenerator = new NullableReferenceSchemaGenerator(_schemaRepositoryAccessor, options.SerializerOptions.PropertyNamingPolicy);

_resourceObjectSchemaGenerator = new ResourceObjectSchemaGenerator(defaultSchemaGenerator, resourceGraph, options, _schemaRepositoryAccessor,
Expand Down Expand Up @@ -84,6 +87,11 @@ public OpenApiSchema GenerateSchema(Type modelType, SchemaRepository schemaRepos
{
SetDataObjectSchemaToNullable(schema);
}

if (!_options.IncludeJsonApiVersion)
{
RemoveJsonApiObject(schema);
}
}

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

private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSchemaForResourceObject)
{
return new OpenApiSchema
{
Items = referenceSchemaForResourceObject,
Type = "array"
};
}

private void SetDataObjectSchemaToNullable(OpenApiSchema referenceSchemaForDocument)
{
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
OpenApiSchema referenceSchemaForData = fullSchemaForDocument.Properties[JsonApiPropertyName.Data];
fullSchemaForDocument.Properties[JsonApiPropertyName.Data] = _nullableReferenceSchemaGenerator.GenerateSchema(referenceSchemaForData);
}

private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSchemaForResourceObject)
private void RemoveJsonApiObject(OpenApiSchema referenceSchemaForDocument)
{
return new OpenApiSchema
{
Items = referenceSchemaForResourceObject,
Type = "array"
};
OpenApiSchema fullSchemaForDocument = _schemaRepositoryAccessor.Current.Schemas[referenceSchemaForDocument.Reference.Id];
fullSchemaForDocument.Properties.Remove(JsonApiPropertyName.Jsonapi);

_schemaRepositoryAccessor.Current.Schemas.Remove("jsonapi-object");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public NullableReferenceSchemaGenerator(ISchemaRepositoryAccessor schemaReposito
ArgumentGuard.NotNull(schemaRepositoryAccessor);

_schemaRepositoryAccessor = schemaRepositoryAccessor;

_nullableSchemaReferenceId = namingPolicy != null ? namingPolicy.ConvertName(PascalCaseNullableSchemaReferenceId) : PascalCaseNullableSchemaReferenceId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ internal static class OpenApiSchemaExtensions
{
public static void ReorderProperties(this OpenApiSchema fullSchemaForResourceObject, IEnumerable<string> propertyNamesInOrder)
{
ArgumentGuard.NotNull(fullSchemaForResourceObject);
ArgumentGuard.NotNull(propertyNamesInOrder);

var propertiesInOrder = new Dictionary<string, OpenApiSchema>();

foreach (string propertyName in propertyNamesInOrder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private IDictionary<string, OpenApiSchema> GetFieldSchemas()

public void SetMembersOfAttributesObject(OpenApiSchema fullSchemaForAttributesObject)
{
ArgumentGuard.NotNull(fullSchemaForAttributesObject);

AttrCapabilities requiredCapability = GetRequiredCapabilityForAttributes(_resourceTypeInfo.ResourceObjectOpenType);

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

public void SetMembersOfRelationshipsObject(OpenApiSchema fullSchemaForRelationshipsObject)
{
ArgumentGuard.NotNull(fullSchemaForRelationshipsObject);

foreach (string fieldName in _schemasForResourceFields.Keys)
{
RelationshipAttribute? matchingRelationship = _resourceTypeInfo.ResourceType.FindRelationshipByPublicName(fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ResourceObjectSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IRe
ArgumentGuard.NotNull(resourceGraph);
ArgumentGuard.NotNull(options);
ArgumentGuard.NotNull(schemaRepositoryAccessor);
ArgumentGuard.NotNull(resourceFieldValidationMetadataProvider);

_defaultSchemaGenerator = defaultSchemaGenerator;
_resourceGraph = resourceGraph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public SchemaRepository Current
{
if (_schemaRepository == null)
{
throw new InvalidOperationException("SchemaRepository unavailable.");
throw new InvalidOperationException("SchemaRepository is unavailable.");
}

return _schemaRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
},
Expand Down Expand Up @@ -1002,9 +999,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
},
Expand Down Expand Up @@ -1215,9 +1209,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
},
Expand Down Expand Up @@ -1245,9 +1236,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down Expand Up @@ -1429,9 +1417,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
},
Expand Down Expand Up @@ -1551,9 +1536,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
},
Expand Down Expand Up @@ -1002,9 +999,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
},
Expand All @@ -1028,9 +1022,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
},
Expand All @@ -1057,9 +1048,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down Expand Up @@ -1261,9 +1249,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
},
Expand Down Expand Up @@ -1291,9 +1276,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down Expand Up @@ -1467,9 +1449,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
},
Expand Down Expand Up @@ -1589,9 +1568,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1236,9 +1236,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
},
Expand Down Expand Up @@ -1304,9 +1301,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierCollectionDocument"
},
Expand All @@ -1330,9 +1324,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
},
Expand All @@ -1359,9 +1350,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down Expand Up @@ -1563,9 +1551,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceIdentifierDocument"
},
Expand Down Expand Up @@ -1593,9 +1578,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down Expand Up @@ -1800,9 +1782,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceCollectionDocument"
},
Expand Down Expand Up @@ -1922,9 +1901,6 @@
],
"type": "object",
"properties": {
"jsonapi": {
"$ref": "#/components/schemas/jsonapiObject"
},
"links": {
"$ref": "#/components/schemas/linksInResourceDocument"
},
Expand Down
Loading