|
| 1 | +using System.Reflection; |
| 2 | +using JsonApiDotNetCore.Configuration; |
| 3 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents; |
| 4 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.Relationships; |
| 5 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects; |
| 6 | +using JsonApiDotNetCore.OpenApi.SchemaGenerators.Components; |
| 7 | +using JsonApiDotNetCore.OpenApi.SwaggerComponents; |
| 8 | +using Microsoft.OpenApi.Models; |
| 9 | +using Swashbuckle.AspNetCore.SwaggerGen; |
| 10 | + |
| 11 | +namespace JsonApiDotNetCore.OpenApi.SchemaGenerators.Bodies; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Generates the OpenAPI component schema for a resource/relationship request/response body. |
| 15 | +/// </summary> |
| 16 | +internal sealed class ResourceOrRelationshipBodySchemaGenerator : BodySchemaGenerator |
| 17 | +{ |
| 18 | + private static readonly Type[] RequestSchemaTypes = |
| 19 | + [ |
| 20 | + typeof(CreateResourceRequestDocument<>), |
| 21 | + typeof(UpdateResourceRequestDocument<>), |
| 22 | + typeof(ToOneRelationshipInRequest<>), |
| 23 | + typeof(NullableToOneRelationshipInRequest<>), |
| 24 | + typeof(ToManyRelationshipInRequest<>) |
| 25 | + ]; |
| 26 | + |
| 27 | + private static readonly Type[] ResponseSchemaTypes = |
| 28 | + [ |
| 29 | + typeof(ResourceCollectionResponseDocument<>), |
| 30 | + typeof(PrimaryResourceResponseDocument<>), |
| 31 | + typeof(SecondaryResourceResponseDocument<>), |
| 32 | + typeof(NullableSecondaryResourceResponseDocument<>), |
| 33 | + typeof(ResourceIdentifierResponseDocument<>), |
| 34 | + typeof(NullableResourceIdentifierResponseDocument<>), |
| 35 | + typeof(ResourceIdentifierCollectionResponseDocument<>) |
| 36 | + ]; |
| 37 | + |
| 38 | + private readonly SchemaGenerator _defaultSchemaGenerator; |
| 39 | + private readonly AbstractResourceDataSchemaGenerator _abstractResourceDataSchemaGenerator; |
| 40 | + private readonly DataSchemaGenerator _dataSchemaGenerator; |
| 41 | + private readonly IncludeDependencyScanner _includeDependencyScanner; |
| 42 | + private readonly IResourceGraph _resourceGraph; |
| 43 | + |
| 44 | + public ResourceOrRelationshipBodySchemaGenerator(SchemaGenerator defaultSchemaGenerator, |
| 45 | + AbstractResourceDataSchemaGenerator abstractResourceDataSchemaGenerator, DataSchemaGenerator dataSchemaGenerator, |
| 46 | + MetaSchemaGenerator metaSchemaGenerator, LinksVisibilitySchemaGenerator linksVisibilitySchemaGenerator, |
| 47 | + IncludeDependencyScanner includeDependencyScanner, IResourceGraph resourceGraph, IJsonApiOptions options) |
| 48 | + : base(metaSchemaGenerator, linksVisibilitySchemaGenerator, options) |
| 49 | + { |
| 50 | + ArgumentGuard.NotNull(defaultSchemaGenerator); |
| 51 | + ArgumentGuard.NotNull(abstractResourceDataSchemaGenerator); |
| 52 | + ArgumentGuard.NotNull(dataSchemaGenerator); |
| 53 | + ArgumentGuard.NotNull(includeDependencyScanner); |
| 54 | + ArgumentGuard.NotNull(resourceGraph); |
| 55 | + |
| 56 | + _defaultSchemaGenerator = defaultSchemaGenerator; |
| 57 | + _abstractResourceDataSchemaGenerator = abstractResourceDataSchemaGenerator; |
| 58 | + _dataSchemaGenerator = dataSchemaGenerator; |
| 59 | + _includeDependencyScanner = includeDependencyScanner; |
| 60 | + _resourceGraph = resourceGraph; |
| 61 | + } |
| 62 | + |
| 63 | + public override bool CanGenerate(Type modelType) |
| 64 | + { |
| 65 | + Type modelOpenType = modelType.ConstructedToOpenType(); |
| 66 | + return RequestSchemaTypes.Contains(modelOpenType) || ResponseSchemaTypes.Contains(modelOpenType); |
| 67 | + } |
| 68 | + |
| 69 | + protected override OpenApiSchema GenerateBodySchema(Type bodyType, SchemaRepository schemaRepository) |
| 70 | + { |
| 71 | + ArgumentGuard.NotNull(bodyType); |
| 72 | + ArgumentGuard.NotNull(schemaRepository); |
| 73 | + |
| 74 | + if (schemaRepository.TryLookupByType(bodyType, out OpenApiSchema? referenceSchemaForBody)) |
| 75 | + { |
| 76 | + return referenceSchemaForBody; |
| 77 | + } |
| 78 | + |
| 79 | + bool isRequestSchema = RequestSchemaTypes.Contains(bodyType.ConstructedToOpenType()); |
| 80 | + |
| 81 | + if (!isRequestSchema) |
| 82 | + { |
| 83 | + // There's no way to intercept in the Swashbuckle recursive component schema generation when using inheritance, which we need |
| 84 | + // to perform generic type expansions. As a workaround, we generate an empty base schema upfront. Each time the schema |
| 85 | + // for a derived type is generated, we'll add it to the discriminator mapping. |
| 86 | + _ = _abstractResourceDataSchemaGenerator.GenerateSchema(schemaRepository); |
| 87 | + } |
| 88 | + |
| 89 | + Type dataConstructedType = GetInnerTypeOfDataProperty(bodyType); |
| 90 | + |
| 91 | + if (!isRequestSchema) |
| 92 | + { |
| 93 | + // Ensure all reachable related resource types are available in the discriminator mapping so includes work. |
| 94 | + // Doing this matters when not all endpoints are exposed. |
| 95 | + EnsureResourceTypesAreMappedInDiscriminator(dataConstructedType, schemaRepository); |
| 96 | + } |
| 97 | + |
| 98 | + OpenApiSchema referenceSchemaForData = _dataSchemaGenerator.GenerateSchema(dataConstructedType, schemaRepository); |
| 99 | + |
| 100 | + if (!isRequestSchema) |
| 101 | + { |
| 102 | + _abstractResourceDataSchemaGenerator.MapDiscriminator(dataConstructedType, referenceSchemaForData, schemaRepository); |
| 103 | + } |
| 104 | + |
| 105 | + referenceSchemaForBody = _defaultSchemaGenerator.GenerateSchema(bodyType, schemaRepository); |
| 106 | + OpenApiSchema fullSchemaForBody = schemaRepository.Schemas[referenceSchemaForBody.Reference.Id].UnwrapLastExtendedSchema(); |
| 107 | + |
| 108 | + if (JsonApiSchemaFacts.HasNullableDataProperty(bodyType)) |
| 109 | + { |
| 110 | + fullSchemaForBody.Properties[JsonApiPropertyName.Data].Nullable = true; |
| 111 | + } |
| 112 | + |
| 113 | + return referenceSchemaForBody; |
| 114 | + } |
| 115 | + |
| 116 | + private static Type GetInnerTypeOfDataProperty(Type bodyType) |
| 117 | + { |
| 118 | + PropertyInfo? dataProperty = bodyType.GetProperty("Data"); |
| 119 | + |
| 120 | + if (dataProperty == null) |
| 121 | + { |
| 122 | + throw new UnreachableCodeException(); |
| 123 | + } |
| 124 | + |
| 125 | + return dataProperty.PropertyType.ConstructedToOpenType().IsAssignableTo(typeof(ICollection<>)) |
| 126 | + ? dataProperty.PropertyType.GenericTypeArguments[0] |
| 127 | + : dataProperty.PropertyType; |
| 128 | + } |
| 129 | + |
| 130 | + private void EnsureResourceTypesAreMappedInDiscriminator(Type dataConstructedType, SchemaRepository schemaRepository) |
| 131 | + { |
| 132 | + Type dataOpenType = dataConstructedType.GetGenericTypeDefinition(); |
| 133 | + |
| 134 | + if (dataOpenType == typeof(ResourceDataInResponse<>)) |
| 135 | + { |
| 136 | + var resourceTypeInfo = ResourceTypeInfo.Create(dataConstructedType, _resourceGraph); |
| 137 | + |
| 138 | + foreach (ResourceType resourceType in _includeDependencyScanner.GetReachableRelatedTypes(resourceTypeInfo.ResourceType)) |
| 139 | + { |
| 140 | + EnsureResourceTypeIsMappedInDiscriminator(schemaRepository, resourceType); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void EnsureResourceTypeIsMappedInDiscriminator(SchemaRepository schemaRepository, ResourceType resourceType) |
| 146 | + { |
| 147 | + Type resourceDataConstructedType = typeof(ResourceDataInResponse<>).MakeGenericType(resourceType.ClrType); |
| 148 | + OpenApiSchema referenceSchemaForResourceData = _dataSchemaGenerator.GenerateSchema(resourceDataConstructedType, schemaRepository); |
| 149 | + |
| 150 | + _abstractResourceDataSchemaGenerator.MapDiscriminator(resourceDataConstructedType, referenceSchemaForResourceData, schemaRepository); |
| 151 | + } |
| 152 | +} |
0 commit comments