Skip to content

Commit 9749a47

Browse files
authored
fix: Swagger 2.0 Response.examples (via #5464)
1 parent eaa1f4a commit 9749a47

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

src/core/components/response.jsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,18 @@ export default class Response extends React.Component {
136136
})
137137
}
138138
} else {
139-
sampleResponse = schema ? getSampleSchema(schema.toJS(), activeContentType, {
140-
includeReadOnly: true,
141-
includeWriteOnly: true // writeOnly has no filtering effect in swagger 2.0
142-
}) : null
139+
if(response.getIn(["examples", activeContentType])) {
140+
sampleResponse = response.getIn(["examples", activeContentType])
141+
} else {
142+
sampleResponse = schema ? getSampleSchema(
143+
schema.toJS(),
144+
activeContentType,
145+
{
146+
includeReadOnly: true,
147+
includeWriteOnly: true // writeOnly has no filtering effect in swagger 2.0
148+
}
149+
) : null
150+
}
143151
}
144152

145153
let example = getExampleComponent( sampleResponse, HighlightCode )
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
swagger: "2.0"
2+
info:
3+
title: test
4+
version: 1.0.0
5+
paths:
6+
/foo1:
7+
get:
8+
summary: Response without a schema
9+
produces:
10+
- application/json
11+
responses:
12+
200:
13+
description: Successful response
14+
examples:
15+
application/json:
16+
foo: custom value
17+
/foo2:
18+
get:
19+
summary: Response with schema
20+
produces:
21+
- application/json
22+
responses:
23+
200:
24+
description: Successful response
25+
schema:
26+
$ref: '#/definitions/Foo'
27+
examples:
28+
application/json:
29+
foo: custom value
30+
31+
definitions:
32+
Foo:
33+
type: object
34+
properties:
35+
foo:
36+
type: string
37+
example: bar

test/e2e-cypress/tests/bugs/5458.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// http://github.com/swagger-api/swagger-ui/issues/5458
2+
3+
describe("#5458: Swagger 2.0 `Response.examples` mappings", () => {
4+
it("should render a custom example when a schema is not defined", () => {
5+
cy.visit("/?url=/documents/bugs/5458.yaml")
6+
.get("#operations-default-get_foo1")
7+
.click()
8+
.get(".model-example .highlight-code")
9+
.contains("custom value")
10+
})
11+
it("should render a custom example when a schema is defined", () => {
12+
cy.visit("/?url=/documents/bugs/5458.yaml")
13+
.get("#operations-default-get_foo2")
14+
.click()
15+
.get(".model-example .highlight-code")
16+
.contains("custom value")
17+
})
18+
})

0 commit comments

Comments
 (0)