fix(xml): render example with oneOf/anyOf (#8206)
* test(e2e): render xml example with oneOf/anyOf
This commit is contained in:
@@ -605,18 +605,19 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
|
||||
}
|
||||
|
||||
const getXmlSampleSchema = (schema, config, exampleOverride) => {
|
||||
if (schema && (!schema.xml || !schema.xml.name)) {
|
||||
schema.xml = schema.xml || {}
|
||||
|
||||
if (schema && !schema.xml) {
|
||||
schema.xml = {}
|
||||
}
|
||||
if (schema && !schema.xml.name) {
|
||||
if (!schema.$$ref && (schema.type || schema.items || schema.properties || schema.additionalProperties)) {
|
||||
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated; root element name is undefined -->"
|
||||
}
|
||||
if (schema.$$ref) {
|
||||
let match = schema.$$ref.match(/\S*\/(\S+)$/)
|
||||
schema.xml.name = match[1]
|
||||
} else if (schema.type || schema.items || schema.properties || schema.additionalProperties) {
|
||||
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated; root element name is undefined -->"
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return memoizedCreateXMLExample(schema, config, exampleOverride)
|
||||
}
|
||||
|
||||
|
||||
309
test/e2e-cypress/static/documents/features/oas3-xml.json
Normal file
309
test/e2e-cypress/static/documents/features/oas3-xml.json
Normal file
@@ -0,0 +1,309 @@
|
||||
{
|
||||
"openapi": "3.0.0",
|
||||
"info": {
|
||||
"version": "1.0.0",
|
||||
"title": "XML definition with oneOf & anyOf",
|
||||
"description": "Definition to cover multiple XML examples"
|
||||
},
|
||||
"paths": {
|
||||
"/foo": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with oneOf for /foo request and mediaType text/xml",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfOne"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfTwo"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfThree"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfFour"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"application/xml": {
|
||||
"schema": {
|
||||
"description": "fallback XML schema with mediaType application/xml",
|
||||
"$ref": "#/components/schemas/OneOfTwo"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with mediaType text/xml",
|
||||
"$ref": "#/components/schemas/foobarResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/bar": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with anyOf for /bar request and mediaType text/xml",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfOne"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfTwo"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfThree"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfFour"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"application/xml": {
|
||||
"schema": {
|
||||
"description": "fallback XML schema with mediaType application/xml",
|
||||
"$ref": "#/components/schemas/OneOfTwo"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with mediaType text/xml",
|
||||
"$ref": "#/components/schemas/foobarResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/foobar": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with oneOf for /foobar request and mediaType application/xml",
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfOne"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfTwo"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfThree"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfFour"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "fallback XML schema with mediaType text/xml",
|
||||
"$ref": "#/components/schemas/OneOfThree"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with mediaType application/xml",
|
||||
"$ref": "#/components/schemas/foobarResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/barfoo": {
|
||||
"post": {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with anyOf for /barfoo request and mediaType application/xml",
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfOne"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfTwo"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfThree"
|
||||
},
|
||||
{
|
||||
"$ref": "#/components/schemas/OneOfFour"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"text/xml": {
|
||||
"schema": {
|
||||
"description": "fallback XML schema with mediaType text/xml",
|
||||
"$ref": "#/components/schemas/OneOfThree"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/xml": {
|
||||
"schema": {
|
||||
"description": "XML schema with mediaType application/xml",
|
||||
"$ref": "#/components/schemas/foobarResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"OneOfOne": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Body": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"fooOne": {}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv",
|
||||
"namespace": "http://schemas.xmlsoap.org/soap/envelope/",
|
||||
"name": "Envelope"
|
||||
}
|
||||
},
|
||||
"OneOfTwo": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Body": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"fooTwo": {}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv",
|
||||
"namespace": "http://schemas.xmlsoap.org/soap/envelope/",
|
||||
"name": "Envelope"
|
||||
}
|
||||
},
|
||||
"OneOfThree": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Body": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"fooThree": {}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv",
|
||||
"namespace": "http://schemas.xmlsoap.org/soap/envelope/",
|
||||
"name": "Envelope"
|
||||
}
|
||||
},
|
||||
"OneOfFour": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Body": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"fooFour": {}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv",
|
||||
"namespace": "http://schemas.xmlsoap.org/soap/envelope/",
|
||||
"name": "Envelope"
|
||||
}
|
||||
},
|
||||
"foobarResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Body": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"foobarResObj": {
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"xml": {
|
||||
"prefix": "soapenv",
|
||||
"namespace": "http://schemas.xmlsoap.org/soap/envelope/",
|
||||
"name": "Envelope"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
70
test/e2e-cypress/tests/features/oas3-xml.js
Normal file
70
test/e2e-cypress/tests/features/oas3-xml.js
Normal file
@@ -0,0 +1,70 @@
|
||||
describe("XML schema rendering examples", () => {
|
||||
it("Should render RequestBody example value when schema contains `oneOf` for mediaType `text/xml`", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_foo")
|
||||
.click()
|
||||
.get("label > .content-type-wrapper > .content-type option:selected")
|
||||
.should("have.text", "text/xml")
|
||||
.get(".body-param__example")
|
||||
.should("contains.text", "<fooOne>")
|
||||
})
|
||||
it("Should render RequestBody example value when schema contains `anyOf` for mediaType `text/xml`", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_bar")
|
||||
.click()
|
||||
.get("label > .content-type-wrapper > .content-type option:selected")
|
||||
.should("have.text", "text/xml")
|
||||
.get(".body-param__example")
|
||||
.should("contains.text", "<fooOne>")
|
||||
})
|
||||
it("Should render RequestBody example value when schema contains `oneOf` for mediaType `application/xml`", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_foobar")
|
||||
.click()
|
||||
.get("label > .content-type-wrapper > .content-type option:selected")
|
||||
.should("have.text", "application/xml")
|
||||
.get(".body-param__example")
|
||||
.should("contains.text", "<fooOne>")
|
||||
})
|
||||
it("Should render RequestBody example value when schema contains `anyOf` for mediaType `application/xml`", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_barfoo")
|
||||
.click()
|
||||
.get("label > .content-type-wrapper > .content-type option:selected")
|
||||
.should("have.text", "application/xml")
|
||||
.get(".body-param__example")
|
||||
.should("contains.text", "<fooOne>")
|
||||
})
|
||||
it("Should render RequestBody example value when switching mediaType to `text/xml` with singular content schema", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_barfoo")
|
||||
.click()
|
||||
.get("label > .content-type-wrapper > .content-type")
|
||||
.select("text/xml")
|
||||
.get(".body-param__example")
|
||||
.should("contains.text", "<fooThree>")
|
||||
})
|
||||
it("Should render RequestBody example value when switching mediaType to `application/xml` with singular content schema", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_foo")
|
||||
.click()
|
||||
.get("label > .content-type-wrapper > .content-type")
|
||||
.select("application/xml")
|
||||
.get(".body-param__example")
|
||||
.should("contains.text", "<fooTwo>")
|
||||
})
|
||||
it("Should render Response example value for mediaType `application/xml`", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_foo")
|
||||
.click()
|
||||
.get(".response-col_description > .model-example")
|
||||
.should("contains.text", "<foobarResObj>")
|
||||
})
|
||||
it("Should render Response example value for mediaType `text/xml`", () => {
|
||||
cy.visit("?url=/documents/features/oas3-xml.json")
|
||||
.get("#operations-default-post_foobar")
|
||||
.click()
|
||||
.get(".response-col_description > .model-example")
|
||||
.should("contains.text", "<foobarResObj>")
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user