-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwaggerAddEnumDescriptions.cs
244 lines (231 loc) · 9.52 KB
/
SwaggerAddEnumDescriptions.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
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SwaggerAddEnumDescriptions.cs" company="OpenSky">
// OpenSky project 2021-2023
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace OpenSky.API
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Add enum value descriptions to swagger documentation.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <seealso cref="T:Swashbuckle.AspNetCore.SwaggerGen.IDocumentFilter"/>
/// -------------------------------------------------------------------------------------------------
public class SwaggerAddEnumDescriptions : IDocumentFilter
{
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Applies the document filter.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <param name="swaggerDoc">
/// The swagger documentation.
/// </param>
/// <param name="context">
/// The context.
/// </param>
/// -------------------------------------------------------------------------------------------------
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
// add enum descriptions to result models
foreach (var (key, value) in swaggerDoc.Components.Schemas.Where(x => x.Value?.Enum?.Count > 0))
{
var propertyEnums = value.Enum;
if (propertyEnums is { Count: > 0 })
{
value.Description += DescribeEnum(propertyEnums, key);
}
}
// add enum descriptions to input parameters
foreach (var (key, value) in swaggerDoc.Paths)
{
DescribeEnumParameters(value.Operations, swaggerDoc, context.ApiDescriptions, key);
}
}
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Describe enum for documentation.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <param name="enums">
/// The enums.
/// </param>
/// <param name="propertyTypeName">
/// Name of the property type.
/// </param>
/// <returns>
/// A string describing the enum for the documentation.
/// </returns>
/// -------------------------------------------------------------------------------------------------
private static string DescribeEnum(IEnumerable<IOpenApiAny> enums, string propertyTypeName)
{
var enumType = GetEnumTypeByName(propertyTypeName);
if (enumType == null)
return null;
return " " + string.Join(", ", (from OpenApiInteger enumOption in enums select enumOption.Value into enumInt select $"{enumInt} = {Enum.GetName(enumType, enumInt)}").ToArray());
}
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Describe enum parameters.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <param name="operations">
/// The operations.
/// </param>
/// <param name="swaggerDoc">
/// The swagger documentation.
/// </param>
/// <param name="apiDescriptions">
/// The API descriptions.
/// </param>
/// <param name="path">
/// Full pathname of the file.
/// </param>
/// -------------------------------------------------------------------------------------------------
private static void DescribeEnumParameters(IDictionary<OperationType, OpenApiOperation> operations, OpenApiDocument swaggerDoc, IEnumerable<ApiDescription> apiDescriptions, string path)
{
path = path.Trim('/');
if (operations != null)
{
var pathDescriptions = apiDescriptions.Where(a => a.RelativePath == path).ToList();
foreach (var (key, value) in operations)
{
var operationDescription = pathDescriptions.FirstOrDefault(a => a.HttpMethod != null && a.HttpMethod.Equals(key.ToString(), StringComparison.InvariantCultureIgnoreCase));
foreach (var param in value.Parameters)
{
var parameterDescription = operationDescription?.ParameterDescriptions.FirstOrDefault(a => a.Name == param.Name);
if (parameterDescription != null && TryGetEnumType(parameterDescription.Type, out var enumType))
{
var paramEnum = swaggerDoc.Components.Schemas.FirstOrDefault(x => x.Key == enumType.Name);
if (paramEnum.Value != null)
{
param.Description += DescribeEnum(paramEnum.Value.Enum, paramEnum.Key);
}
}
}
}
}
}
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Get enum by type name.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <param name="enumTypeName">
/// Name of the enum type.
/// </param>
/// <returns>
/// The enum type by name.
/// </returns>
/// -------------------------------------------------------------------------------------------------
private static Type GetEnumTypeByName(string enumTypeName)
{
return AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.FirstOrDefault(x => x.Name == enumTypeName);
}
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Gets type of an enum contained in a generic IEnumerable.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <param name="type">
/// The type to check.
/// </param>
/// <returns>
/// The enum type, or null.
/// </returns>
/// -------------------------------------------------------------------------------------------------
private static Type GetTypeIEnumerableType(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
var underlyingType = type.GetGenericArguments()[0];
if (underlyingType.IsEnum)
{
return underlyingType;
}
}
return null;
}
/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Try to get enum value.
/// </summary>
/// <remarks>
/// sushi.at, 03/06/2021.
/// </remarks>
/// <param name="type">
/// The type.
/// </param>
/// <param name="enumType">
/// [out] Type of the enum.
/// </param>
/// <returns>
/// True if it succeeds, false if it fails.
/// </returns>
/// -------------------------------------------------------------------------------------------------
private static bool TryGetEnumType(Type type, out Type enumType)
{
if (type.IsEnum)
{
enumType = type;
return true;
}
else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var underlyingType = Nullable.GetUnderlyingType(type);
if (underlyingType is { IsEnum: true })
{
enumType = underlyingType;
return true;
}
}
else
{
var underlyingType = GetTypeIEnumerableType(type);
if (underlyingType is { IsEnum: true })
{
enumType = underlyingType;
return true;
}
else
{
var interfaces = type.GetInterfaces();
foreach (var interfaceType in interfaces)
{
underlyingType = GetTypeIEnumerableType(interfaceType);
if (underlyingType is { IsEnum: true })
{
enumType = underlyingType;
return true;
}
}
}
}
enumType = null;
return false;
}
}
}