-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathConfigureSwaggerGenOptions.cs
153 lines (127 loc) · 5.85 KB
/
ConfigureSwaggerGenOptions.cs
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
using System.Reflection;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
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 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<SortSchemasFilter>();
}
private List<Type> SelectDerivedTypes(Type baseType)
{
if (BaseToDerivedSchemaTypes.TryGetValue(baseType, out var schemaOpenType))
{
return GetConstructedTypesFromResourceGraph(schemaOpenType);
}
if (baseType == typeof(AtomicOperation))
{
return GetConstructedTypesForAtomicOperation();
}
if (baseType.IsAssignableTo(typeof(IIdentifiable)))
{
var resourceType = _resourceGraph.FindResourceType(baseType);
if (resourceType != null && resourceType.IsPartOfTypeHierarchy())
{
return GetResourceDerivedTypes(resourceType);
}
}
return [];
}
private List<Type> GetConstructedTypesFromResourceGraph(Type schemaOpenType)
{
List<Type> constructedTypes = [];
foreach (var resourceType in _resourceGraph.GetResourceTypes())
{
var constructedType = schemaOpenType.MakeGenericType(resourceType.ClrType);
constructedTypes.Add(constructedType);
}
return constructedTypes;
}
private List<Type> GetConstructedTypesForAtomicOperation()
{
List<Type> derivedTypes = [];
foreach (var 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 (var derivedType in baseType.DirectlyDerivedTypes)
{
clrTypes.Add(derivedType.ClrType);
IncludeDerivedTypes(derivedType, clrTypes);
}
}
private static List<string> GetOpenApiOperationTags(ApiDescription description, IControllerResourceMapping controllerResourceMapping)
{
var actionMethod = description.ActionDescriptor.GetActionMethod();
var resourceType = controllerResourceMapping.GetResourceTypeForController(actionMethod.ReflectedType);
return resourceType == null ? ["operations"] : [resourceType.PublicName];
}
}