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

Issue with referenced json file example representation on SwaggerUI #4262

Closed
AgentSir opened this issue Feb 27, 2018 · 12 comments
Closed

Issue with referenced json file example representation on SwaggerUI #4262

AgentSir opened this issue Feb 27, 2018 · 12 comments

Comments

@AgentSir
Copy link

Q A
Bug or feature request? Bug
Which Swagger/OpenAPI version? 2.0/3.0.1
Which Swagger-UI version? 3.9.3
How did you install Swagger-UI? I use the sources locally from swagger-ui-master/dist directory
Which browser & version? Chrome
Which operating system? Windows 10

Demonstration API definition

{
    "swagger": "2.0",
    "openapi": "3.0.1",
...
    "responses": {
        "200": {
	    "description": "Customer.",
	        "content": {
		    "application/json": {
		        "schema": {
			    "$ref": "#/components/schemas/Customer",
			    "example": {
			        "$ref": "/assets/examples/models/customers/630432.json"
			    }
			}
		    }
		}
   	    },
...
}  
{
  "id": "630432",
  "name": "test",
  "contactId": "728867",
  "publicId": "0010339970",
  "city": "wien",
  "zipCode": "1234"
}

Expected Behavior

Swagger UI has to display my example as:

{
  "id": "630432",
  "name": "test",
  "contactId": "728867",
  "publicId": "0010339970",
  "city": "wien",
  "zipCode": "1234"
}

Current Behavior

It renders to UI as following:

{
  "id": "630432",
  "name": "test",
  "contactId": "728867",
  "publicId": "0010339970",
  "city": "wien",
  "zipCode": "1234",
  "$$ref": "/assets/examples/models/customers/630432.json"
}

Possible Solution

The issue has been already discussed there: #3638

@hkosova
Copy link
Contributor

hkosova commented Feb 27, 2018

1 ) The example keyword requires an inline example, it does not support $ref.
To point to an external file containing the literal example, you need to use examples (plural) + externalValue, like so:

"content": {
  "application/json": {
    "schema": {
      "$ref": "#/components/schemas/Customer"
    },
    "examples": {
      "customer": {
        "externalValue": "/assets/examples/models/customers/630432.json"
      }
    }
  }

Please note that Swagger UI does not support examples and externalValue yet; it's tracked in #3437.

2 ) You need to remove "swagger": "2.0" from your definition because it's "openapi": "3.0.1".

The fact that $$ref is displayed is still a bug though.

@AgentSir
Copy link
Author

Thank you for suggestions.
I found out next things:
The structure below works fine with SwaggerUI 3.9.3:

"content": {
  "application/json": {
    "schema": {
      "$ref": "#/components/schemas/Customer"
    }
  }
},
"examples": {
  "Customer": {
    "$ref": "/assets/examples/models/customers/630432.json"
  }
}

capture

But if you try to show example as array of objects it still works wrong:

"content": {
  "application/json": {
    "schema": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/Customer"
      }
    }
  }
},
"examples": {
  "Customers": [{
    "$ref": "/assets/examples/models/customers/630432.json"
  }]
}

capture

@heldersepu
Copy link
Contributor

heldersepu commented Feb 27, 2018

@AgentSir do you have a full definition you can share?
If it's large please use https://gist.github.com/ and share a link

@AgentSir
Copy link
Author

The full definition of last code version:
https://gist.github.com/AgentSir/fef6dc84bcbc60f51e0affc2b0cccb12

@heldersepu
Copy link
Contributor

There is definitely an error popping with that definition:
http://petstore.swagger.io/?docExpansion=full&url=https://gist.githubusercontent.com/AgentSir/fef6dc84bcbc60f51e0affc2b0cccb12/raw/5e9b46816606e6c2df3ad41fcb34222fb32ec82f/swagger.json
But I think the error points to something else...

To narrow down the problem can you try an examples with the $ref is on the same definition?
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#example-object-example

And also try with a ref to a full path like they show on the example
$ref: http://example.org/petapi-examples/openapi.json#/components/examples/name-example

@webron
Copy link
Contributor

webron commented Feb 27, 2018

The API definition is invalid - it's a mix of 2.0 and 3.0, and not really a valid structure of either.
Please use the http://editor.swagger.io/ to check for the errors and fix them, before we can address any rendering issues.

@shockey
Copy link
Contributor

shockey commented Mar 7, 2018

Closing due to inactivity.

This is simply to keep our issue tracker clean - feel free to comment if there are any further thoughts or concerns, and we'll be happy to reopen this issue.

@shockey shockey closed this as completed Mar 7, 2018
@erikbozic
Copy link

We're experiencing the same issue.
Using $ref in example renders the $$ref property in the UI.

Any ETA on the fix, or support for examples?

@webron
Copy link
Contributor

webron commented Mar 17, 2018

@erikbozic it would be better to file a new ticket.

@shockey
Copy link
Contributor

shockey commented Mar 20, 2018

@erikbozic, we fixed an example $$ref issue in Friday's release, can you test with the latest version?

@erikbozic
Copy link

Yes, it now works with 3.13.0 !
Thank you!

@lock
Copy link

lock bot commented Jul 2, 2019

Locking due to inactivity.

This is done to avoid resurrecting old issues and bumping long threads with new, possibly unrelated content.

If you think you're experiencing something similar to what you've found here: please open a new issue, follow the template, and reference this issue in your report.

Thanks!

@lock lock bot locked and limited conversation to collaborators Jul 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants