Forum Discussion

sudha78's avatar
sudha78
New Contributor
9 months ago

Swagger schema with oneOf with List of object is no generating sample response in UI

Hi,
The below swagger definition is not creating sample responses in the Swagger UI. 

I believe its due to usage of oneOf with List of objects. Please see the example below

 

"content": {
"application/json": {
"schema": {
"oneOf": [
{"$ref": "#dogs"},
{"$ref": "#cats"}

]
}
}
}

 

"dogs": {
"type": "array",
"items": {
"$ref": "#/animal"
}
},

 

"cats": {
"type": "array",
"items": {
"$ref": "#/animal"
}
}

 

"animal": {
"type": "object",
"properties": {
"breed": {
"type": "string"
}
}
}

 

3 Replies

  • Hi sudha78,

     

    I'm not 100% sure on what you are trying to achieve, but here's a definition showing two flavours similar to above.

     

    openapi: 3.0.3
    info:
      title: Simple Pets API
      description: |-
        This is a sample Pets API showcasing some `oneOf` scenarios
      version: 0.0.1
    tags:
      - name: pets
        description: Everything about your Pets
    paths:
      /pets:
        get:
          tags:
            - pets
          summary: Get pets from catalog
          description: Returns a list of pets found on the server
          operationId: getPets
          responses:
            '200':
              description: successful operation
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/Pets'
      /animals:
        get:
          tags:
            - pets
          summary: Get pets from catalog
          description: Returns a list of pets found on the server
          operationId: getAnimals
          responses:
            '200':
              description: successful operation
              content:
                application/json:
                  schema:
                    oneOf: 
                      - $ref: '#/components/schemas/Cat1'
                      - $ref: '#/components/schemas/Dog1'            
    components:
      schemas:
        Dog:
          type: object
          properties:
            barks:
              type: boolean
              example: true
            likesFetch:
              type: boolean
              example: true
        Cat:
          type: object
          properties:
            meows:
              type: boolean
              example: true
            likesFetch:
              type: boolean
              example: false
        Pets:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/Cat'
              - $ref: '#/components/schemas/Dog' 
        Animal:
          type: object
          properties:
            breed:
              type: string
              example: poodle
        Cat1:
          type: array
          items:
            $ref: '#/components/schemas/Animal'
        Dog1:
          type: array
          items:
            $ref: '#/components/schemas/Animal'    
    • sudha78's avatar
      sudha78
      New Contributor

      Hi frankkilcommins 

      Thanks for your reply.

      The swagger definition is fine, but the problem is Response Sample(using ReDoc) is not generated for definition
      1) The problem am facing for path '/pets' is, the 'Response Sample'  is generated only for first option 'Cat1' and not generated for 'Dog1'

      2) For path '/animal', There is no Response sample generated for either 'Cat' or 'Dog'

  • Hi sudha78,

     

    You're welcome. As far as I can tell both definitions are rendering fine (with and without examples) via SwaggerUI. You can try by importing either of the following into https://editor-next.swagger.io (File > Import URL):

     

    https://raw.githubusercontent.com/frankkilcommins/OAS-Examples/main/OAS3_0/oneOf-simple-withexample.yaml

     

    https://raw.githubusercontent.com/frankkilcommins/OAS-Examples/main/OAS3_0/oneOf-simple-noexample.yaml

     

    If your problem is purely with ReDoc, then head over to https://github.com/Redocly/redoc and register and issue. They do have some open issues regarding multiple oneOf rendering.