Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/core/plugins/json-schema-2020-12-samples/fn/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,31 @@ export const sampleFromSchemaGeneric = (
return value
}

const normalizeXMLContent = (obj) => {
if (Array.isArray(obj)) {
if (obj.length === 0) return null
// If every item in the array is an _attr object (no actual content), collapse
// to a plain object so the xml package renders a self-closing tag with attributes
const nonAttrItems = obj.filter(
(item) => !(item && typeof item === "object" && item._attr)
)
if (nonAttrItems.length === 0) {
const merged = {}
obj.forEach((item) => Object.assign(merged, item._attr))
return { _attr: merged }
}
return obj.map(normalizeXMLContent)
}
if (obj !== null && typeof obj === "object") {
const result = {}
for (const key of Object.keys(obj)) {
result[key] = normalizeXMLContent(obj[key])
}
return result
}
return obj
}

export const createXMLExample = (schema, config, o) => {
const json = sampleFromSchemaGeneric(schema, config, o, true)
if (!json) {
Expand All @@ -544,7 +569,7 @@ export const createXMLExample = (schema, config, o) => {
if (typeof json === "string") {
return json
}
return XML(json, { declaration: true, indent: "\t" })
return XML(normalizeXMLContent(json), { declaration: true, indent: "\t" })
}

export const sampleFromSchema = (schema, config, o) => {
Expand Down
27 changes: 26 additions & 1 deletion src/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,38 @@ export const inferSchema = (thing) => {
return thing // Hopefully this will have something schema like in it... `type` for example
}

const normalizeXMLContent = (obj) => {
if (Array.isArray(obj)) {
if (obj.length === 0) return null
// If every item in the array is an _attr object (no actual content), collapse
// to a plain object so the xml package renders a self-closing tag with attributes
const nonAttrItems = obj.filter(
(item) => !(item && typeof item === "object" && item._attr)
)
if (nonAttrItems.length === 0) {
const merged = {}
obj.forEach((item) => Object.assign(merged, item._attr))
return { _attr: merged }
}
return obj.map(normalizeXMLContent)
}
if (obj !== null && typeof obj === "object") {
const result = {}
for (const key of Object.keys(obj)) {
result[key] = normalizeXMLContent(obj[key])
}
return result
}
return obj
}

export const createXMLExample = (schema, config, o) => {
const json = sampleFromSchemaGeneric(schema, config, o, true)
if (!json) { return }
if(typeof json === "string") {
return json
}
return XML(json, { declaration: true, indent: "\t" })
return XML(normalizeXMLContent(json), { declaration: true, indent: "\t" })
}


Expand Down
2 changes: 1 addition & 1 deletion test/e2e-cypress/e2e/bugs/4943.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ describe("#4943: XML example not rendered correctly with oneOf", () => {
.get(".try-out__btn")
.click()
.get(".microlight")
.contains("<c>\n\t</c>")
.contains("<c/>")
})
})
2 changes: 1 addition & 1 deletion test/e2e-cypress/e2e/bugs/6627.cy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("#6627: XML example when defined as array", () => {
it("should render xml like json", () => {
const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Users>\n\t<User id=\"123\" name=\"bob\">\n\t</User>\n\t<User id=\"456\" name=\"jane\">\n\t</User>\n</Users>"
const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Users>\n\t<User id=\"123\" name=\"bob\"/>\n\t<User id=\"456\" name=\"jane\"/>\n</Users>"
cy
.visit("/?url=/documents/bugs/6627.yaml")
.get("#operations-default-get_users")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e-cypress/e2e/features/oas3-xml.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ describe("XML schema rendering examples", () => {
.get("#operations-default-post_foo")
.click()
.get(".response-col_description > .model-example")
.should("contains.text", "<foobarResObj>")
.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>")
.should("contains.text", "<foobarResObj/>")
})
})
10 changes: 5 additions & 5 deletions test/unit/core/plugins/json-schema-2020-12-samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2954,7 +2954,7 @@ describe("createXMLExample", function () {
})
it("should use exampleOverride for attr too", () => {
const expected =
'<?xml version="1.0" encoding="UTF-8"?>\n<aliens test="probe">\n</aliens>'
'<?xml version="1.0" encoding="UTF-8"?>\n<aliens test="probe"/>\n'
const definition = {
type: "object",
properties: {
Expand Down Expand Up @@ -3041,8 +3041,8 @@ describe("createXMLExample", function () {
}

const expected = `<?xml version="1.0" encoding="UTF-8"?>
<test arrayOfStrings="string" arrayOfArrays="UnknownTypeArray UnknownTypeArray UnknownTypeArray" arrayOfContainsObject="UnknownTypeObject UnknownTypeObject UnknownTypeObject">
</test>`
<test arrayOfStrings="string" arrayOfArrays="UnknownTypeArray UnknownTypeArray UnknownTypeArray" arrayOfContainsObject="UnknownTypeObject UnknownTypeObject UnknownTypeObject"/>
`

expect(sut(definition)).toEqual(expected)
})
Expand All @@ -3069,8 +3069,8 @@ describe("createXMLExample", function () {
}

const expected = `<?xml version="1.0" encoding="UTF-8"?>
<test object="UnknownTypeObject">
</test>`
<test object="UnknownTypeObject"/>
`

expect(sut(definition)).toEqual(expected)
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2405,7 +2405,7 @@ describe("createXMLExample", function () {
expect(sut(definition, {}, expected)).toEqual(expected)
})
it("should use exampleOverride for attr too", () => {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens test=\"probe\">\n</aliens>"
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens test=\"probe\"/>\n"
let definition = {
type: "object",
properties: {
Expand Down