Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active July 10, 2020 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/038d7c59bc978550dffa9dcc8931e2fd to your computer and use it in GitHub Desktop.
Save ankitvijay/038d7c59bc978550dffa9dcc8931e2fd to your computer and use it in GitHub Desktop.
Part 4 - EnumerationToEnumSchemaFilter.cs
// 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