如何使用两个架构对象定义响应

时间:2019-01-20 19:31:57

标签: openapi

我有两个模式对象:

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

是否有办法使用开放的API 3.0规范同时定义“#/ components / schemas / customer”和“#/ components / schemas / account”后响应正文

1 个答案:

答案 0 :(得分:0)

这取决于用例。

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

针对Swagger UI用户的说明:当前来自oneOf模式的Swagger UI does not generate examples。解决方法是在example旁边提供自定义oneOf


2)如果响应包含customeraccount的一组组合属性,请使用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'


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'