|
| 1 | +using JsonApiDotNetCore.Configuration; |
| 2 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents; |
| 3 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.Relationships; |
| 4 | +using JsonApiDotNetCore.OpenApi.JsonApiObjects.ResourceObjects; |
| 5 | +using JsonApiDotNetCore.Resources.Annotations; |
| 6 | +using Microsoft.OpenApi.Models; |
| 7 | +using Swashbuckle.AspNetCore.SwaggerGen; |
| 8 | + |
| 9 | +namespace JsonApiDotNetCore.OpenApi.SwaggerComponents; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Hides links that are never returned. |
| 13 | +/// </summary> |
| 14 | +/// <remarks> |
| 15 | +/// Tradeoff: Special-casing links per resource type and per relationship means an explosion of expanded types, only because the links visibility may |
| 16 | +/// vary. Furthermore, relationship links fallback to their left-type resource, whereas we generate right-type component schemas for relationships. To |
| 17 | +/// keep it simple, we take the union of exposed links on resource types and relationships. Only what's not in this unification gets hidden. For example, |
| 18 | +/// when options == None, typeof(Blogs) == Self, and typeof(Posts) == Related, we'll keep Self | Related for both Blogs and Posts, and remove any other |
| 19 | +/// links. |
| 20 | +/// </remarks> |
| 21 | +internal sealed class LinksVisibilitySchemaGenerator |
| 22 | +{ |
| 23 | + private const LinkTypes ResourceTopLinkTypes = LinkTypes.Self | LinkTypes.DescribedBy; |
| 24 | + private const LinkTypes ResourceCollectionTopLinkTypes = LinkTypes.Self | LinkTypes.DescribedBy | LinkTypes.Pagination; |
| 25 | + private const LinkTypes ResourceIdentifierTopLinkTypes = LinkTypes.Self | LinkTypes.Related | LinkTypes.DescribedBy; |
| 26 | + private const LinkTypes ResourceIdentifierCollectionTopLinkTypes = LinkTypes.Self | LinkTypes.Related | LinkTypes.DescribedBy | LinkTypes.Pagination; |
| 27 | + private const LinkTypes ErrorTopLinkTypes = LinkTypes.Self | LinkTypes.DescribedBy; |
| 28 | + private const LinkTypes RelationshipLinkTypes = LinkTypes.Self | LinkTypes.Related; |
| 29 | + private const LinkTypes ResourceLinkTypes = LinkTypes.Self; |
| 30 | + |
| 31 | + private static readonly Dictionary<Type, LinkTypes> LinksInJsonApiComponentTypes = new() |
| 32 | + { |
| 33 | + [typeof(NullableSecondaryResourceResponseDocument<>)] = ResourceTopLinkTypes, |
| 34 | + [typeof(PrimaryResourceResponseDocument<>)] = ResourceTopLinkTypes, |
| 35 | + [typeof(SecondaryResourceResponseDocument<>)] = ResourceTopLinkTypes, |
| 36 | + [typeof(ResourceCollectionResponseDocument<>)] = ResourceCollectionTopLinkTypes, |
| 37 | + [typeof(ResourceIdentifierResponseDocument<>)] = ResourceIdentifierTopLinkTypes, |
| 38 | + [typeof(NullableResourceIdentifierResponseDocument<>)] = ResourceIdentifierTopLinkTypes, |
| 39 | + [typeof(ResourceIdentifierCollectionResponseDocument<>)] = ResourceIdentifierCollectionTopLinkTypes, |
| 40 | + [typeof(ErrorResponseDocument)] = ErrorTopLinkTypes, |
| 41 | + [typeof(NullableToOneRelationshipInResponse<>)] = RelationshipLinkTypes, |
| 42 | + [typeof(ToManyRelationshipInResponse<>)] = RelationshipLinkTypes, |
| 43 | + [typeof(ToOneRelationshipInResponse<>)] = RelationshipLinkTypes, |
| 44 | + [typeof(ResourceDataInResponse<>)] = ResourceLinkTypes |
| 45 | + }; |
| 46 | + |
| 47 | + private static readonly Dictionary<LinkTypes, List<string>> LinkTypeToPropertyNamesMap = new() |
| 48 | + { |
| 49 | + [LinkTypes.Self] = ["self"], |
| 50 | + [LinkTypes.Related] = ["related"], |
| 51 | + [LinkTypes.DescribedBy] = ["describedby"], |
| 52 | + [LinkTypes.Pagination] = |
| 53 | + [ |
| 54 | + "first", |
| 55 | + "last", |
| 56 | + "prev", |
| 57 | + "next" |
| 58 | + ] |
| 59 | + }; |
| 60 | + |
| 61 | + private readonly Lazy<LinksVisibility> _lazyLinksVisibility; |
| 62 | + |
| 63 | + public LinksVisibilitySchemaGenerator(IResourceGraph resourceGraph, IJsonApiOptions options) |
| 64 | + { |
| 65 | + ArgumentGuard.NotNull(resourceGraph); |
| 66 | + ArgumentGuard.NotNull(options); |
| 67 | + |
| 68 | + _lazyLinksVisibility = new Lazy<LinksVisibility>(() => new LinksVisibility(resourceGraph, options), LazyThreadSafetyMode.ExecutionAndPublication); |
| 69 | + } |
| 70 | + |
| 71 | + public void UpdateSchemaForTopLevel(Type modelType, OpenApiSchema fullSchemaForLinksContainer, SchemaRepository schemaRepository) |
| 72 | + { |
| 73 | + ArgumentGuard.NotNull(modelType); |
| 74 | + ArgumentGuard.NotNull(fullSchemaForLinksContainer); |
| 75 | + |
| 76 | + Type lookupType = modelType.IsConstructedGenericType ? modelType.GetGenericTypeDefinition() : modelType; |
| 77 | + |
| 78 | + if (LinksInJsonApiComponentTypes.TryGetValue(lookupType, out LinkTypes possibleLinkTypes)) |
| 79 | + { |
| 80 | + UpdateLinksProperty(fullSchemaForLinksContainer, _lazyLinksVisibility.Value.TopLevelLinks, possibleLinkTypes, schemaRepository); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public void UpdateSchemaForResource(ResourceTypeInfo resourceTypeInfo, OpenApiSchema fullSchemaForResourceData, SchemaRepository schemaRepository) |
| 85 | + { |
| 86 | + ArgumentGuard.NotNull(resourceTypeInfo); |
| 87 | + ArgumentGuard.NotNull(fullSchemaForResourceData); |
| 88 | + |
| 89 | + if (LinksInJsonApiComponentTypes.TryGetValue(resourceTypeInfo.ResourceDataOpenType, out LinkTypes possibleLinkTypes)) |
| 90 | + { |
| 91 | + UpdateLinksProperty(fullSchemaForResourceData, _lazyLinksVisibility.Value.ResourceLinks, possibleLinkTypes, schemaRepository); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public void UpdateSchemaForRelationship(Type modelType, OpenApiSchema fullSchemaForRelationship, SchemaRepository schemaRepository) |
| 96 | + { |
| 97 | + ArgumentGuard.NotNull(modelType); |
| 98 | + ArgumentGuard.NotNull(fullSchemaForRelationship); |
| 99 | + |
| 100 | + Type lookupType = modelType.GetGenericTypeDefinition(); |
| 101 | + |
| 102 | + if (LinksInJsonApiComponentTypes.TryGetValue(lookupType, out LinkTypes possibleLinkTypes)) |
| 103 | + { |
| 104 | + UpdateLinksProperty(fullSchemaForRelationship, _lazyLinksVisibility.Value.RelationshipLinks, possibleLinkTypes, schemaRepository); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void UpdateLinksProperty(OpenApiSchema fullSchemaForLinksContainer, LinkTypes visibleLinkTypes, LinkTypes possibleLinkTypes, |
| 109 | + SchemaRepository schemaRepository) |
| 110 | + { |
| 111 | + if ((visibleLinkTypes & possibleLinkTypes) == 0) |
| 112 | + { |
| 113 | + fullSchemaForLinksContainer.Required.Remove(JsonApiPropertyName.Links); |
| 114 | + fullSchemaForLinksContainer.Properties.Remove(JsonApiPropertyName.Links); |
| 115 | + } |
| 116 | + else if (visibleLinkTypes != possibleLinkTypes) |
| 117 | + { |
| 118 | + OpenApiSchema referenceSchemaForLinks = fullSchemaForLinksContainer.Properties[JsonApiPropertyName.Links]; |
| 119 | + string linksSchemaId = referenceSchemaForLinks.AllOf[0].Reference.Id; |
| 120 | + |
| 121 | + if (schemaRepository.Schemas.TryGetValue(linksSchemaId, out OpenApiSchema? fullSchemaForLinks)) |
| 122 | + { |
| 123 | + UpdateLinkProperties(fullSchemaForLinks, visibleLinkTypes); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void UpdateLinkProperties(OpenApiSchema fullSchemaForLinks, LinkTypes availableLinkTypes) |
| 129 | + { |
| 130 | + foreach (string propertyName in LinkTypeToPropertyNamesMap.Where(pair => !availableLinkTypes.HasFlag(pair.Key)).SelectMany(pair => pair.Value)) |
| 131 | + { |
| 132 | + fullSchemaForLinks.Required.Remove(propertyName); |
| 133 | + fullSchemaForLinks.Properties.Remove(propertyName); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private sealed class LinksVisibility |
| 138 | + { |
| 139 | + public LinkTypes TopLevelLinks { get; } |
| 140 | + public LinkTypes ResourceLinks { get; } |
| 141 | + public LinkTypes RelationshipLinks { get; } |
| 142 | + |
| 143 | + public LinksVisibility(IResourceGraph resourceGraph, IJsonApiOptions options) |
| 144 | + { |
| 145 | + ArgumentGuard.NotNull(resourceGraph); |
| 146 | + ArgumentGuard.NotNull(options); |
| 147 | + |
| 148 | + var unionTopLevelLinks = LinkTypes.None; |
| 149 | + var unionResourceLinks = LinkTypes.None; |
| 150 | + var unionRelationshipLinks = LinkTypes.None; |
| 151 | + |
| 152 | + foreach (ResourceType resourceType in resourceGraph.GetResourceTypes()) |
| 153 | + { |
| 154 | + LinkTypes topLevelLinks = GetTopLevelLinks(resourceType, options); |
| 155 | + unionTopLevelLinks |= topLevelLinks; |
| 156 | + |
| 157 | + LinkTypes resourceLinks = GetResourceLinks(resourceType, options); |
| 158 | + unionResourceLinks |= resourceLinks; |
| 159 | + |
| 160 | + LinkTypes relationshipLinks = GetRelationshipLinks(resourceType, options); |
| 161 | + unionRelationshipLinks |= relationshipLinks; |
| 162 | + } |
| 163 | + |
| 164 | + TopLevelLinks = Normalize(unionTopLevelLinks); |
| 165 | + ResourceLinks = Normalize(unionResourceLinks); |
| 166 | + RelationshipLinks = Normalize(unionRelationshipLinks); |
| 167 | + } |
| 168 | + |
| 169 | + private LinkTypes GetTopLevelLinks(ResourceType resourceType, IJsonApiOptions options) |
| 170 | + { |
| 171 | + return resourceType.TopLevelLinks != LinkTypes.NotConfigured ? resourceType.TopLevelLinks : |
| 172 | + options.TopLevelLinks == LinkTypes.NotConfigured ? LinkTypes.None : options.TopLevelLinks; |
| 173 | + } |
| 174 | + |
| 175 | + private LinkTypes GetResourceLinks(ResourceType resourceType, IJsonApiOptions options) |
| 176 | + { |
| 177 | + return resourceType.ResourceLinks != LinkTypes.NotConfigured ? resourceType.ResourceLinks : |
| 178 | + options.ResourceLinks == LinkTypes.NotConfigured ? LinkTypes.None : options.ResourceLinks; |
| 179 | + } |
| 180 | + |
| 181 | + private LinkTypes GetRelationshipLinks(ResourceType resourceType, IJsonApiOptions options) |
| 182 | + { |
| 183 | + LinkTypes unionRelationshipLinks = resourceType.RelationshipLinks != LinkTypes.NotConfigured ? resourceType.RelationshipLinks : |
| 184 | + options.RelationshipLinks == LinkTypes.NotConfigured ? LinkTypes.None : options.RelationshipLinks; |
| 185 | + |
| 186 | + foreach (RelationshipAttribute relationship in resourceType.Relationships) |
| 187 | + { |
| 188 | + LinkTypes relationshipLinks = relationship.Links != LinkTypes.NotConfigured ? relationship.Links : |
| 189 | + relationship.LeftType.RelationshipLinks != LinkTypes.NotConfigured ? relationship.LeftType.RelationshipLinks : |
| 190 | + options.RelationshipLinks == LinkTypes.NotConfigured ? LinkTypes.None : options.RelationshipLinks; |
| 191 | + |
| 192 | + unionRelationshipLinks |= relationshipLinks; |
| 193 | + } |
| 194 | + |
| 195 | + return unionRelationshipLinks; |
| 196 | + } |
| 197 | + |
| 198 | + private static LinkTypes Normalize(LinkTypes linkTypes) |
| 199 | + { |
| 200 | + return linkTypes != LinkTypes.None ? linkTypes & ~LinkTypes.None : linkTypes; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments