Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple responses using oneOf attribute do not appear in UI #3803

Open
danr1979 opened this issue Oct 23, 2017 · 119 comments · May be fixed by #5530
Open

Multiple responses using oneOf attribute do not appear in UI #3803

danr1979 opened this issue Oct 23, 2017 · 119 comments · May be fixed by #5530

Comments

@danr1979
Copy link

danr1979 commented Oct 23, 2017

Q A
Bug or feature request? Bug
Which Swagger/OpenAPI version?
Which Swagger-UI version?
How did you install Swagger-UI?
Which browser & version?
Which operating system?

Demonstration API definition

responses:
  '200':
    description: OK
    content:
      application/json:
        schema:
          oneOf:
            - $ref: '#/components/schemas/AuthorisationResponse'
            - $ref: '#/components/schemas/ForbiddenResponse'

Configuration (browser query string, constructor, config.yaml)

constructorConfig contains just the default values.

Expected Behavior

Since I have two schemas passed in as a $ref to the 200 response I would expect to see two schemas in the Swagger UI application.

Current Behavior

Instead I see only the reference to the 200 response and its description with no example responses displayed as JSON in the black boxes.

Possible Solution

Context

@webron
Copy link
Contributor

webron commented Oct 23, 2017

You've left out a bunch of details from the table - please fill them in.

We also need a full API definition - we can't really test the issue with the snippet you provided.

@mrichar1
Copy link

mrichar1 commented Nov 3, 2017

The following spec shows nothing in the responses section of the UI - replacing the oneOf section with just an object containing either of the refs and it works fine.

Q A
Bug or feature request? Bug
Which Swagger/OpenAPI version? 3.0.0
Which Swagger-UI version? 3.4.2
How did you install Swagger-UI? CDN (Cloudflare)
Which browser & version? firefox 56/Chromium 62
Which operating system? Linux (Ubuntu 16.04.3
openapi: 3.0.0
info:
  contact:
    email: [email protected]
    name: No Name
  description: Example API
  version: 0.0.2
  title: example
paths:
  "/test":
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                oneOf:
                - "$ref": "#/definitions/foo"
                - "$ref": "#/definitions/bar"
definitions:
  foo:
    type: string
  bar:
    type: int

@Zakini
Copy link

Zakini commented Nov 20, 2017

Similar issue occurs when using oneOf for a request body.

Q A
Bug or feature request? Bug
Which Swagger/OpenAPI version? 3.0.0
Which Swagger-UI version? which ever version is on editor.swagger.io
How did you install Swagger-UI? using editor.swagger.io
Which browser & version? Firefox 57.0
Which operating system? macOS 10.12.6
openapi: 3.0.0
info:
  title: Test API
  version: 0.0.1
servers:
  - url: 'localhost:3000/api'
    description: Local API URL.
paths:
  /test:
    get:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/a'
                - $ref: '#/components/schemas/b'
      responses:
        '200':
          description: OK
components:
  schemas:
    a:
      type: integer
    b:
      type: string

@epetitje
Copy link

Hello,

I have the exact same problem and this issue still exists in version 3.5.

@Oleg-Arkhipov
Copy link

I would be glad to see this implemented too. In the current state using multiple responses is not really usable because documentation consumer has to dig into YAML/JSON to find out all this information.

@crussell52
Copy link

@shockey I'm confused why this has been tagged as a Feature instead of Bug. According to the Specification, schema is listed as supporting anyOf, oneOf, and allOf.

@crussell52
Copy link

Applying the oneOf to type within components/schema does not work around this problem.

components:
  responses:
    ...
    ServerWriteError:
      description: Write Failed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ServerWriteErrorResponse'

  schemas:
    ...
    ServerWriteErrorResponse:
      oneOf:
        - $ref: '#/components/schemas/PartialWriteError'
        - $ref: '#/components/schemas/BaseError'

You can sort of work around this by declaring type: object as a sibling to oneOf:

components:
  responses:
    ...
    ServerWriteError:
      description: Write Failed
      content:
        application/json:
          schema:
            oneOf:
            $ref: '#/components/schemas/ServerWriteErrorResponse'

  schemas:
    ...
    ServerWriteErrorResponse:
      type: object     # Workaround-ish
      oneOf:
        - $ref: '#/components/schemas/PartialWriteError'
        - $ref: '#/components/schemas/BaseError'

OR In the referenced schema component

components:
 responses:
    ...
    ServerWriteError:
      description: Write Failed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ServerWriteErrorResponse'

  schemas:
    ...
    ServerWriteErrorResponse:
      type: object
      oneOf:
        - $ref: '#/components/schemas/PartialWriteError'
        - $ref: '#/components/schemas/BaseError'

The work-around isn't great because it puts an empty object in the response example (which is shown by default), but at least consumers can see the relevant models:
image

image

@shockey
Copy link
Contributor

shockey commented Jan 11, 2018

Hey @crussell52, I tagged it as a feature because Swagger-UI hasn't/doesn't support oneOf responses, as opposed to there being an issue with how a oneOf responses implementation renders things.

In other news... Work on this stalled (clearly), I'm going to do my best to circle back in January.

@crussell52
Copy link

@shockey That makes sense... thanks for the clarification :)

@shockey shockey mentioned this issue Jan 23, 2018
22 tasks
@vickysg0210
Copy link

I am facing the same issue. Using oneOf in requestBody is fine. You can not see the exact example but can still see the model. User can dig inside.

However, responses not working. :) Just to share

@JinSung-Hwang
Copy link

i`m facing the same issue

@jasongonzales23
Copy link

+1

@PavelStefanov
Copy link
Contributor

@shockey I want to fix this issue. But I don't know how to do. There are two cases:
1.

schema: 
  type: array 
  items:  
    oneOf:  
      - type: string  
      - type: integer

I can do like this

image

schema:
  oneOf:
  - $ref: '#/components/schemas/a'
  - $ref: '#/components/schemas/b'

components:
  schemas:
    a:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
    b:
      type: array
      items:
        type: integer
        format: int64

I can do like this
image

Or something more complicated like in model tab? What do you think?

@JinSung-Hwang
Copy link

JinSung-Hwang commented Feb 5, 2018

i want the 2 type

@DustinOgan
Copy link

of those I prefer the second one. Since we can name examples in the yaml, would it be a stretch to have
example1:
text box

example2:
text box

@PavelStefanov
Copy link
Contributor

@DustinOgan this is just two different case for oneOf attribute, for array and for object, and example like I can do.

@NeoDobby
Copy link

For arrays I like the approach to have one example of each item type in the example array.
For objects, I think an approach like in the model tab seems appropriate to me.

@mikefidel
Copy link
Contributor

I too wish to see some way for Models to display oneOf. It is used in my OpenAPI document. It's not used in a particularly critical API endpoint, but thumbs-up to anybody working on a solution.

@rickyblaha
Copy link

@mikefidel Models in Swagger UI does display oneOf now. We just recently migrated our docs from swagger 2 to open api 3 just for that reason. See this help page: https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

@mrichar1
Copy link

@rickyblaha The spec supports it, but not the UI (unless you're saying bug has been resolved in the UI code?)

@mathis-m
Copy link
Contributor

mathis-m commented Sep 4, 2020

Swagger ui should generate multiple examples for nested combined schemas as well.

For example:

openapi: 3.0.3
info:
  title: title
  version: 0.0.0
paths:
  /test:
    post:
      summary: test
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/nested'
      responses:
        200:
          description: OK
      operationId: test.post
components:
  schemas:
    nested:
      type: object
      properties:
        some: 
          type: string
        nestedProp:
          type: object
          anyOf:
            - $ref: '#/components/schemas/one'
            - $ref: '#/components/schemas/two'
    one:
      title: One
      type: object
      required:
        - name
      properties:
        name:
          type: string
          pattern: ^one$
      example: 
        name: one
    two:
      title: Two
      type: object
      required:
        - name
      properties:
        name:
          type: string
          pattern: ^two$
      example:
        name: two

is rendered like:
image
and should be rendered like:

openapi: 3.0.3
info:
  title: title
  version: 0.0.0
paths:
  /test:
    post:
      summary: test
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/nested'
            examples:
              nested_One:
                $ref: '#/components/examples/nested_One'
              nested_Two:
                $ref: '#/components/examples/nested_Two'
      responses:
        200:
          description: OK
      operationId: test.post
components:
  examples:
    nested_One:
      value: 
        some: string
        nestedProp:
          name: one
    nested_Two:
      value:
        some: string
        nestedProp:
          name: two
  schemas:
    nested:
      type: object
      properties:
        some: 
          type: string
        nestedProp:
          type: object
          anyOf:
            - $ref: '#/components/schemas/one'
            - $ref: '#/components/schemas/two'
    one:
      title: One
      type: object
      required:
        - name
      properties:
        name:
          type: string
          pattern: ^one$
      example: 
        name: one
    two:
      title: Two
      type: object
      required:
        - name
      properties:
        name:
          type: string
          pattern: ^two$
      example:
        name: two

image

@ssm-shah
Copy link

+1

@jokaorgua
Copy link

any news from maintainers regarding this issue?

@NagoLazaro
Copy link

Are there any updates from Swagger team regarding this functionality?

@hpl002
Copy link

hpl002 commented Mar 18, 2021

+1

@raffig
Copy link

raffig commented Jun 24, 2021

Any news about this issue?

@lsc-dotsource
Copy link

lsc-dotsource commented Sep 17, 2021

considering the website of SwaggerUI claims "Complete OAS Support Complete OAS Support Visualize APIs defined in Swagger 2.0 or OAS 3.0" (https://swagger.io/tools/swagger-ui/) and oneOf, anyOf and allOf are clearly part of openApi 3, this should thus be re-categorized as a Bug, or the softwares description be corrected.

@anthony-janocko
Copy link

Any update here? Not working for subschemas in responses.

@Laurianti
Copy link

Any news?

@jacobjmarks
Copy link

I am having a similar issue here. See below an example spec in which I am attempting to describe a form which can accept one of two possible schemas:

openapi: 3.0.1
info:
  title: FooBar
  version: 1.0.0
paths:
  /foobar:
    post:
      summary: Summary
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              oneOf:
              - type: object
                properties:
                  Foo:
                    type: string
              - type: object
                properties:
                  Bar:
                    type: string
      responses:
        default:
          description: Description

Please note that the same issue occurs when using a requestBody/content type of multipart/form-data

This endpoint should expect to receive a body of either Foo= or Bar=.
This schema causes the following Swagger UI interface to be rendered:

image

This is not correct. It is representing the form as a JSON payload and causes invalid requests to be sent to the endpoint in which the Swagger UI is encoding each character as a separate form value. E.g. sending a value of hi would result in the body 0=h&1=i.

This same issue occurs when using anyOf.

When using allOf, however, a correct representation is rendered as below:

image

Are there any updates here regarding the support for such oneOf and anyOf usage? Alternatively; am I incorrectly defining my schema in some way to achieve such usage?

@jaredbarranco
Copy link

jaredbarranco commented Jan 13, 2022

+1 to this - My scenario:
API client should expect return to contain array of objects who's underlying structure changes based on type fields that are optional query params.

Disregard above - I found a solution with current version:

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

@tomasvts
Copy link

tomasvts commented Feb 4, 2022

If anyone is having this problem, I changed to using the latest 4.4.1 version from unpkg and it worked fine.
I'm using the standalone version like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Swagger API</title>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/[email protected]/swagger-ui.css">
</head>
<body>

<div id="swagger-ui"></div>

<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js"></script>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js"></script>
<script>
    window.onload = function() {
        window.ui = SwaggerUIBundle({
            url: "YOUR URL HERE",
            dom_id: '#swagger-ui',
            deepLinking: true,
            presets: [
                SwaggerUIBundle.presets.apis,
                SwaggerUIStandalonePreset
            ],
            plugins: [
                SwaggerUIBundle.plugins.DownloadUrl
            ],
        })
    }
</script>
</body>
</html>

@EladG77
Copy link

EladG77 commented Apr 19, 2022

Any news about this?

@tomasvts
Copy link

tomasvts commented Apr 19, 2022 via email

@thiagomengue
Copy link

Hello,

Does anyone have a solution or workaround ?
I'm using the swaggerapi/swagger-ui:v4.11.0 docker image but the problem persists....

@ajith428
Copy link

Any update/workaround for the issue?

@alecnmk
Copy link

alecnmk commented Aug 10, 2022

👋 the is a workaround I've found working and built a simple demo:
https://gitlab.com/alecnmk/openapi-swaggerui-multiple-response-examples-demo/-/blob/main/openapi.yaml#/default/get_products__id_

@WellingtonSI
Copy link

I'd like to thank alecnmk for the help with the code.

@vpaflah
Copy link

vpaflah commented Mar 29, 2023

@jaredbarranco ' solution worked for me.

@njmulsqb
Copy link

I am also having this issue, @alecnmk solution worked partially, but anyOf is not working at all.
Using examples is not resolving the example content from YAML file properly.

@Moro-Code
Copy link

This issue should have been solved #5435 (comment)

@ivanzigoni
Copy link

👋 the is a workaround I've found working and built a simple demo: https://gitlab.com/alecnmk/openapi-swaggerui-multiple-response-examples-demo/-/blob/main/openapi.yaml#/default/get_products__id_

looks great but for anyone trying to use nestjs swagger module decorators, this unfortunately doesn't seem to apply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment