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.

4

1 回答 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,

}

但在招摇 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

于 2020-04-21T23:52:37.630 回答