fix(schema example): xml gen should follow json gen behavior (#6555)

* ref: #6470 
* fixes: #6540
* fixes: #4943 

* add example override option to json
* add example override option to xml
* added basic oneOf and anyOf support
* fix anyof|oneof
* only lift xml to items


Co-authored-by: Tim Lai <timothy.lai@gmail.com>
This commit is contained in:
Mahtis Michel
2020-11-03 19:58:59 +01:00
committed by GitHub
parent 64ae7af565
commit 288c89bdbb
8 changed files with 488 additions and 228 deletions

View File

@@ -0,0 +1,43 @@
openapi: 3.0.0
info:
description: Test API
version: v1
title: Test API
tags:
- name: Test
description: Test API
servers:
- url: /v1
paths:
/test:
post:
tags:
- Test
summary: Test endpoint
description: Test
operationId: postTest
responses:
'200':
description: Returns response
content:
application/xml:
schema:
$ref: '#/components/schemas/test'
components:
schemas:
test:
type: object
properties:
a:
type: string
b:
type: integer
c:
oneOf:
- type: object
- type: array
items:
type: string
- type: boolean
- type: integer
- type: number

View File

@@ -0,0 +1,85 @@
openapi: 3.0.0
info:
description: Test API
version: v1
title: Test API
tags:
- name: Test
description: Test API
servers:
- url: /v1
paths:
/test:
post:
tags:
- Test
summary: Test endpoint
description: Test
operationId: postTest
responses:
'200':
description: Returns response
content:
application/xml:
schema:
$ref: '#/components/schemas/test'
components:
schemas:
test:
type: object
properties:
a:
type: string
b:
type: integer
c:
type: array
items:
$ref: '#/components/schemas/Things'
d:
type: array
items:
anyOf:
- $ref: '#/components/schemas/TextObject'
- $ref: '#/components/schemas/ImageObject'
Things:
type: object
oneOf:
- $ref: '#/components/schemas/TextObject'
- $ref: '#/components/schemas/ImageObject'
TextObject:
required:
- data
type: object
properties:
objectType:
type: string
example: Text
xml:
name: ObjectType
data:
type: string
example: This is a text
xml:
name: Data
description: Contains a text
ImageObject:
required:
- data
type: object
properties:
objectType:
type: string
example: image
xml:
name: ObjectType
data:
type: string
example: This is a image
xml:
name: Data
description: Contains a image

View File

@@ -0,0 +1,20 @@
describe("#4943: XML example not rendered correctly with oneOf", () => {
it("should render integer property correctly", () => {
cy
.visit("/?url=/documents/bugs/4943.yaml")
.get("#operations-Test-postTest")
.click()
.get(".microlight")
.contains("<b>0</b>")
})
it("should render oneOf property correctly", () => {
cy
.visit("/?url=/documents/bugs/4943.yaml")
.get("#operations-Test-postTest")
.click()
.get(".try-out__btn")
.click()
.get(".microlight")
.contains("<c>\n\t</c>")
})
})

View File

@@ -0,0 +1,11 @@
describe("#6540: XML example not rendered correctly with oneOf", () => {
it("should render xml like json", () => {
const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test>\n\t<a>string</a>\n\t<b>0</b>\n\t<c>\n\t\t<ObjectType>Text</ObjectType>\n\t\t<Data>This is a text</Data>\n\t</c>\n\t<c>\n\t\t<ObjectType>image</ObjectType>\n\t\t<Data>This is a image</Data>\n\t</c>\n\t<d>\n\t\t<ObjectType>Text</ObjectType>\n\t\t<Data>This is a text</Data>\n\t</d>\n\t<d>\n\t\t<ObjectType>image</ObjectType>\n\t\t<Data>This is a image</Data>\n\t</d>\n</test>"
cy
.visit("/?url=/documents/bugs/6540.yaml")
.get("#operations-Test-postTest")
.click()
.get(".microlight")
.contains(expected)
})
})

View File

@@ -512,6 +512,26 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})
})
it("should use overrideExample when defined", () => {
const definition = {
type: "object",
properties: {
foo: {
type: "string"
}
},
example: {
foo: null
}
}
const expected = {
foo: "override"
}
expect(sampleFromSchema(definition, {}, expected)).toEqual(expected)
})
})
describe("createXMLExample", function () {
@@ -1337,7 +1357,7 @@ describe("createXMLExample", function () {
})
it("returns object with additional props", function () {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp>string</additionalProp>\n</animals>"
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp1>string</additionalProp1>\n\t<additionalProp2>string</additionalProp2>\n\t<additionalProp3>string</additionalProp3>\n</animals>"
let definition = {
type: "object",
properties: {
@@ -1394,7 +1414,7 @@ describe("createXMLExample", function () {
})
it("returns object with additional props with no type passed", function () {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<additionalProp>string</additionalProp>\n</animals>"
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<additionalProp1>string</additionalProp1>\n\t<additionalProp2>string</additionalProp2>\n\t<additionalProp3>string</additionalProp3>\n</animals>"
let definition = {
additionalProperties: {
type: "string"
@@ -1406,5 +1426,34 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})
it("should use overrideExample when defined", () => {
const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bar>\n\t<foo>override</foo>\n</bar>"
const definition = {
type: "object",
properties: {
foo: {
type: "string",
xml: {
name: "foo"
}
}
},
example: {
foo: null
},
xml: {
name: "bar"
}
}
const overrideExample = {
foo: "override"
}
expect(sut(definition, {}, overrideExample)).toEqual(expected)
})
})
})