redoc icon indicating copy to clipboard operation
redoc copied to clipboard

oneOf type or array of same type generated examples

Open indigane opened this issue 8 months ago • 5 comments

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

indigane avatar Mar 27 '25 10:03 indigane

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

jeremyfiel avatar Apr 08 '25 15:04 jeremyfiel

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.

indigane avatar Apr 09 '25 09:04 indigane

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.

jeremyfiel avatar Apr 13 '25 04:04 jeremyfiel

Thanks for taking a second look. I do have interest in solving these and contributing a fix but regrettably I do not currently have the time to do so. If this is still an open issue when I do I'll look into it but that may be far in the future.

indigane avatar Apr 18 '25 12:04 indigane

Everyone is knee deep preparing the V2 release so it may take a bit of time to address this.
Thanks for your patience

jeremyfiel avatar Apr 18 '25 14:04 jeremyfiel