-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathOpenApiEndpointConvention.cs
More file actions
348 lines (304 loc) · 12.8 KB
/
OpenApiEndpointConvention.cs
File metadata and controls
348 lines (304 loc) · 12.8 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System.Net;
using System.Reflection;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.Documents;
using JsonApiDotNetCore.Resources.Annotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
/// <summary>
/// Sets metadata on controllers for OpenAPI documentation generation by Swagger. Only targets JsonApiDotNetCore controllers.
/// </summary>
internal sealed class OpenApiEndpointConvention : IActionModelConvention
{
private readonly IControllerResourceMapping _controllerResourceMapping;
private readonly IJsonApiOptions _options;
public OpenApiEndpointConvention(IControllerResourceMapping controllerResourceMapping, IJsonApiOptions options)
{
ArgumentNullException.ThrowIfNull(controllerResourceMapping);
ArgumentNullException.ThrowIfNull(options);
_controllerResourceMapping = controllerResourceMapping;
_options = options;
}
public void Apply(ActionModel action)
{
ArgumentNullException.ThrowIfNull(action);
JsonApiEndpointWrapper endpoint = JsonApiEndpointWrapper.FromActionModel(action);
if (endpoint.IsUnknown)
{
// Not a JSON:API controller, or a non-standard action method in a JSON:API controller.
// None of these are yet implemented, so hide them to avoid downstream crashes.
action.ApiExplorer.IsVisible = false;
return;
}
ResourceType? resourceType = _controllerResourceMapping.GetResourceTypeForController(action.Controller.ControllerType);
if (ShouldSuppressEndpoint(endpoint, resourceType))
{
action.ApiExplorer.IsVisible = false;
return;
}
SetResponseMetadata(action, endpoint, resourceType);
SetRequestMetadata(action, endpoint);
}
private bool ShouldSuppressEndpoint(JsonApiEndpointWrapper endpoint, ResourceType? resourceType)
{
if (resourceType == null)
{
return false;
}
if (!IsEndpointAvailable(endpoint.Value, resourceType))
{
return true;
}
if (IsSecondaryOrRelationshipEndpoint(endpoint.Value))
{
if (resourceType.Relationships.Count == 0)
{
return true;
}
if (endpoint.Value is JsonApiEndpoints.DeleteRelationship or JsonApiEndpoints.PostRelationship)
{
return !resourceType.Relationships.OfType<HasManyAttribute>().Any();
}
}
return false;
}
private static bool IsEndpointAvailable(JsonApiEndpoints endpoint, ResourceType resourceType)
{
JsonApiEndpoints availableEndpoints = GetGeneratedControllerEndpoints(resourceType);
if (availableEndpoints == JsonApiEndpoints.None)
{
// Auto-generated controllers are disabled, so we can't know what to hide.
// It is assumed that a handwritten JSON:API controller only provides action methods for what it supports.
// To accomplish that, derive from BaseJsonApiController instead of JsonApiController.
return true;
}
// For an overridden JSON:API action method in a partial class to show up, it's flag must be turned on in [Resource].
// Otherwise, it is considered to be an action method that throws because the endpoint is unavailable.
return IncludesEndpoint(endpoint, availableEndpoints);
}
private static bool IncludesEndpoint(JsonApiEndpoints endpoint, JsonApiEndpoints availableEndpoints)
{
bool? isIncluded = null;
if (endpoint == JsonApiEndpoints.GetCollection)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetCollection);
}
else if (endpoint == JsonApiEndpoints.GetSingle)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetSingle);
}
else if (endpoint == JsonApiEndpoints.GetSecondary)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetSecondary);
}
else if (endpoint == JsonApiEndpoints.GetRelationship)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.GetRelationship);
}
else if (endpoint == JsonApiEndpoints.Post)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.Post);
}
else if (endpoint == JsonApiEndpoints.PostRelationship)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.PostRelationship);
}
else if (endpoint == JsonApiEndpoints.Patch)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.Patch);
}
else if (endpoint == JsonApiEndpoints.PatchRelationship)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.PatchRelationship);
}
else if (endpoint == JsonApiEndpoints.Delete)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.Delete);
}
else if (endpoint == JsonApiEndpoints.DeleteRelationship)
{
isIncluded = availableEndpoints.HasFlag(JsonApiEndpoints.DeleteRelationship);
}
ConsistencyGuard.ThrowIf(isIncluded == null);
return isIncluded.Value;
}
private static JsonApiEndpoints GetGeneratedControllerEndpoints(ResourceType resourceType)
{
var resourceAttribute = resourceType.ClrType.GetCustomAttribute<ResourceAttribute>();
return resourceAttribute?.GenerateControllerEndpoints ?? JsonApiEndpoints.None;
}
private static bool IsSecondaryOrRelationshipEndpoint(JsonApiEndpoints endpoint)
{
return endpoint is JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship or JsonApiEndpoints.PostRelationship or
JsonApiEndpoints.PatchRelationship or JsonApiEndpoints.DeleteRelationship;
}
private void SetResponseMetadata(ActionModel action, JsonApiEndpointWrapper endpoint, ResourceType? resourceType)
{
JsonApiMediaType mediaType = GetMediaTypeForEndpoint(endpoint);
action.Filters.Add(new ProducesAttribute(mediaType.ToString()));
foreach (HttpStatusCode statusCode in GetSuccessStatusCodesForEndpoint(endpoint))
{
// The return type is set later by JsonApiActionDescriptorCollectionProvider.
action.Filters.Add(new ProducesResponseTypeAttribute((int)statusCode));
}
foreach (HttpStatusCode statusCode in GetErrorStatusCodesForEndpoint(endpoint, resourceType))
{
action.Filters.Add(new ProducesResponseTypeAttribute(typeof(ErrorResponseDocument), (int)statusCode));
}
}
private JsonApiMediaType GetMediaTypeForEndpoint(JsonApiEndpointWrapper endpoint)
{
return endpoint.IsAtomicOperationsEndpoint ? OpenApiMediaTypes.RelaxedAtomicOperationsWithRelaxedOpenApi : OpenApiMediaTypes.RelaxedOpenApi;
}
private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpointWrapper endpoint)
{
if (endpoint.IsAtomicOperationsEndpoint)
{
return
[
HttpStatusCode.OK,
HttpStatusCode.NoContent
];
}
HttpStatusCode[]? statusCodes = null;
if (endpoint.Value is JsonApiEndpoints.GetCollection or JsonApiEndpoints.GetSingle or JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship)
{
statusCodes =
[
HttpStatusCode.OK,
HttpStatusCode.NotModified
];
}
else if (endpoint.Value == JsonApiEndpoints.Post)
{
statusCodes =
[
HttpStatusCode.Created,
HttpStatusCode.NoContent
];
}
else if (endpoint.Value == JsonApiEndpoints.Patch)
{
statusCodes =
[
HttpStatusCode.OK,
HttpStatusCode.NoContent
];
}
else if (endpoint.Value is JsonApiEndpoints.Delete or JsonApiEndpoints.PostRelationship or JsonApiEndpoints.PatchRelationship or
JsonApiEndpoints.DeleteRelationship)
{
statusCodes = [HttpStatusCode.NoContent];
}
ConsistencyGuard.ThrowIf(statusCodes == null);
return statusCodes;
}
private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpointWrapper endpoint, ResourceType? resourceType)
{
if (endpoint.IsAtomicOperationsEndpoint)
{
return
[
HttpStatusCode.BadRequest,
HttpStatusCode.Forbidden,
HttpStatusCode.NotFound,
HttpStatusCode.Conflict,
HttpStatusCode.UnprocessableEntity
];
}
// Condition doesn't apply to atomic operations, because Forbidden is also used when an operation is not accessible.
ClientIdGenerationMode clientIdGeneration = resourceType?.ClientIdGeneration ?? _options.ClientIdGeneration;
HttpStatusCode[]? statusCodes = null;
if (endpoint.Value == JsonApiEndpoints.GetCollection)
{
statusCodes = [HttpStatusCode.BadRequest];
}
else if (endpoint.Value is JsonApiEndpoints.GetSingle or JsonApiEndpoints.GetSecondary or JsonApiEndpoints.GetRelationship)
{
statusCodes =
[
HttpStatusCode.BadRequest,
HttpStatusCode.NotFound
];
}
else if (endpoint.Value == JsonApiEndpoints.Post && clientIdGeneration == ClientIdGenerationMode.Forbidden)
{
statusCodes =
[
HttpStatusCode.BadRequest,
HttpStatusCode.Forbidden,
HttpStatusCode.NotFound,
HttpStatusCode.Conflict,
HttpStatusCode.UnprocessableEntity
];
}
else if (endpoint.Value is JsonApiEndpoints.Post or JsonApiEndpoints.Patch)
{
statusCodes =
[
HttpStatusCode.BadRequest,
HttpStatusCode.NotFound,
HttpStatusCode.Conflict,
HttpStatusCode.UnprocessableEntity
];
}
else if (endpoint.Value == JsonApiEndpoints.Delete)
{
statusCodes = [HttpStatusCode.NotFound];
}
else if (endpoint.Value is JsonApiEndpoints.PostRelationship or JsonApiEndpoints.PatchRelationship or JsonApiEndpoints.DeleteRelationship)
{
statusCodes =
[
HttpStatusCode.BadRequest,
HttpStatusCode.NotFound,
HttpStatusCode.Conflict,
HttpStatusCode.UnprocessableEntity
];
}
ConsistencyGuard.ThrowIf(statusCodes == null);
return statusCodes;
}
private void SetRequestMetadata(ActionModel action, JsonApiEndpointWrapper endpoint)
{
if (RequiresRequestBody(endpoint))
{
JsonApiMediaType mediaType = GetMediaTypeForEndpoint(endpoint);
action.Filters.Add(new ConsumesAttribute(mediaType.ToString()));
}
}
private static bool RequiresRequestBody(JsonApiEndpointWrapper endpoint)
{
return endpoint.IsAtomicOperationsEndpoint || endpoint.Value is JsonApiEndpoints.Post or JsonApiEndpoints.Patch or JsonApiEndpoints.PostRelationship or
JsonApiEndpoints.PatchRelationship or JsonApiEndpoints.DeleteRelationship;
}
private sealed class JsonApiEndpointWrapper
{
private static readonly JsonApiEndpointWrapper AtomicOperations = new(true, JsonApiEndpoints.None);
public bool IsAtomicOperationsEndpoint { get; }
public JsonApiEndpoints Value { get; }
public bool IsUnknown => !IsAtomicOperationsEndpoint && Value == JsonApiEndpoints.None;
private JsonApiEndpointWrapper(bool isAtomicOperationsEndpoint, JsonApiEndpoints value)
{
IsAtomicOperationsEndpoint = isAtomicOperationsEndpoint;
Value = value;
}
public static JsonApiEndpointWrapper FromActionModel(ActionModel actionModel)
{
if (EndpointResolver.Instance.IsAtomicOperationsController(actionModel.ActionMethod))
{
return AtomicOperations;
}
JsonApiEndpoints endpoint = EndpointResolver.Instance.GetEndpoint(actionModel.ActionMethod);
return new JsonApiEndpointWrapper(false, endpoint);
}
public override string ToString()
{
return IsAtomicOperationsEndpoint ? "PostOperations" : Value.ToString();
}
}
}