Skip to content

oneOf type or array of same type generated examples #2676

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

Open
indigane opened this issue Mar 27, 2025 · 3 comments
Open

oneOf type or array of same type generated examples #2676

indigane opened this issue Mar 27, 2025 · 3 comments

Comments

@indigane
Copy link

indigane commented Mar 27, 2025

Describe the bug
When having a schema as follows

MetaOrganisationMembership:
  oneOf:
  - $ref: '#/components/schemas/OrganisationMembership'
  - type: array
    items:
      $ref: '#/components/schemas/OrganisationMembership'

Redoc renders only one example, and the content is an empty array [ ].

The request body schema on the other hand correctly shows two options, with one of them being the type itself and the other being an array of the same type.

Only the example generation is the issue here.

Edit for clarification:

  • Redoc is only generating one example while it should generate two (this is due to the examples having the same key and overwriting each other).
  • The generated example is wrong (an empty array).

Expected behavior

There are two examples, one example of the type and other of the type in an array.

Minimal reproducible OpenAPI snippet(if possible)

If you give me an editor where I can edit the openapi.yaml and see the Redoc result I can create one.

Below is how the schema currently uses the MetaOrganisation schema.

post:
  operationId: organisations_members_create
  parameters:
  - in: path
    name: organisation_id
    schema:
      type: string
    required: true
  tags:
  - organisations-members
  requestBody:
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/MetaOrganisationMembership'

Screenshots

This is correct:

Image

This is the generated example(s):

Image

This is how it looks like in swagger-ui:

Image

Swagger-ui only displays one of the examples. If the oneOf order is swapped then it renders this:

Image

Additional context

Redoc version: 2.4.0

After debugging this a bit, there are actually two issues here:

  1. The subSchema.title here has the same value ("OrganisationMembership") for both the type and the array of the same type, which means that only the last example in oneOf is kept. https://github.com/Redocly/redoc/blob/v2.4.0/src/services/models/MediaType.ts#L69
    • This means that swapping the oneOf order will also render one example in redoc:
      Image
    • The expected behvavior is two examples using a dropdown two switch between them.
  2. The Sampler returns [] as the sample for the array of type example. https://github.com/Redocly/redoc/blob/v2.4.0/src/services/models/MediaType.ts#L63
@jeremyfiel
Copy link

Redoc does not sync the examples window with the main column. related #2346

@indigane
Copy link
Author

indigane commented Apr 9, 2025

Redoc does not sync the examples window with the main column. related #2346

I'll just note for clarity that #2346 is only tangentially related as it also mentions oneOf and the same part of the UI but the similarities end there.

Of course #2346 would be nice-to-have as well if the examples were being generated at all. However for me #2346 is just that, nice-to-have, but this issue (#2676) is more of a blocker.

I added some clarification to the issue description as well just in case.

@jeremyfiel
Copy link

thanks for describing more information. it seems you're onto a few things, first the title duplication. I tried adding different titles to each subschema and it created the first oneOf without any issue but it still has the empty array for the second one.

Image

Image

Image

Digging a little deeper, I think it has to do with this
https://github.com/Redocly/openapi-sampler/blob/5010bc068f8d3b40770de74948bbb4f06cc15bf9/src/traverse.js#L118-L124

the sampler is passing schema.oneOf[0] as the selectedSubSchema function argument rather than iterating through the array and is only taking the first oneOf instance when traversing the list of schemas.

So this line will always only take the first index of a oneOf
https://github.com/Redocly/openapi-sampler/blob/5010bc068f8d3b40770de74948bbb4f06cc15bf9/src/traverse.js#L176


If you're up for it, a PR is welcome to try to fix this issue. Otherwise, the team will prioritize this request.

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

No branches or pull requests

2 participants