Last active
December 3, 2019 22:18
-
-
Save ZeVS777/55e0139f9533976c1ae15d24fb0f32f5 to your computer and use it in GitHub Desktop.
Swashbuckle.AspNetCore 5.0.0-rc support for Unchase.Swashbuckle.AspNetCore.Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DisplayEnumsWithValuesDocumentFilter : IDocumentFilter | |
{ | |
private string AddEnumValuesDescription(OpenApiSchema schema) | |
{ | |
if (schema.Enum.Count == 0) | |
{ | |
return null; | |
} | |
var sb = new StringBuilder(); | |
for (int i = 0; i < schema.Enum.Count; i++) | |
{ | |
var value = ((OpenApiInteger)schema.Enum[i]).Value; | |
var name = ((OpenApiString)((OpenApiArray)schema.Extensions["x-enumNames"])[i]).Value; | |
sb.Append($"<br>{value} = {name}"); | |
} | |
return sb.ToString(); | |
} | |
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | |
{ | |
// add enum descriptions to enumType components | |
foreach (var schemaDictionaryItem in swaggerDoc.Components.Schemas) | |
{ | |
var schema = schemaDictionaryItem.Value; | |
schema.Description += AddEnumValuesDescription(schema); | |
} | |
if (swaggerDoc.Paths.Count <= 0) | |
return; | |
// add enum descriptions to input parameters of every operation | |
foreach (var parameter in swaggerDoc.Paths.Values.SelectMany(v => v.Operations).SelectMany(op => op.Value.Parameters)) | |
{ | |
if (parameter.Schema.Reference == null) | |
continue; | |
var componentReference = parameter.Schema.Reference.Id; | |
var schema = swaggerDoc.Components.Schemas[componentReference]; | |
parameter.Description += AddEnumValuesDescription(schema); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSwaggerGen(options => | |
{ | |
//... Other options | |
options.AddEnumsWithValuesFixFilters(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SwaggerGenOptionsExtensions | |
{ | |
public static void AddEnumsWithValuesFixFilters(this SwaggerGenOptions swaggerGenOptions) | |
{ | |
swaggerGenOptions.SchemaFilter<XEnumNamesSchemaFilter>(); | |
swaggerGenOptions.ParameterFilter<XEnumNamesParameterFilter>(); | |
swaggerGenOptions.DocumentFilter<DisplayEnumsWithValuesDocumentFilter>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XEnumNamesParameterFilter : IParameterFilter | |
{ | |
public void Apply(OpenApiParameter parameter, ParameterFilterContext context) | |
{ | |
var typeInfo = context.ParameterInfo.ParameterType; | |
if (typeInfo.IsEnum) | |
{ | |
var names = Enum.GetNames(context.ParameterInfo.ParameterType).Select(name => new OpenApiString(name)); | |
var arr = new OpenApiArray(); | |
arr.AddRange(names); | |
parameter.Extensions.Add("x-enumNames", arr); | |
} | |
else if (typeInfo.IsGenericType && !parameter.Extensions.ContainsKey("x-enumNames")) | |
{ | |
foreach (var genericArgumentType in typeInfo.GetGenericArguments()) | |
{ | |
if (genericArgumentType.IsEnum && !parameter.Extensions.ContainsKey("x-enumNames")) | |
{ | |
var names = Enum.GetNames(genericArgumentType).Select(name => new OpenApiString(name)); | |
var arr = new OpenApiArray(); | |
arr.AddRange(names); | |
parameter.Extensions.Add("x-enumNames", arr); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XEnumNamesSchemaFilter : ISchemaFilter | |
{ | |
public void Apply(OpenApiSchema schema, SchemaFilterContext context) | |
{ | |
var typeInfo = context.ApiModel.Type; | |
if (typeInfo.IsEnum) | |
{ | |
var names = Enum.GetNames(context.ApiModel.Type).Select(name => new OpenApiString(name)); | |
var arr = new OpenApiArray(); | |
arr.AddRange(names); | |
schema.Extensions.Add("x-enumNames", arr); | |
} | |
else if (schema.Enum != null) | |
{ | |
var names = schema.Enum.Select(n => new OpenApiString(n.ToString())); | |
var arr = new OpenApiArray(); | |
arr.AddRange(names); | |
schema.Extensions.Add("x-enumNames", arr); | |
} | |
else if (typeInfo.IsGenericType && !schema.Extensions.ContainsKey("x-enumNames")) | |
{ | |
foreach (var genericArgumentType in typeInfo.GetGenericArguments()) | |
{ | |
if (genericArgumentType.IsEnum) | |
{ | |
var names = Enum.GetNames(genericArgumentType).Select(name => new OpenApiString(name)); | |
var arr = new OpenApiArray(); | |
arr.AddRange(names); | |
if (!schema.Extensions.ContainsKey("x-enumNames")) | |
schema.Extensions.Add("x-enumNames", arr); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment