-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Examples referenced with $ref are not rendered correctly #4924
Comments
Yep, I see that too in 3.19.2 and quite a few releases before that |
I'm wondering if this same spec interpretation extends to $ref usage inside example content. If I define a schema that envelops properties of other schema types, $ref usage is permitted. But If I then want to express an example, can I use the same $ref approach, or must the example be completely literal (without any $refs)? For example, is this valid according to swagger-ui team's spec interpretation, @webron : paths:
/order:
post:
requestBody:
content:
'application/json':
schema:
type: object
properties:
user:
$ref: '#/components/schemas/User'
product:
$ref: '#/components/schemas/Product'
quantity:
type: integer
example:
user:
$ref: '#/components/examples/User'
product:
$ref: '#/components/examples/Product'
quantity: 1
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
Product:
type: object
properties:
id:
type: integer
name:
type: string
examples:
User:
id: 1
name: Sasha
Product:
id: 1
name: Book |
I think $ref should work in that case too. Clearly, passing a 10-KB piece of JSON example as a string is very problematic. |
@sasha-borodin the spec isn't explicit about it, but I would expect what you shared to be rendered simply as: user:
$ref: '#/components/examples/User'
product:
$ref: '#/components/examples/Product'
quantity: 1 (that is, $ref's are not being dereferenced) |
@webron, thanks for the response! I think there are two issues:
Thank you again. |
I would like add another argument to the case for resolution. From my point of view, what is wrong in this example is the openapi: '3.0.0'
info:
title: 'test'
version: '1.0.0'
paths:
/hello:
get:
description: 'Get welcome message.'
responses:
'200':
description: 'welcome message'
content:
application/json:
schema:
$ref: '#/components/schemas/Welcome'
components:
schemas:
Welcome:
type: object
properties:
message:
$ref: '#/components/schemas/Message'
Message:
type: object
properties:
title:
type: string
info:
type: string
example:
$ref: '#/components/examples/WelcomeExample'
examples:
WelcomeExample:
$ref: '#/components/examples/MessageExample'
MessageExample:
value:
foo: 2
bar: 3 |
Encountering the same issue. Is anyone working on it? |
Been running into this lately as well. Any news? |
I'm picking this up - as mentioned in #4931 (comment), we shouldn't be resolving |
Just trying to clarify things. So basically we cannot use examples not only in a good way, but in fact. Because no one in their clear mind would insert a huge block of example data right in the middle of definitions - and that is exactly what we're forced to do, because:
Did I get it right? |
Couldn't really follow what you're saying, but regardless, this is how the spec is defined, not something within the control of this tool. |
I agree with @alexgmin. Could we request the ability to nest examples so we can, uhm... reuse definitions? After all, that's the whole idea of Domains, isn't it? |
@carlos-soto you can propose changes to the OpenAPI Specification here: |
Would be nice in addition to support arrays in
|
@dmak you can propose changes to the OpenAPI Specification syntax constructs here: As of OAS 3.0.3, multiple examples require the use of the responses:
"200":
content:
application/json:
examples:
sample1:
$ref: "#/components/examples/sample1"
sample2:
$ref: "#/components/examples/sample2"
sample3:
$ref: "#/components/examples/sample3" |
What do you think about the issue where So for example this: ...
examples:
Users:
value:
- $ref: '#/components/examples/User1/value'
- $ref: '#/components/examples/User2/value'
... Renders as this (in JSON): [
{
"id": 1,
"email": "[email protected]",
"name": "John Doe",
"$$ref": "#/components/examples/User1/value"
},
{
"id": 2,
"email": "[email protected]",
"name": "Jane Doe",
"$$ref": "#/components/examples/User2/value"
}
] Notice the Should that be opened as a separate issue than? |
This is not valid usage. examples:
Users:
$ref: '#/components/examples/User1' The
|
Oh, makes sense! Thanks for providing clarity! |
I tried combining multiple $ref elements using allOf, just for the sake of it. Even though I'm getting a Structural error in Swagger editor, the output for examples seems to be correct. For e.g.,
This produces the output value for myTestExample as follows:
Is this an expected behaviour? |
Hi @alexgmin, just like this:
|
I am surprised to find that this ticket is closed. Using references in component A list endpoint which is making use of an enveloped response becomes difficult to create an example for without duplication of the single instance example data which is required for other endpoints. examples:
someResponse:
value:
count: 15
next: null
previous: null
results:
- $ref: '#/components/examples/exampleOne/value'
- $ref: '#/components/examples/exampleTwo/value' |
Not sure if a regression of #4021
This renders the example but also adds the $$ref key
And this one shows the $ref key instead of parsing it (Although the editor shows an error if the $ref doesn't exist):
All of this tested in the swagger editor.
The text was updated successfully, but these errors were encountered: