Is it possible to hide an Enum member in Swashbuck

2019-07-24 01:44发布

问题:

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