Is it possible to hide an Enum member in Swashbuck

2019-07-24 01:55发布

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条回答
Bombasti
2楼-- · 2019-07-24 02:24

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

查看更多
登录 后发表回答