-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathConfigureSwaggerGenOptions.cs
More file actions
171 lines (143 loc) · 6.49 KB
/
ConfigureSwaggerGenOptions.cs
File metadata and controls
171 lines (143 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata.ActionMethods;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.AtomicOperations;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
using JsonApiDotNetCore.OpenApi.Swashbuckle.SwaggerComponents;
using JsonApiDotNetCore.Resources;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
internal sealed class ConfigureSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
{
private static readonly Dictionary<Type, Type> BaseToDerivedSchemaTypes = new()
{
[typeof(IdentifierInRequest)] = typeof(IdentifierInRequest<>),
[typeof(ResourceInCreateRequest)] = typeof(DataInCreateRequest<>),
[typeof(ResourceInUpdateRequest)] = typeof(DataInUpdateRequest<>),
[typeof(ResourceInResponse)] = typeof(DataInResponse<>)
};
private static readonly Type[] AtomicOperationDerivedSchemaTypes =
[
typeof(CreateOperation<>),
typeof(UpdateOperation<>),
typeof(DeleteOperation<>),
typeof(UpdateToOneRelationshipOperation<>),
typeof(UpdateToManyRelationshipOperation<>),
typeof(AddToRelationshipOperation<>),
typeof(RemoveFromRelationshipOperation<>)
];
private static readonly Func<ApiDescription, IList<string>> DefaultTagsSelector = new SwaggerGeneratorOptions().TagsSelector;
private readonly OpenApiOperationIdSelector _operationIdSelector;
private readonly JsonApiSchemaIdSelector _schemaIdSelector;
private readonly IControllerResourceMapping _controllerResourceMapping;
private readonly IResourceGraph _resourceGraph;
public ConfigureSwaggerGenOptions(OpenApiOperationIdSelector operationIdSelector, JsonApiSchemaIdSelector schemaIdSelector,
IControllerResourceMapping controllerResourceMapping, IResourceGraph resourceGraph)
{
ArgumentNullException.ThrowIfNull(operationIdSelector);
ArgumentNullException.ThrowIfNull(schemaIdSelector);
ArgumentNullException.ThrowIfNull(controllerResourceMapping);
ArgumentNullException.ThrowIfNull(resourceGraph);
_operationIdSelector = operationIdSelector;
_schemaIdSelector = schemaIdSelector;
_controllerResourceMapping = controllerResourceMapping;
_resourceGraph = resourceGraph;
}
public void Configure(SwaggerGenOptions options)
{
ArgumentNullException.ThrowIfNull(options);
options.SupportNonNullableReferenceTypes();
options.UseAllOfToExtendReferenceSchemas();
options.UseAllOfForInheritance();
options.SelectDiscriminatorNameUsing(_ => JsonApiPropertyName.Type);
options.SelectDiscriminatorValueUsing(clrType => _resourceGraph.GetResourceType(clrType).PublicName);
options.SelectSubTypesUsing(SelectDerivedTypes);
options.TagActionsBy(description => GetOpenApiOperationTags(description, _controllerResourceMapping));
options.CustomOperationIds(_operationIdSelector.GetOpenApiOperationId);
options.CustomSchemaIds(_schemaIdSelector.GetSchemaId);
options.OperationFilter<DocumentationOpenApiOperationFilter>();
options.DocumentFilter<ServerDocumentFilter>();
options.DocumentFilter<EndpointOrderingFilter>();
options.DocumentFilter<StringEnumOrderingFilter>();
options.DocumentFilter<SetSchemaTypeToObjectDocumentFilter>();
options.DocumentFilter<UnusedComponentSchemaCleaner>();
options.DocumentFilter<RemoveTagsFilter>();
}
private List<Type> SelectDerivedTypes(Type baseType)
{
if (BaseToDerivedSchemaTypes.TryGetValue(baseType, out Type? schemaOpenType))
{
return GetConstructedTypesFromResourceGraph(schemaOpenType);
}
if (baseType == typeof(AtomicOperation))
{
return GetConstructedTypesForAtomicOperation();
}
if (baseType.IsAssignableTo(typeof(IIdentifiable)))
{
ResourceType? resourceType = _resourceGraph.FindResourceType(baseType);
if (resourceType != null && resourceType.IsPartOfTypeHierarchy())
{
return GetResourceDerivedTypes(resourceType);
}
}
return [];
}
private List<Type> GetConstructedTypesFromResourceGraph(Type schemaOpenType)
{
List<Type> constructedTypes = [];
foreach (ResourceType resourceType in _resourceGraph.GetResourceTypes())
{
Type constructedType = schemaOpenType.MakeGenericType(resourceType.ClrType);
constructedTypes.Add(constructedType);
}
return constructedTypes;
}
private List<Type> GetConstructedTypesForAtomicOperation()
{
List<Type> derivedTypes = [];
foreach (ResourceType resourceType in _resourceGraph.GetResourceTypes())
{
derivedTypes.AddRange(AtomicOperationDerivedSchemaTypes.Select(openType => openType.MakeGenericType(resourceType.ClrType)));
}
return derivedTypes;
}
private static List<Type> GetResourceDerivedTypes(ResourceType baseType)
{
List<Type> clrTypes = [];
IncludeDerivedTypes(baseType, clrTypes);
return clrTypes;
}
private static void IncludeDerivedTypes(ResourceType baseType, List<Type> clrTypes)
{
foreach (ResourceType derivedType in baseType.DirectlyDerivedTypes)
{
clrTypes.Add(derivedType.ClrType);
IncludeDerivedTypes(derivedType, clrTypes);
}
}
private static IList<string> GetOpenApiOperationTags(ApiDescription description, IControllerResourceMapping controllerResourceMapping)
{
var actionMethod = JsonApiActionMethod.TryCreate(description.ActionDescriptor);
switch (actionMethod)
{
case OperationsActionMethod:
{
return ["operations"];
}
case ResourceActionMethod resourceActionMethod:
{
ResourceType? resourceType = controllerResourceMapping.GetResourceTypeForController(resourceActionMethod.ControllerType);
ConsistencyGuard.ThrowIf(resourceType == null);
return [resourceType.PublicName];
}
default:
{
return DefaultTagsSelector(description);
}
}
}
}