Last active
July 10, 2020 22:03
-
-
Save ankitvijay/038d7c59bc978550dffa9dcc8931e2fd to your computer and use it in GitHub Desktop.
Part 4 - EnumerationToEnumSchemaFilter.cs
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
// Import Swashbuckle.AspNetCore.SwaggerGen | |
public class EnumerationToEnumSchemaFilter : ISchemaFilter | |
{ | |
public void Apply(OpenApiSchema schema, SchemaFilterContext context) | |
{ | |
if (!context.Type.IsSubclassOf(typeof(Enumeration))) | |
{ | |
return; | |
} | |
var fields = context.Type.GetFields(BindingFlags.Static | BindingFlags.Public); | |
schema.Enum = fields.Select(field => new OpenApiString(field.Name)).Cast<IOpenApiAny>().ToList(); | |
schema.Type = "string"; | |
schema.Properties = null; | |
schema.AllOf = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment