【问题标题】:Swagger validation error when API uses enumAPI 使用枚举时出现 Swagger 验证错误
【发布时间】:2021-02-09 12:26:55
【问题描述】:

我有以下带有两个参数的 Swagger 文档(简化版)。 TypeCode(字符串)和 Status(枚举)。当我尝试验证/导入 Azure Api Management 时,出现以下错误

Parsing error(s): JSON is valid against no schemas from 'oneOf'. Path 'paths['/Bids'].get.parameters[1]', line 1, position 188.

招摇文档

{
  "swagger": "2.0",
  "info": {
    "title": "/my-api",
    "description": "My API",
    "version": "1.0"
  },
  "paths": {
    "/Bids": {
      "get": {
        "tags": [
          "Bids"
        ],
        "parameters": [
          {
            "in": "query",
            "name": "typeCode",
            "type": "string"
          },
          {
            "in": "query",
            "name": "status"
          }

        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }   

  },
  "definitions": {
    "MyApi.ApplicationCore.Filter.Status": {
      "enum": [
        "Submitted",
        "Created",
        "Cancelled",
        "Accepted"
      ],
      "type": "string"
    }
  }
}

我不确定是什么导致了这个错误。我怀疑与枚举有关

【问题讨论】:

    标签: api swagger azure-api-management swashbuckle json-schema-validator


    【解决方案1】:

    发生错误是因为status 参数缺少type 属性。既然你说这个参数应该是一个枚举,它还需要包含值列表的 enum 属性。即使枚举定义在MyApi.ApplicationCore.Filter.Status schema 中,OpenAPI 2.0 中的查询参数也不能$ref schemas,所以enum 必须直接在status 参数中定义。

    正确版本:

    "parameters": [
      ...
    
      {
        "in": "query",
        "name": "status",
        "type": "string",
        "enum": [
          "Submitted",
          "Created",
          "Cancelled",
          "Accepted"
        ]
      }
    ],
    

    【讨论】:

    • 这一切都很好。发布的招摇是自动生成的(netcore 3.1)。我如何将“参数”部分中定义的枚举与“定义”相反(如解决方案中所述)?
    • Swashbuckle 可能有注释或配置选项来生成内联枚举。不过,我对 Swashbuckle 并不熟悉。试试asking a new question
    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2019-02-08
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多