|
1 | 1 | using Microsoft.OpenApi.Models;
|
2 | 2 | using Swashbuckle.AspNetCore.SwaggerGen;
|
3 | 3 |
|
4 |
| -namespace Swashbuckle.AspNetCore.Annotations |
| 4 | +namespace Swashbuckle.AspNetCore.Annotations; |
| 5 | + |
| 6 | +public class AnnotationsOperationFilter : IOperationFilter |
5 | 7 | {
|
6 |
| - public class AnnotationsOperationFilter : IOperationFilter |
| 8 | + public void Apply(OpenApiOperation operation, OperationFilterContext context) |
7 | 9 | {
|
8 |
| - public void Apply(OpenApiOperation operation, OperationFilterContext context) |
9 |
| - { |
10 |
| - IEnumerable<object> controllerAttributes = []; |
11 |
| - IEnumerable<object> actionAttributes = []; |
12 |
| - IEnumerable<object> metadataAttributes = []; |
| 10 | + IEnumerable<object> controllerAttributes = []; |
| 11 | + IEnumerable<object> actionAttributes = []; |
| 12 | + IEnumerable<object> metadataAttributes = []; |
13 | 13 |
|
14 |
| - if (context.MethodInfo != null) |
15 |
| - { |
16 |
| - controllerAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true); |
17 |
| - actionAttributes = context.MethodInfo.GetCustomAttributes(true); |
18 |
| - } |
| 14 | + if (context.MethodInfo != null) |
| 15 | + { |
| 16 | + controllerAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true); |
| 17 | + actionAttributes = context.MethodInfo.GetCustomAttributes(true); |
| 18 | + } |
19 | 19 |
|
20 |
| -#if NET6_0_OR_GREATER |
21 |
| - if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata != null) |
22 |
| - { |
23 |
| - metadataAttributes = context.ApiDescription.ActionDescriptor.EndpointMetadata; |
24 |
| - } |
| 20 | +#if NET |
| 21 | + if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata != null) |
| 22 | + { |
| 23 | + metadataAttributes = context.ApiDescription.ActionDescriptor.EndpointMetadata; |
| 24 | + } |
25 | 25 | #endif
|
26 | 26 |
|
27 |
| - // NOTE: When controller and action attributes are applicable, action attributes should take priority. |
28 |
| - // Hence, why they're at the end of the list (i.e. last one wins). |
29 |
| - // Distinct() is applied due to an ASP.NET Core issue: https://github.com/dotnet/aspnetcore/issues/34199. |
30 |
| - var allAttributes = controllerAttributes |
31 |
| - .Union(actionAttributes) |
32 |
| - .Union(metadataAttributes) |
33 |
| - .Distinct(); |
34 |
| - |
35 |
| - var actionAndEndpointAttributes = actionAttributes |
36 |
| - .Union(metadataAttributes) |
37 |
| - .Distinct(); |
38 |
| - |
39 |
| - ApplySwaggerOperationAttribute(operation, actionAndEndpointAttributes); |
40 |
| - ApplySwaggerOperationFilterAttributes(operation, context, allAttributes); |
41 |
| - ApplySwaggerResponseAttributes(operation, context, allAttributes); |
42 |
| - } |
| 27 | + // NOTE: When controller and action attributes are applicable, action attributes should take priority. |
| 28 | + // Hence, why they're at the end of the list (i.e. last one wins). |
| 29 | + // Distinct() is applied due to an ASP.NET Core issue: https://github.com/dotnet/aspnetcore/issues/34199. |
| 30 | + var allAttributes = controllerAttributes |
| 31 | + .Union(actionAttributes) |
| 32 | + .Union(metadataAttributes) |
| 33 | + .Distinct(); |
| 34 | + |
| 35 | + var actionAndEndpointAttributes = actionAttributes |
| 36 | + .Union(metadataAttributes) |
| 37 | + .Distinct(); |
| 38 | + |
| 39 | + ApplySwaggerOperationAttribute(operation, actionAndEndpointAttributes); |
| 40 | + ApplySwaggerOperationFilterAttributes(operation, context, allAttributes); |
| 41 | + ApplySwaggerResponseAttributes(operation, context, allAttributes); |
| 42 | + } |
43 | 43 |
|
44 |
| - private static void ApplySwaggerOperationAttribute( |
45 |
| - OpenApiOperation operation, |
46 |
| - IEnumerable<object> actionAttributes) |
47 |
| - { |
48 |
| - var swaggerOperationAttribute = actionAttributes |
49 |
| - .OfType<SwaggerOperationAttribute>() |
50 |
| - .FirstOrDefault(); |
| 44 | + private static void ApplySwaggerOperationAttribute( |
| 45 | + OpenApiOperation operation, |
| 46 | + IEnumerable<object> actionAttributes) |
| 47 | + { |
| 48 | + var swaggerOperationAttribute = actionAttributes |
| 49 | + .OfType<SwaggerOperationAttribute>() |
| 50 | + .FirstOrDefault(); |
51 | 51 |
|
52 |
| - if (swaggerOperationAttribute == null) return; |
| 52 | + if (swaggerOperationAttribute == null) return; |
53 | 53 |
|
54 |
| - if (swaggerOperationAttribute.Summary != null) |
55 |
| - operation.Summary = swaggerOperationAttribute.Summary; |
| 54 | + if (swaggerOperationAttribute.Summary != null) |
| 55 | + operation.Summary = swaggerOperationAttribute.Summary; |
56 | 56 |
|
57 |
| - if (swaggerOperationAttribute.Description != null) |
58 |
| - operation.Description = swaggerOperationAttribute.Description; |
| 57 | + if (swaggerOperationAttribute.Description != null) |
| 58 | + operation.Description = swaggerOperationAttribute.Description; |
59 | 59 |
|
60 |
| - if (swaggerOperationAttribute.OperationId != null) |
61 |
| - operation.OperationId = swaggerOperationAttribute.OperationId; |
| 60 | + if (swaggerOperationAttribute.OperationId != null) |
| 61 | + operation.OperationId = swaggerOperationAttribute.OperationId; |
62 | 62 |
|
63 |
| - if (swaggerOperationAttribute.Tags != null) |
64 |
| - { |
65 |
| - operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })]; |
66 |
| - } |
| 63 | + if (swaggerOperationAttribute.Tags != null) |
| 64 | + { |
| 65 | + operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })]; |
67 | 66 | }
|
| 67 | + } |
68 | 68 |
|
69 |
| - public static void ApplySwaggerOperationFilterAttributes( |
70 |
| - OpenApiOperation operation, |
71 |
| - OperationFilterContext context, |
72 |
| - IEnumerable<object> controllerAndActionAttributes) |
73 |
| - { |
74 |
| - var swaggerOperationFilterAttributes = controllerAndActionAttributes |
75 |
| - .OfType<SwaggerOperationFilterAttribute>(); |
| 69 | + public static void ApplySwaggerOperationFilterAttributes( |
| 70 | + OpenApiOperation operation, |
| 71 | + OperationFilterContext context, |
| 72 | + IEnumerable<object> controllerAndActionAttributes) |
| 73 | + { |
| 74 | + var swaggerOperationFilterAttributes = controllerAndActionAttributes |
| 75 | + .OfType<SwaggerOperationFilterAttribute>(); |
76 | 76 |
|
77 |
| - foreach (var swaggerOperationFilterAttribute in swaggerOperationFilterAttributes) |
78 |
| - { |
79 |
| - var filter = (IOperationFilter)Activator.CreateInstance(swaggerOperationFilterAttribute.FilterType); |
80 |
| - filter.Apply(operation, context); |
81 |
| - } |
| 77 | + foreach (var swaggerOperationFilterAttribute in swaggerOperationFilterAttributes) |
| 78 | + { |
| 79 | + var filter = (IOperationFilter)Activator.CreateInstance(swaggerOperationFilterAttribute.FilterType); |
| 80 | + filter.Apply(operation, context); |
82 | 81 | }
|
| 82 | + } |
83 | 83 |
|
84 |
| - private static void ApplySwaggerResponseAttributes( |
85 |
| - OpenApiOperation operation, |
86 |
| - OperationFilterContext context, |
87 |
| - IEnumerable<object> controllerAndActionAttributes) |
| 84 | + private static void ApplySwaggerResponseAttributes( |
| 85 | + OpenApiOperation operation, |
| 86 | + OperationFilterContext context, |
| 87 | + IEnumerable<object> controllerAndActionAttributes) |
| 88 | + { |
| 89 | + var swaggerResponseAttributes = controllerAndActionAttributes.OfType<SwaggerResponseAttribute>(); |
| 90 | + |
| 91 | + foreach (var swaggerResponseAttribute in swaggerResponseAttributes) |
88 | 92 | {
|
89 |
| - var swaggerResponseAttributes = controllerAndActionAttributes.OfType<SwaggerResponseAttribute>(); |
| 93 | + var statusCode = swaggerResponseAttribute.StatusCode.ToString(); |
90 | 94 |
|
91 |
| - foreach (var swaggerResponseAttribute in swaggerResponseAttributes) |
92 |
| - { |
93 |
| - var statusCode = swaggerResponseAttribute.StatusCode.ToString(); |
| 95 | + operation.Responses ??= []; |
94 | 96 |
|
95 |
| - operation.Responses ??= []; |
| 97 | + if (!operation.Responses.TryGetValue(statusCode, out OpenApiResponse response)) |
| 98 | + { |
| 99 | + response = new OpenApiResponse(); |
| 100 | + } |
96 | 101 |
|
97 |
| - if (!operation.Responses.TryGetValue(statusCode, out OpenApiResponse response)) |
98 |
| - { |
99 |
| - response = new OpenApiResponse(); |
100 |
| - } |
| 102 | + if (swaggerResponseAttribute.Description != null) |
| 103 | + { |
| 104 | + response.Description = swaggerResponseAttribute.Description; |
| 105 | + } |
101 | 106 |
|
102 |
| - if (swaggerResponseAttribute.Description != null) |
103 |
| - { |
104 |
| - response.Description = swaggerResponseAttribute.Description; |
105 |
| - } |
| 107 | + operation.Responses[statusCode] = response; |
106 | 108 |
|
107 |
| - operation.Responses[statusCode] = response; |
| 109 | + if (swaggerResponseAttribute.ContentTypes != null) |
| 110 | + { |
| 111 | + response.Content.Clear(); |
108 | 112 |
|
109 |
| - if (swaggerResponseAttribute.ContentTypes != null) |
| 113 | + foreach (var contentType in swaggerResponseAttribute.ContentTypes) |
110 | 114 | {
|
111 |
| - response.Content.Clear(); |
112 |
| - |
113 |
| - foreach (var contentType in swaggerResponseAttribute.ContentTypes) |
114 |
| - { |
115 |
| - var schema = (swaggerResponseAttribute.Type != null && swaggerResponseAttribute.Type != typeof(void)) |
116 |
| - ? context.SchemaGenerator.GenerateSchema(swaggerResponseAttribute.Type, context.SchemaRepository) |
117 |
| - : null; |
| 115 | + var schema = (swaggerResponseAttribute.Type != null && swaggerResponseAttribute.Type != typeof(void)) |
| 116 | + ? context.SchemaGenerator.GenerateSchema(swaggerResponseAttribute.Type, context.SchemaRepository) |
| 117 | + : null; |
118 | 118 |
|
119 |
| - response.Content.Add(contentType, new OpenApiMediaType { Schema = schema }); |
120 |
| - } |
| 119 | + response.Content.Add(contentType, new OpenApiMediaType { Schema = schema }); |
121 | 120 | }
|
122 | 121 | }
|
123 | 122 | }
|
|
0 commit comments