From 91b9dc46ae0dbe165bb31477d2c210ee168ba78f Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Sat, 13 Jul 2019 00:11:45 -0500 Subject: [PATCH 1/2] fix: Swagger 2.0 `Response.examples` --- src/core/components/response.jsx | 16 ++++++-- .../static/documents/bugs/5458.yaml | 37 +++++++++++++++++++ test/e2e-cypress/tests/bugs/5458.js | 18 +++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 test/e2e-cypress/static/documents/bugs/5458.yaml create mode 100644 test/e2e-cypress/tests/bugs/5458.js diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx index 563b803f968..cf404d07a9d 100644 --- a/src/core/components/response.jsx +++ b/src/core/components/response.jsx @@ -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 ) diff --git a/test/e2e-cypress/static/documents/bugs/5458.yaml b/test/e2e-cypress/static/documents/bugs/5458.yaml new file mode 100644 index 00000000000..2ab2b4a2e26 --- /dev/null +++ b/test/e2e-cypress/static/documents/bugs/5458.yaml @@ -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 diff --git a/test/e2e-cypress/tests/bugs/5458.js b/test/e2e-cypress/tests/bugs/5458.js new file mode 100644 index 00000000000..da92e4cc880 --- /dev/null +++ b/test/e2e-cypress/tests/bugs/5458.js @@ -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") + }) +}) From f237888928ef3fa23ec4301a746672194abb8f3f Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Sat, 13 Jul 2019 00:36:29 -0500 Subject: [PATCH 2/2] stringify results --- src/core/components/response.jsx | 3 ++- test/e2e-cypress/tests/bugs/5458.js | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx index cf404d07a9d..6cfa2246bfe 100644 --- a/src/core/components/response.jsx +++ b/src/core/components/response.jsx @@ -10,7 +10,7 @@ const getExampleComponent = ( sampleResponse, HighlightCode ) => { sampleResponse !== undefined && sampleResponse !== null ) { return
- +
} return null @@ -138,6 +138,7 @@ export default class Response extends React.Component { } else { if(response.getIn(["examples", activeContentType])) { sampleResponse = response.getIn(["examples", activeContentType]) + debugger } else { sampleResponse = schema ? getSampleSchema( schema.toJS(), diff --git a/test/e2e-cypress/tests/bugs/5458.js b/test/e2e-cypress/tests/bugs/5458.js index da92e4cc880..3c0ccb67ff4 100644 --- a/test/e2e-cypress/tests/bugs/5458.js +++ b/test/e2e-cypress/tests/bugs/5458.js @@ -1,18 +1,22 @@ // http://github.com/swagger-api/swagger-ui/issues/5458 +const expectedValue = `{ + "foo": "custom value" +}` + 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") + .contains(expectedValue) }) 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") + .contains(expectedValue) }) })