Is it possible to hide an Enum member in Swashbuckle.AspNetCore?

扶醉桌前 提交于 2019-12-11 11:28:10

问题


I have an enum such as

public enum SampleFormats
{
   unknown = 0,
   audio = 1,
   video = 2,
}

Is it possible to decorate the unknown member in a way that it is excluded by the generated swagger json?

I could possibly write a schema/document filter, but was wondering if there was something out of the box.


回答1:


You can try this:

public enum SampleFormats
{
    unknown = 0,
    audio = 1,
    video = 2,
}

public class ResultModel
{
    public SampleFormats Format { get; set; }

    [JsonIgnore]
    public bool FormatSpecified
    {
        get { return Format != SampleFormats.unknown; }
    }

    public string Name { get; set; }
}

[HttpGet()]
[AllowAnonymous]
public async Task<ResultModel> Get()
{
    return new ResultModel { Format = SampleFormats.unknown, Name = "Test" };
}

A magic trick is suffix Specified that indicate a property would be rendered by Newtonsoft.Json



来源:https://stackoverflow.com/questions/52290269/is-it-possible-to-hide-an-enum-member-in-swashbuckle-aspnetcore

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