首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swagger 2.0 Yaml中的多个请求示例

Swagger 2.0 Yaml中的多个请求示例
EN

Stack Overflow用户
提问于 2021-03-06 07:30:02
回答 1查看 527关注 0票数 1

我有一个API,它对json有效负载有一些互斥的参数。我想在多个示例中显示这一点,但是yaml文件中的schema似乎只能生成一个示例。

如果我的输入可以是:

代码语言:javascript
运行
复制
{
  "text": "some text"
}

代码语言:javascript
运行
复制
{
  "list": ["some text", "some more"]
}

但不是

代码语言:javascript
运行
复制
{
  "text": "some text",
  "list": ["some text", "some more"]
}

在swagger 2.0中如何做到这一点?

像下面这样的模式定义具有误导性

代码语言:javascript
运行
复制
definitions:
  MutexSchema:
    type: object
    properties:
      list:
        type: array
        items:
          type: string
        example: ["some text", "some more"]
      text:
        type: string
        example: "Some text"

而且似乎您不能指定多个body选项。显示相互排斥的有效负载及其相应响应的好方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2021-03-06 22:27:39

OpenAPI 2.0不支持互斥属性,但您可以通过将minProperties: 1maxProperties: 1添加到您的架构来模拟这一点。这本质上意味着只能传递textlist,但不能同时传递两者。

代码语言:javascript
运行
复制
definitions:
  MutexSchema:
    type: object
    properties:
      list:
        type: array
        items:
          type: string
        example: ["some text", "some more"]
      text:
        type: string
        example: "Some text"
    minProperties: 1   # <--------
    maxProperties: 1

显示相互排斥的有效负载及其相应响应的好方法是什么?

迁移到支持用于请求和响应的oneOfmultiple examplesOpenAPI 3。请注意,无法将请求和响应示例关联起来,但是可以在description字段中提供附加信息。

代码语言:javascript
运行
复制
paths:
  /something:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MutexSchema'

            # Request body examples
            examples:
              text example:
                summary: Example with text
                value:
                  text: Some text
              list example:
                summary: Example with list
                value:
                  list: [some text, some more]
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                ...

              # Response examples
              examples:
                ex1:
                  summary: ...
                  value:
                    ...
                ex2:
                  summary: ...
                  value:
                    ...

components:
  schemas:
    MutexSchema:
      oneOf:
        - $ref: '#/components/schemas/Text'
        - $ref: '#/components/schemas/List'

    Text:
      type: object
      required:
        - text     # <--- Property must be marked as required for oneOf to work
      properties:
        text:
          type: string
          example: Some text
      additionalProperties: false

    List:
      type: object
      required:
        - list     # <--- Property must be marked as required for oneOf to work
      properties:
        list:
          type: array
          items:
            type: string
          example: [some text, some more]
      additionalProperties: false
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66500859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档