首页 > 解决方案 > 是否可以在 yaml 中使用枚举作为路径参数?

问题描述

Swagger中有一个类似的问题:Reusing an enum definition as query parameter。我的问题是我是否可以使用枚举(可重复使用或不可重复使用)。每当我尝试这样做时,我都会收到错误,但是使用字符串不会给出任何错误

/path/{protocol}:
  patch:
    summary:
    operationId:
    tags:
    parameters:
      - name: protocol
        in: path
        description: # some description
        required: true
        schema:
          $ref: "#/components/schemas/ProtocolType"

   ProtocolType:
     type: string
     default: abcd
     enum:
      - abcd
      - aaa
      - bbb

我的问题是上述示例是否有效或者我应该尝试哪些可能的更改。我正在使用 OpenAPI 3.0.0 。

错误:

Compilation errors in XX.client.cpp
XX.client.cpp: In static member function ‘static void 
XX::SendSetProtocolReqRequest(std::string, const 
XX::model::SetProtocolReq_Request*, 
HTTPRequestEventContext::Ptr, uint64_t, HTTPClient*, FSM*, Statistics*, 
std::string, bool)’:
XX_Management.client.cpp:1822:33: error: no matching function for call to 
‘Json::ToValue(XX::model::XXEnumProtocolType*, 
framework::json::Value*)’
 Json::ToValue(&param, &value);  
                             ^

我对 XX.client.cpp 了解不多。它是一个自动生成的文件,它是在 yaml 文件编译后构建的。

标签: enumsyamlopenapi

解决方案


对于 SwaggerUI - 使用 2.0 规范,您必须使用内联枚举,例如:

swagger: '2.0'
info:
  title: Report API
  version: v1
paths:
  /report/{reportType}:
    get:
      tags:
      - ReportController
      parameters:
      - in: path
        name: reportType
        required: true
        type: string
        enum: [foo, bar, baz]

在 3.0 中,您可以使用参考:

---
openapi: 3.0.1
info:
  title: Report API
  version: v1
paths:
  "/report/{reportType}":
    get:
      tags:
      - ReportController
      parameters:
      - name: exportType
        in: path
        description: ''
        required: true
        schema:
          "$ref": "#/components/schemas/ReportType"
components:
  schemas:
    ReportType:
      enum:
      - foo
      - bar
      - baz
      type: string

推荐阅读