NET core 2.0 Swagger & Enum Display

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-15 05:28:44

问题


i have the next enum :

public enum DataTypes { [Description("All")] All, Man, Woman }

and i want to achive next :

to display decription for each enum to display the Enum Key and Value.

i used this next post : swagger-ui-web-api-documentation-present-enums-as-strings

I did the next code changes : in the enum, i defined :

[JsonConverter(typeof(StringEnumConverter))] //this is using newton
    public enum DataTypes
    {
        [Description("All")]
        All,
        [Description("Male222")]
        Male, 
       [Description("Female")]
       Female
    }

in ConfigureServices added :

services.AddMvc().AddJsonOptions(options =>
            options.SerializerSettings.Converters.Add(new StringEnumConverter()));
//and also  services.AddSwaggerGen(c =>            {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            c.DescribeAllEnumsAsStrings();
        });

in the model that used the Enum i did :

[EnumDataType(typeof(Constans.DataTypes))]
    [JsonConverter(typeof(StringEnumConverter))]
    public Constans.DataTypes DataType;

the only change i see in swagger that before it showed : [0,1,2] and now : [All,Male,Female] do i miss some settings?

UPDATE : i added above each enum value :

[Description("None enum descriptiondd")]
    [EnumMember(Value = "None")]

and replaced to :

c.DescribeAllEnumsAsStrings();
           to->   c.DocumentFilter<EnumDocumentFilter>();

for class EnumDocumentFilter i used code from swagger-ui-web-api-documentation-present-enums-as-strings

but i get error :

来源:https://stackoverflow.com/questions/64764537/net-core-2-0-swagger-enum-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!