Swagger UI Web Api documentation Present enums as strings?

后端 未结 20 2054
半阙折子戏
半阙折子戏 2020-11-27 11:31

Is there a way to display all enums as their string value in swagger instead of their int value?

I want to be able to submit POST actions and put enums according to

20条回答
  •  悲&欢浪女
    2020-11-27 11:53

    I have found nice workaround Here:

    @PauloVetor - solved it using ShemaFilter like this:

    public class EnumSchemaFilter : ISchemaFilter
    {
        public void Apply(OpenApiSchema model, SchemaFilterContext context)
        {
            if (context.Type.IsEnum)
            {
                model.Enum.Clear();
                Enum.GetNames(context.Type)
                    .ToList()
                    .ForEach(n => model.Enum.Add(new OpenApiString(n)));
                }
            }
        }
    }
    

    And in Startup.cs:

    services.AddSwaggerGen(options =>
    {
        options.SchemaFilter();
    }
    

提交回复
热议问题