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:
43
test/e2e-cypress/static/documents/bugs/4943.yaml
Normal file
43
test/e2e-cypress/static/documents/bugs/4943.yaml
Normal 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
|
||||
85
test/e2e-cypress/static/documents/bugs/6540.yaml
Normal file
85
test/e2e-cypress/static/documents/bugs/6540.yaml
Normal 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
|
||||
20
test/e2e-cypress/tests/bugs/4943.js
Normal file
20
test/e2e-cypress/tests/bugs/4943.js
Normal 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>")
|
||||
})
|
||||
})
|
||||
11
test/e2e-cypress/tests/bugs/6540.js
Normal file
11
test/e2e-cypress/tests/bugs/6540.js
Normal 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)
|
||||
})
|
||||
})
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user