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

ReDoc fails to expand JSON Schema refs in examples #840

Closed
MateusAzevedo opened this issue Mar 8, 2019 · 7 comments
Closed

ReDoc fails to expand JSON Schema refs in examples #840

MateusAzevedo opened this issue Mar 8, 2019 · 7 comments

Comments

@MateusAzevedo
Copy link

Related to #311 and #380

I'm using external schema and example files to reuse them in multple operations. ReDoc correctly render example code for the first operation, but fails to render subsequent ones, rendering something like this:

captura de tela de 2019-03-08 12-20-27

As @vegyraupe commented in #380:

The problem occurs when the same external reference is being used twice. This results in bundle to only include it once and returning a relative reference instead of the dereferenced object.

You asked for a reproducible example, so a created this Gist to help.

I tested the example spec with "redoc-cli serve", "redoc-cli bundle" and "speccy serve", all result in the same problem.
In the final bundled spec file, the schema and example code are included in the first operation, but the other ones have just a reference to the first operation:

"responses": {
    "200": {
        "description": "OK",
        "content": {
            "application/json": {
                "schema": {
                    "$ref": "#/paths/~1cities/post/responses/201/content/application~1json/schema"
                },
                "example": {
                    "$ref": "#/paths/~1cities/post/responses/201/content/application~1json/example"
                }
            }
        }
    }
}

As you can see, both schema and example have a reference to the first operation. ReDoc can render the schema for all operataion (it follows the $ref) but don't do the same for the example.

@RomanHotsiy
Copy link
Member

Your spec is incorrect according to the OpenAPI 3.0 spec.

example field of Media Type Object can't be a $ref - it is direct example value. (the fact ReDoc dereferences it for the first operation is, in fact, ReDoc bug which I don't think I can fix in short term)

In order to use $ref for example values use examples field. It supports $ref and ReDoc will handle it properly in the upcoming release (I just implemented it).

Here is the updated gist: https://gist.github.com/RomanGotsiy/29152ff5a4da68dbb49131eaaf347530

Closing this issue. Feel free to continue the discussion here.

The new ReDoc version is coming tomorrow.

RomanHotsiy added a commit that referenced this issue Mar 11, 2019
@alexgmin
Copy link
Contributor

alexgmin commented Mar 13, 2019

Your spec is incorrect according to the OpenAPI 3.0 spec.

example field of Media Type Object can't be a $ref - it is direct example value. (the fact ReDoc dereferences it for the first operation is, in fact, ReDoc bug which I don't think I can fix in short term)

This was also a point of contention in Swagger-UI. swagger-api/swagger-ui#4924 (comment)

While I would prefer that example was rendered, I don't mind if it works in examples as long as it works.

@RomanHotsiy
Copy link
Member

It will work using externalValue in examples once I release a new version. This happens a bit later today.

@RomanHotsiy
Copy link
Member

v2.0.0-rc.4 has been just released with the support of externalValue

@Okobiti
Copy link

Okobiti commented Dec 13, 2019

@RomanHotsiy the gist you provided https://gist.github.com/RomanGotsiy/29152ff5a4da68dbb49131eaaf347530 seems to have been deleted. Any chance you can provide an example of how examples and $ref should work with redoc? I'm using version 2.0.0-rc.18, and having some issues displaying it.

@jaspersorrio
Copy link

TL;DR

It works if you use examples instead of example

# DOESN'T WORK ×
      example:
        title: Test & Measure
        children:
          - $ref: "#/components/schemas/SubCategory/example"

# WORKS ✓  (Changed "example" -> "examples") 
      examples:
        title: Test & Measure
        children:
          - $ref: "#/components/schemas/SubCategory/example"

@dpkirchner
Copy link

@jaspersorrio Would you mind including that in the context of the original gist? I'm not sure where to add that to get it to render properly in ReDoc.

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

No branches or pull requests

6 participants