enums - 是否可以在 yaml 中使用枚举作为路径参数?

标签 enums yaml openapi

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文件编译后自动生成的文件。

最佳答案

对于 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

关于enums - 是否可以在 yaml 中使用枚举作为路径参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56699051/

相关文章:

C 枚举类型名称作用域

python - 如何使用 python 从字典中编写 yaml 文件?

excel - 我可以使用什么文本格式来呈现 Excel 电子表格中的原始数据?

swagger - 我如何记录我没有所有权的对象?

java 枚举新列表创建未按预期工作

C++ std::random 和枚举类

c++ - 在类外定义静态匿名枚举

json - YAML 和 JSON 有什么区别?

java - OpenAPI validator 规则使用 max 而不是 size

java - UnitTest call/swagger-ui.html 导致 springdoc-openapi 出现 404