swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Examples referenced with $ref are not rendered correctly

Open alexgmin opened this issue 8 years ago • 21 comments

Not sure if a regression of https://github.com/swagger-api/swagger-ui/issues/4021

This renders the example but also adds the $$ref key

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'
              example:
                $ref: '#/components/examples/WelcomeExample'
components:
  schemas:
    Welcome:
      type: object
      properties:
        message:
          type: string
  examples:
    WelcomeExample:
      value:
        api_identifier: act_12345

And this one shows the $ref key instead of parsing it (Although the editor shows an error if the $ref doesn't exist):

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'
              example:
                $ref: '#/components/examples/WelcomeExample'
components:
  schemas:
    Welcome:
      type: object
      properties:
        message:
          type: string
  examples:
    WelcomeExample:
      value:
        api_identifier: act_12345

All of this tested in the swagger editor.

alexgmin avatar Oct 05 '18 11:10 alexgmin

Yep, I see that too in 3.19.2 and quite a few releases before that

lrtherond avatar Oct 05 '18 23:10 lrtherond

In #4021, @webron states that

The spec does not support references for example fields, only examples. This means that any value of example will be rendered as-is

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

sasha-borodin avatar Oct 07 '18 23:10 sasha-borodin

I think $ref should work in that case too. Clearly, passing a 10-KB piece of JSON example as a string is very problematic.

lrtherond avatar Oct 08 '18 15:10 lrtherond

@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 avatar Oct 10 '18 01:10 webron

@webron, thanks for the response! I think there are two issues:

  1. The implementation both a) resolves the $ref, and b) renders an erroneous artifact of $$ref. I think that consistency needs to be achieved. Either $refs are not supported (and displayed literally), or they are supported (in which case, rendering $$ref is a bug that needs to be fixed).
  2. Since the spec isn't explicit about the matter of resolving $refs inside examples, I would like to make a case for resolution instead of literal rendering. I think this behavior is intuitively expected by a number of users as well as some on the swagger-ui team (see @shockey's comment in #4931). What's the most appropriate channel through which to make such a case or feature request?

Thank you again.

sasha-borodin avatar Oct 10 '18 01:10 sasha-borodin

I would like add another argument to the case for resolution. Is the current behavior for schemas. As a developer trying to define an API, I don't see why I can have nested schema references but not nested example references.

From my point of view, what is wrong in this example is the value key being rendered. Why can I reuse schemas but not examples?

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

alexgmin avatar Oct 10 '18 09:10 alexgmin

Encountering the same issue. Is anyone working on it?

dkrantsberg avatar Jan 03 '19 20:01 dkrantsberg

Been running into this lately as well. Any news?

qbedard avatar Feb 21 '19 23:02 qbedard

I'm picking this up - as mentioned in https://github.com/swagger-api/swagger-ui/issues/4931#issuecomment-428358133, we shouldn't be resolving example values at all.

shockey avatar Apr 05 '19 20:04 shockey

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:

  1. Arrays is what desperately needs loads of data,
  2. but arrays can only consume example and not examples,
  3. which is actually the only option to decouple definitions and data.
  4. And this nonsense behavior we totally owe to some API spec, sent from above.

Did I get it right?

OnkelTem avatar Mar 24 '20 03:03 OnkelTem

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.

webron avatar Mar 24 '20 17:03 webron

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 avatar Jun 09 '20 00:06 carlos-soto

@carlos-soto you can propose changes to the OpenAPI Specification here: https://github.com/OAI/OpenAPI-Specification/issues

hkosova avatar Jun 10 '20 07:06 hkosova

Would be nice in addition to support arrays in example:

responses:
  "200":
    content:
      application/json:
        example:
          - $ref: "#/components/examples/sample1"
          - $ref: "#/components/examples/sample2"
          - $ref: "#/components/examples/sample3"

dmak avatar Jun 12 '20 11:06 dmak

@dmak you can propose changes to the OpenAPI Specification syntax constructs here: https://github.com/OAI/OpenAPI-Specification/issues

As of OAS 3.0.3, multiple examples require the use of the examples keyword:

responses:
  "200":
    content:
      application/json:
        examples:
          sample1:
            $ref: "#/components/examples/sample1"
          sample2:
            $ref: "#/components/examples/sample2"
          sample3:
            $ref: "#/components/examples/sample3"

hkosova avatar Jun 12 '20 14:06 hkosova

What do you think about the issue where $ref used in examples (plural, which is valid, and part of the OpenAPI specification as opposed to inside example (singular)) is dereferenced properly, but also causes an artifact of $$ref being added to the dereferenced object?

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 "$$ref": "#/components/examples/User*/value" as the last property. This seems to be an issue regardless what's going on with example (singular) also mentioned in https://github.com/swagger-api/swagger-ui/issues/4924#issuecomment-428411720

Should that be opened as a separate issue than?

BenceSzalai avatar Nov 02 '20 00:11 BenceSzalai

@BenceSzalai

What do you think about the issue where $ref used in examples (plural, which is valid, and part of the OpenAPI specification as opposed to inside example (singular)) is dereferenced properly, but also causes an artifact of $$ref being added to the dereferenced object?

...
examples:
  Users:
    value:
      - $ref: '#/components/examples/User1/value'
      - $ref: '#/components/examples/User2/value'
...

This is not valid usage. $ref is supported only under examples.<name>, that is:

examples:
  Users:
    $ref: '#/components/examples/User1'

The value keyword is an analog of example (singular) and requires an inline example value.

Should that be opened as a separate issue than?

#5625

hkosova avatar Nov 17 '20 18:11 hkosova

Oh, makes sense! Thanks for providing clarity!

BenceSzalai avatar Nov 17 '20 18:11 BenceSzalai

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.,

examples:
 myTestExample:
  $ref: '#/components/examples/testExample'
  
components:
 examples:
  testExample:
   allOf:
    - $ref: '#/components/examples/data1'
    - $ref: '#/components/examples/data2'
  data1:
   value:
    'key1': 'value1'
  data2:
   value:
    'key2': 'value2'

This produces the output value for myTestExample as follows:

{
 'key1': 'value1',
 'key2': 'value2'
}

Is this an expected behaviour?

random-developer-7 avatar Mar 10 '22 14:03 random-developer-7

Hi @alexgmin, I'v tried many solutions, the only one that seems to work is using 'examples' key and not 'example', then put a single example (you should name it) and put the $ref under it.

just like this:

paths:
  /hello:
    get:
      description: 'Get welcome message.'
      responses:
        '200':
          description: 'welcome message'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Welcome'
              examples:
                WelcomeExample1: # you can give as an example name any name you want
                  $ref: '#/components/examples/WelcomeExample'

bodo79 avatar Mar 29 '22 12:03 bodo79