Enum name generation nswag and swashbuckle

问题描述 投票:0回答:1

我有一个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

c# swashbuckle .net-core-3.1 swashbuckle.aspnetcore nswagstudio
1个回答
0
投票

我正在使用我的测试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

© www.soinside.com 2019 - 2024. All rights reserved.