-
Notifications
You must be signed in to change notification settings - Fork 19
/
SwaggerEnumDescriptions.cs
46 lines (38 loc) · 1.25 KB
/
SwaggerEnumDescriptions.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
using System;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Passingwind.WorkflowApp.Web;
public class SwaggerEnumDescriptions : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
var type = context.Type;
if (type.IsEnum)
{
var names = Enum.GetNames(type);
var values2 = new OpenApiArray();
values2.AddRange(names.Select(x => new OpenApiObject
{
["name"] = new OpenApiString(Convert.ToInt32(Enum.Parse(type, x)).ToString()),
["value"] = new OpenApiString(x),
}));
var values1 = new OpenApiArray();
values1.AddRange(names.Select(x => new OpenApiString(x)));
schema.Extensions.Add(
"x-enumNames",
values1
);
schema.Extensions.Add(
"x-ms-enum",
new OpenApiObject
{
["name"] = new OpenApiString(type.Name),
["modelAsString"] = new OpenApiBoolean(true),
["values"] = values2,
}
);
}
}
}