Swagger UI Web Api documentation Present enums as strings?

后端 未结 20 2058
半阙折子戏
半阙折子戏 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:43

    From the docs:

    httpConfiguration
        .EnableSwagger(c => 
            {
                c.SingleApiVersion("v1", "A title for your API");
    
                c.DescribeAllEnumsAsStrings(); // this will do the trick
            });
    

    Also, if you want this behavior only on a particular type and property, use the StringEnumConverter:

    public class Letter 
    {
        [Required]
        public string Content {get; set;}
    
        [Required]
        [EnumDataType(typeof(Priority))]
        [JsonConverter(typeof(StringEnumConverter))]
        public Priority Priority {get; set;}
    }
    

提交回复
热议问题