Forum Discussion

mtiwari176's avatar
mtiwari176
New Member
2 months ago

Swagger not rendering multiple schemas

Below is a piece of code from a GET endpoint. The endpoint will return one of the schemas based on the 'subject' which is an input parameter to the endpoint.
I want to display both the schemas and their field's definitions with example under defined response section on the documentation page. But I am not able to do that. My code shows below line:

array[]
The content depends on the subject.
discriminator: {"propertyName":"subject","mapping":
{"Schema_01":"#/components/schemas/Schema1","Schema_02":"#/components/schemas/Schema2"}}


......
content:
          description: The content depends on the subject.
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/Schema1'
              - $ref: '#/components/schemas/Schema2'
          discriminator:
            propertyName: subject
            mapping:
              Schema_01: '#/components/schemas/Schema1'
              Schema_02: '#/components/schemas/Schema2'
........

Here are the schemas:

components:
  schemas:

Schema1:
      description: The content and the structure depends on the subject.
      type: object
      required:
        - subject
      properties:
        subject:
          type: string

........

Schema2:
      description: The content and the structure depends on the subject.
      type: object
      required:
        - subject
      properties:
        subject:
          type: string

 

Please help me fix this. 

1 Reply

  • Hi mtiwari176

    Do you need to use discriminator here? If the goal is to just show examples of the different schemas to the consumer, then you could have:

          responses:
            '200':
              description: search results matching criteria
              content:
                application/json:
                  schema:
                    type: array
                    items:
                      oneOf:
                        - $ref: '#/components/schemas/Schema1'
                        - $ref: '#/components/schemas/Schema2'
                  examples:
                    Schema1:
                      value:
                        subject: foo
                    Schema2:
                      value:
                        subject: bar

    This would then render as follows in SwaggerUI and allow a consumer to select the appropriate example and see the schemas: