如何使用两个模式对象定义响应

1 openapi

我有两个模式对象:

'#/components/schemas/customer' '#/components/schemas/account'

有没有办法我们可以使用开放 API 3.0 规范使用 '#/components/schemas/customer' 和 '#/components/schemas/account' 定义发布响应正文

Hel*_*len 5

这取决于用例。

1) 如果响应是 或customeraccount则使用oneOf

      responses:
        '200':
          description: A `customer` object or an `account` object
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/customer'
                  - $ref: '#/components/schemas/account'
                # Example for Swagger UI - see the note below
                example:
                  foo: bar
Run Code Online (Sandbox Code Playgroud)

Swagger UI 用户请注意:目前 Swagger UI不会oneOf架构生成示例。解决方法是exampleoneOf.


2) 如果响应具有来自customer和 的一组组合属性account,请使用allOf

      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MyResponse'

components:
  MyResponse:
    allOf:
      - $ref: '#/components/schemas/customer'
      - $ref: '#/components/schemas/account'
Run Code Online (Sandbox Code Playgroud)


3) 如果响应有两个属性,其中一个是对象customer,另一个是account对象,则使用以下内容:

      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MyResponse'

components:
  MyResponse:
    type: object
    properties:
      customer:
        $ref: '#/components/schemas/customer'
      account:
        $ref: '#/components/schemas/account'
Run Code Online (Sandbox Code Playgroud)