【问题标题】:Is it possible to hide an Enum member in Swashbuckle.AspNetCore?是否可以在 Swashbuckle.AspNetCore 中隐藏 Enum 成员?
【发布时间】:2019-02-16 19:19:40
【问题描述】:

我有一个枚举,例如

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

是否可以通过生成的 swagger json 排除 unknown 成员的方式来装饰它?

我可能会编写一个架构/文档过滤器,但想知道是否有现成的东西。

【问题讨论】:

  • 也许与 [jsonIgnore]

标签: swagger asp.net-core-webapi swashbuckle


【解决方案1】:

你可以试试这个:

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" };
}

魔术技巧是后缀Specified,表示属性将由 Newtonsoft.Json 呈现

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多