fix(sample-gen): should return xml literal example (#6822)
This commit is contained in:
@@ -214,6 +214,9 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
|
|||||||
// generate xml sample recursively for array case
|
// generate xml sample recursively for array case
|
||||||
if(type === "array") {
|
if(type === "array") {
|
||||||
if (!Array.isArray(sample)) {
|
if (!Array.isArray(sample)) {
|
||||||
|
if(typeof sample === "string") {
|
||||||
|
return sample
|
||||||
|
}
|
||||||
sample = [sample]
|
sample = [sample]
|
||||||
}
|
}
|
||||||
const itemSchema = schema
|
const itemSchema = schema
|
||||||
@@ -239,6 +242,10 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
|
|||||||
|
|
||||||
// generate xml sample recursively for object case
|
// generate xml sample recursively for object case
|
||||||
if(type === "object") {
|
if(type === "object") {
|
||||||
|
// case literal example
|
||||||
|
if(typeof sample === "string") {
|
||||||
|
return sample
|
||||||
|
}
|
||||||
for (let propName in sample) {
|
for (let propName in sample) {
|
||||||
if (!sample.hasOwnProperty(propName)) {
|
if (!sample.hasOwnProperty(propName)) {
|
||||||
continue
|
continue
|
||||||
@@ -382,7 +389,9 @@ export const inferSchema = (thing) => {
|
|||||||
export const createXMLExample = (schema, config, o) => {
|
export const createXMLExample = (schema, config, o) => {
|
||||||
const json = sampleFromSchemaGeneric(schema, config, o, true)
|
const json = sampleFromSchemaGeneric(schema, config, o, true)
|
||||||
if (!json) { return }
|
if (!json) { return }
|
||||||
|
if(typeof json === "string") {
|
||||||
|
return json
|
||||||
|
}
|
||||||
return XML(json, { declaration: true, indent: "\t" })
|
return XML(json, { declaration: true, indent: "\t" })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1115,6 +1115,27 @@ describe("createXMLExample", function () {
|
|||||||
|
|
||||||
expect(sut(definition, {}, [{ notalien: "test" }])).toEqual(expected)
|
expect(sut(definition, {}, [{ notalien: "test" }])).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
it("should return literal example", () => {
|
||||||
|
let expected = "<notaliens>\n\t\n\t<dog>0</dog>\n</notaliens>"
|
||||||
|
let definition = {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
properties: {
|
||||||
|
alien: {
|
||||||
|
type: "string"
|
||||||
|
},
|
||||||
|
dog: {
|
||||||
|
type: "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
xml: {
|
||||||
|
name: "aliens"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(sut(definition, {}, expected)).toEqual(expected)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("object", function () {
|
describe("object", function () {
|
||||||
@@ -1562,6 +1583,25 @@ describe("createXMLExample", function () {
|
|||||||
|
|
||||||
expect(sut(definition, {}, { alien: "test", dog: 1 })).toEqual(expected)
|
expect(sut(definition, {}, { alien: "test", dog: 1 })).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
it("should return literal example", () => {
|
||||||
|
let expected = "<notaliens>\n\t\n\t<dog>0</dog>\n</notaliens>"
|
||||||
|
let definition = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
alien: {
|
||||||
|
type: "string"
|
||||||
|
},
|
||||||
|
dog: {
|
||||||
|
type: "integer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
xml: {
|
||||||
|
name: "aliens"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(sut(definition, {}, expected)).toEqual(expected)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user