【问题标题】:Enum name generation nswag and swashbuckle枚举名称生成 nswag 和 swashbuckle
【发布时间】:2020-08-03 17:46:14
【问题描述】:

我有一个 Web API,它使用 swashbuckle 生成 swagger ui 和 json 以生成代码。问题是当我使用 nswag studio 生成代码时,它不会生成正确格式的枚举。让我更清楚地解释一下。我在后端这样创建的枚举:

public enum OrderType
{
    ASC = 0,
    DESC = 1,
    None = 2,
}

但是我使用nswag studio 代码生成器得到的结果是这样的:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.11.0 (Newtonsoft.Json v12.0.0.0)")]
public enum OrderInfoOrderType
{
    [System.Runtime.Serialization.EnumMember(Value = @"None")]
    None = 0,

    [System.Runtime.Serialization.EnumMember(Value = @"ASC")]
    ASC = 1,

    [System.Runtime.Serialization.EnumMember(Value = @"DESC")]
    DESC = 2,

}

我的启动代码是这样的:

services.AddMvc(option => option.EnableEndpointRouting = false).AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            options.JsonSerializerOptions.IgnoreNullValues = true;
        });

我在这里做错了什么?我正在使用.net core 3.1

【问题讨论】:

  • 哎呀闻起来像个虫子,你能发布你的 swagger.json 吗?

标签: c# swashbuckle .net-core-3.1 swashbuckle.aspnetcore nswagstudio


【解决方案1】:

我正在使用我的测试 API 对此进行测试,并且我有这个枚举:

    public enum CustomEnum
    {
        Text = 1,
        Numeric = 2,
        Date = 4,
        Numeric_Function = 8,
        Dropdown_List = 16,
        Checkbox = 32
    }

https://github.com/heldersepu/Swagger-Net-Test/blob/master/Swagger_Test/Controllers/TestEnumController.cs

生成的nswag studio 看起来像:

/// <summary>CustomEnum</summary>
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.11.0 (Newtonsoft.Json v11.0.0.0)")]
public enum Value4
{
    [System.Runtime.Serialization.EnumMember(Value = @"Text")]
    Text = 0,

    [System.Runtime.Serialization.EnumMember(Value = @"Numeric")]
    Numeric = 1,

    [System.Runtime.Serialization.EnumMember(Value = @"Date")]
    Date = 2,

    [System.Runtime.Serialization.EnumMember(Value = @"Numeric_Function")]
    Numeric_Function = 3,

    [System.Runtime.Serialization.EnumMember(Value = @"Dropdown_List")]
    Dropdown_List = 4,

    [System.Runtime.Serialization.EnumMember(Value = @"Checkbox")]
    Checkbox = 5,

}

但在 swagger json 上没有指示枚举的值:
http://swagger-net-test.azurewebsites.net/swagger/docs/V1

"/api/TestEnum": {
  "get": {
    "tags": [
      "TestEnum"
    ],
    "summary": "Simple GET echoing the given param",
    "operationId": "TestEnum_Get",
    "consumes": [],
    "produces": [
      "application/json",
      "text/json",
      "text/html"
    ],
    "parameters": [
      {
        "name": "value",
        "in": "query",
        "description": "CustomEnum",
        "required": true,
        "type": "string",
        "enum": [
          "Text",
          "Numeric",
          "Date",
          "Numeric_Function",
          "Dropdown_List",
          "Checkbox"
        ]
      }
    ]

我认为这样就可以正常工作,客户端上的数值无关紧要。

你可以在这里看到整个招摇: http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=TestEnum#/TestEnum/TestEnum_Get

【讨论】:

    猜你喜欢
    • 2022-09-28
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多