How define a response with two schemas objects

I have two schemas objects:

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

Is there are way we can define post response body with both '#/components/schemas/customer' and '#/components/schemas/account' using open API 3.0 specs

Upvotes: 1

Views: 2487

Answers (1)

Helen
Helen

Reputation: 98022

This depends on the use case.

1) If the response is either customer or account, use 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

Note for Swagger UI users: Currently Swagger UI does not generate examples from oneOf schemas. The workaround is to provide a custom example alongside oneOf.


2) If the response has a combined set of properties from customer and account, use 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) If the response has two properties, one of which is a customer object and another is an account object, use the following:

      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'

Upvotes: 5

Related Questions