【问题标题】:Swagger with Title and Value带着标题和价值大摇大摆
【发布时间】:2017-11-08 16:45:20
【问题描述】:

我正在使用 Swashbuckle 为我的 .NET 程序集生成 Swagger。我有一个具有非顺序和负值的枚举。像下面这样

[JsonConverter(typeof(StringEnumConverter))]
public enum ShiftDayOffRule
{
    /// <summary>
    /// Shift has no day off rule.
    /// </summary>
    None = 0,       // DayOffNotRequired

    /// <summary>
    /// If shift with this rule is scheduled then next day must be a day off.
    /// </summary>
    OffAfter = 1,   // DayOffAfter <=> next is off

    /// <summary>
    /// If shift with this rule is scheduled then previous day must be a day off.
    /// </summary>
    OffBefore = -1, // DayOffBefore <=> previous is off

    /// <summary>
    /// If shift with this rule is scheduled then next day must be a work day.
    /// </summary>
    InAfter = 3     // DayInAfter <=> next cannot be off 
};

这些值被重新排序为 0、1、2、3 而不是 0、1、-1、3。

如何让输出 json 包含标题和值,例如:

"dayoffRule": { 
   "description": "Day off rule for the shift. ScheduleData.Enums.ShiftDayOffRule", 
   "enum": [ 
      {"title": "None", "value": 0}, 
      {"title": "OffAfter", "value": 1}, 
      {"title": "InAfter", "value": 3}, 
      {"title": "OffBefore", "value": -1} 
    ], 
    "type": "string" 
 }

【问题讨论】:

  • 重新排序可能是 Swashbuckle 中的一个错误。回复:为枚举值指定标签 - OpenAPI 规范不支持这一点。相关功能请求:#348#681

标签: c# swagger swagger-2.0 swashbuckle swagger-codegen


【解决方案1】:

在您的配置中启用DescribeAllEnumsAsStrings
这会将您的枚举更改为如下所示:

    "parameters": [
      {
        "name": "dayOffRule",
        "in": "query",
        "required": true,
        "type": "string",
        "enum": [
          "None",
          "OffAfter",
          "OffBefore",
          "InAfter"
        ]
      }
    ],

你不需要同时拥有title和value...

这是一个现场示例:
http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=Def#/Default/Default_Post



如果您绝对需要包含一个映射(标题:值),您可以使用 SchemaFilter 将其注入示例中,代码如下:

    private class EnumExampleSchemaFilter : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            if (type == typeof(ShiftDayOffRule))
            {
                var example = new Dictionary<string, int>();
                foreach (var item in Enum.GetValues(typeof(ShiftDayOffRule)))
                {
                    example.Add(item.ToString(), (int)item);
                }
                schema.example = example;
            }
        }
    }

这是 swagger 文档中的样子

    "Value": {
      "enum": [
        "None",
        "OffAfter",
        "OffBefore",
        "InAfter"
      ],
      "type": "string",
      "readOnly": true,
      "example": {
        "None": 0,
        "OffAfter": 1,
        "InAfter": 3,
        "OffBefore": -1
      }
    }

【讨论】:

  • 我应该在哪里放置EnumExampleSchemaFilter 类?我需要添加ISchemaFilter 的引用吗?
  • EnumExampleSchemaFilter 继续你的配置,看这里:github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/…
  • 我最终要做的是将 -1 作为枚举的有效值。此解决方案将值默认为 0、1、2、3。如果我删除 c.DescribeAllEnumsAsStrings();,我会收到构建错误,因为生成枚举有 _1 两次。一次为 1,一次为 -1。
  • 我没有收到任何错误,您能更具体一点吗?而且我的解决方案没有任何默认值... DescribeAllEnumsAsStrings 将枚举从其数值更改为字符串表示形式。
  • 我将以下 JSON 输入到 NSwagStudio "dayoffRule": { "format": "int32", "description": "Day off rule for the shift. ScheduleData.Enums.ShiftDayOffRule", "enum": [ 0, 1, 3, -1 ], "type": "integer" }, 并得到以下 C# `public enum ShiftDayoffRule { _0 = 0, _1 = 1, _3 = 3, _1 = -1, }` 注意_1 个重复项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 2014-07-19
  • 2015-12-14
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多