question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using $ref to refer to schema that has $ref in example treats $ref as a property

See original GitHub issue

Q&A (please complete the following information)

  • OS: macOS
  • Browser: chrome
  • Version: 66.0.3359.181
  • Method of installation: homebrew
  • Swagger-UI version: https://editor.swagger.io/ and swagger-editor-dist@^3.3.1 using swagger-ui@^3.12.0
  • Swagger/OpenAPI version: OpenAPI 3.0.1

Content & configuration

Example Swagger/OpenAPI definition:

openapi: '3.0.1'
info:
  title: 'Title'
  version: 1.0.0
components:
  schemas:
    object:
      type: object
      example:
        a:
          $ref: '#/components/schemas/object/properties/a/example'
      properties:
        a:
          type: string
          example: 'aString'
paths:
  '/path':
      post:
        requestBody:
          required: true
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/object'
        responses:
          '201':
            description: 'description'

Swagger-UI configuration options:

I don’t know the configuration. This bug happens on https://editor.swagger.io/ and also on swagger-ui-watcher.

Describe the bug you’re encountering

To reproduce…

Steps to reproduce the behavior:

  1. Go to https://editor.swagger.io/
  2. Paste in the Swagger/OpenAPI definition yaml above.
  3. Expand the POST /path route
  4. Paste in the yaml again. This bug seems to only happen when you update the yaml. So it might be a bug with swagger-editor instead.
  5. Look a the rendered swagger-ui on the right.
  6. The JSON example for the request body has a bug. It treats the $ref as a property of the JSON.

This is what you should see:

{
  "a": {
    "$ref": "#/components/schemas/object/properties/a/example"
  }
}

Expected behavior

The JSON should be:

{
  "a": "aString"
}

Screenshots

image image

Additional context or thoughts

If you go to the Model, it will show example: aString, which is correct.

If you embed the schema within the requestBody schema manually, everything works as expected:

openapi: '3.0.1'
info:
  title: 'Title'
  version: 1.0.0
components:
  schemas:
    object:
      type: object
      example:
        a:
          $ref: '#/components/schemas/object/properties/a/example'
      properties:
        a:
          type: string
          example: 'aString'
paths:
  '/path':
      post:
        requestBody:
          required: true
          content:
            application/json:
              schema:
                type: object
                example:
                  a:
                    $ref: '#/components/schemas/object/properties/a/example'
                properties:
                  a:
                    type: string
                    example: 'aString'
        responses:
          '201':
            description: 'description'

Result: image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shockeycommented, Aug 25, 2018

Thanks, @webron - closing this 😄

1reaction
webroncommented, Aug 23, 2018

Sorry for the delay here. The way it works right now is as expected. As @shockey mentioned, there’s no support for $ref for examples inside the Schema Object. As such, it should treat the $ref as the example itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using $ref - Swagger
To reference a definition, use the $ref keyword: $ref: 'reference to definition'. For example, suppose you have the following schema object, which you...
Read more >
Structuring a complex schema — Understanding JSON ...
A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's...
Read more >
json - Can a property of an allOf item contain a $ref to another ...
Your schema is mostly right, although you've set an $id in a subschema. The "$id" keyword defines a URI for the schema, and...
Read more >
How to use JSON references ($refs) - Redocly
How to use JSON references ($refs) ; a multi-file structure; re-use of schemas or other content ; empty (to refer to the current...
Read more >
17.7.0 API Reference - joi.dev
First, a schema is constructed using the provided types and constraints: const schema = Joi.object({ a: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found