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

fix: Swagger 2.0 Response.examples #5464

Merged
merged 1 commit into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/core/components/response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,18 @@ export default class Response extends React.Component {
})
}
} else {
sampleResponse = schema ? getSampleSchema(schema.toJS(), activeContentType, {
includeReadOnly: true,
includeWriteOnly: true // writeOnly has no filtering effect in swagger 2.0
}) : null
if(response.getIn(["examples", activeContentType])) {
sampleResponse = response.getIn(["examples", activeContentType])
} else {
sampleResponse = schema ? getSampleSchema(
schema.toJS(),
activeContentType,
{
includeReadOnly: true,
includeWriteOnly: true // writeOnly has no filtering effect in swagger 2.0
}
) : null
}
}

let example = getExampleComponent( sampleResponse, HighlightCode )
Expand Down
37 changes: 37 additions & 0 deletions test/e2e-cypress/static/documents/bugs/5458.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
swagger: "2.0"
info:
title: test
version: 1.0.0
paths:
/foo1:
get:
summary: Response without a schema
produces:
- application/json
responses:
200:
description: Successful response
examples:
application/json:
foo: custom value
/foo2:
get:
summary: Response with schema
produces:
- application/json
responses:
200:
description: Successful response
schema:
$ref: '#/definitions/Foo'
examples:
application/json:
foo: custom value

definitions:
Foo:
type: object
properties:
foo:
type: string
example: bar
18 changes: 18 additions & 0 deletions test/e2e-cypress/tests/bugs/5458.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// http://github.com/swagger-api/swagger-ui/issues/5458

describe("#5458: Swagger 2.0 `Response.examples` mappings", () => {
it("should render a custom example when a schema is not defined", () => {
cy.visit("/?url=/documents/bugs/5458.yaml")
.get("#operations-default-get_foo1")
.click()
.get(".model-example .highlight-code")
.contains("custom value")
})
it("should render a custom example when a schema is defined", () => {
cy.visit("/?url=/documents/bugs/5458.yaml")
.get("#operations-default-get_foo2")
.click()
.get(".model-example .highlight-code")
.contains("custom value")
})
})