Search code examples
swagger-uiopenapi

How to display all of the oneOf schemas in Swagger UI?


I have an OpenAPI document where an endpoint uses oneOf for the request body (this endpoint has 2 possible different schemas). In Swagger UI, I can only see one of the schemas where the endpoint is displayed, which I understand is normal. How could I display the other schema or link it, so I can access it easily?


Solution

  • The Schema tab in Swagger UI displays all subschemas of oneOf and anyOf schemas:


    To reflect the alternatives on the Example Value tab, you'll need to manually define multiple request body examples, one for each schema. This will add a dropdown to Swagger UI so that the users can switch between the examples.

    paths:
      /something:
        post:
          requestBody:
            content:
              application/json:
                schema:
                  oneOf:
                    - $ref: '#/components/schemas/Foo'
                    - $ref: '#/components/schemas/Bar'
                examples:
                  fooExample:
                    summary: An example of Foo data
                    value:
                      foo: hello
                  barExample:
                    summary: An example of Bar data
                    value:
                      bar: 123
    


    I think there was an existing feature request to generate multiple examples for oneOf/anyOf subschemas automatically, but I can't find it. Feel free to submit a feature request yourself.