【问题标题】:[OpenAPI 3]Load different schema based on enum selection[OpenAPI 3]根据枚举选择加载不同的模式
【发布时间】:2020-05-14 15:16:35
【问题描述】:

我正在尝试根据 OpenAPI 3 规范为 API 编写一些文档。我想要实现的是每次enum 更改时加载不同的schema

比如我有这个参数。

parameters: 
        - description: The operation code (OC)
          required: true
          schema:
            $ref: '#/components/schemas/OperationCode'
          name: oc
          in: query

具有此架构:

components:
  schemas:
    OperationCode:
      type: string
      enum:
        - Operation1
        - Operation2

现在,在下面,我希望根据之前的枚举选择动态更改另一个参数:

- description: Required parameters for selected operation code
          required: true
          schema:
            $ref: ????
          name: parameters
          in: "body"

假设我想在这两种模式之间进行选择:

components:
  schemas:
    Operation1:
      properties:
        id1:
          type: integer
    Operation2:
      properties:
        id2:
          type: integer

关于如何实现这一目标的任何想法?提前谢谢你。

【问题讨论】:

    标签: yaml swagger openapi


    【解决方案1】:

    你可以这样做:

    ...
    schema:
      oneOf:
        - $ref: '#/components/schemas/Operation1'
        - $ref: '#/components/schemas/Operation2'
    

    这不会将正确的对象强制为枚举参数值。但是您可以描述可能的模式并在描述中解释 enum 操作关联。

    另见openapi "of" keywords

    【讨论】:

    • 这对我实现我的需要没有多大帮助。有没有另一种方法可以让我们说从选择的enum 中获取值并连接以创建正确架构的路径?
    • .. 我想我不明白你想做什么。
    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多