Search code examples
swagger-editorswaggerhub

Reference multiple schema in a single schema


In swaggerhub I am trying to build a restful API. For which I have created n number of POJOs for request. I would like to represent them as references in other POJO example:

 ClientInformation:
   type: object
   schema:
     $ref: '#/definitions/EditClient'
     $ref: '#/definitions/OtherDetails' #Duplicate mapping key error I see at this line
    properties:
      notes:
      type: string
      example: This is a test
      description: A description

I tried , but didn't work. Kindly suggest.


Solution

  • If ClientInformation is a merger of the EditClient and OtherDetails schemas, you need allOf:

    ClientInformation:
      allOf:
        - $ref: '#/definitions/EditClient'
        - $ref: '#/definitions/OtherDetails'
        - type: object
          properties:
            notes:
              type: string
              example: This is a test
              description: A description
    

    If ClientInformation has properties that are instances of other schemas, you can $ref the property schemas as follows:

    ClientInformation:
      type: object
      properties:
        notes:
          type: string
          example: This is a test
          description: A description
        foo:
          $ref: '#/definitions/EditClient'
        bar:
          $ref: '#/definitions/OtherDetails'