Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: improve the xml schema to support CDATA(the data does not need to be escaped) #4919

Closed
10 changes: 5 additions & 5 deletions src/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ export const sampleXmlFromSchema = (schema, config={}) => {
}
}

if (additionalProperties === true) {
res[displayName].push({additionalProp: "Anything can be here"})
} else if (additionalProperties) {
res[displayName].push({additionalProp: primitive(additionalProperties)})
if (additionalProperties) {
value = additionalProperties === true || !Object.getOwnPropertyNames(additionalProperties).length ? "Anything can be here" : primitive(additionalProperties)
value = (additionalProperties.xml || xml || {})["x-cdata"] ? {_cdata: value} : value
res[displayName].push({additionalProp: value})
}

if (_attr) {
Expand All @@ -263,7 +263,7 @@ export const sampleXmlFromSchema = (schema, config={}) => {
value = primitive(schema)
}

res[displayName] = _attr ? [{_attr: _attr}, value] : value
res[displayName] = _attr ? xml["x-cdata"] ? {_attr: _attr, _cdata: value} : [{_attr: _attr}, value] : value

return res
}
Expand Down
121 changes: 120 additions & 1 deletion test/mocha/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,19 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})

it("returns value wrapped by `CDATA` when passing {'x-cdata': true}", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<notagname><![CDATA[foo&bar 1<2]]></notagname>"
var definition = {
type: "string",
example: "foo&bar 1<2",
xml: {
"x-cdata": true
}
}

expect(sut(definition)).toEqual(expected)
})

it("sets first enum if provided", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<newtagname>one</newtagname>"
var definition = {
Expand Down Expand Up @@ -1009,7 +1022,27 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})

})
it("returns array with example values wrapped by `CDATA` when passing {'x-cdata': true}", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal><![CDATA[foo&bar]]></animal>\n\t<animal><![CDATA[1<2]]></animal>\n</animals>"
var definition = {
type: "array",
items: {
type: "string",
xml: {
"x-cdata": true,
name: "animal"
}
},
"example": [ "foo&bar", "1<2" ],
xml: {
wrapped: true,
name: "animals"
}
}

expect(sut(definition)).toEqual(expected)
})
})

describe("object", function () {
it("returns object with 2 properties", function () {
Expand Down Expand Up @@ -1407,5 +1440,91 @@ describe("createXMLExample", function () {

expect(sut(definition)).toEqual(expected)
})

it("returns object with example value wrapped by `CDATA` when passing {'x-cdata': true}", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bob>\n\t<foo><![CDATA[&bar]]></foo>\n\t<bar><![CDATA[<2]]></bar>\n</bob>"
var definition = {
type: "object",
properties: {
foo: {
type: "string",
xml: {
"x-cdata": true
}
},
bar:{
type: "string",
xml: {
"x-cdata": true
}
}
},
xml: {
name: "bob"
},
example: {
foo: "&bar",
bar: "<2"
}
}

expect(sut(definition)).toEqual(expected)
})

it("returns object with 2 properties those values wrapped by `CDATA` when passing {'x-cdata': true}", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bob>\n\t<foo><![CDATA[string]]></foo>\n\t<bar><![CDATA[string]]></bar>\n</bob>"
var definition = {
properties: {
foo: {
type: "string",
xml: {
"x-cdata": true
}
},
bar: {
type: "string",
xml: {
"x-cdata": true
}
}
},
xml: {
name: "bob"
}
}

expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props equals `true` and those values wrapped by `CDATA` when the defs passing {'x-cdata': true}", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<additionalProp><![CDATA[Anything can be here]]></additionalProp>\n</animals>"
var definition = {
additionalProperties: true,
xml: {
name: "animals",
wrapped: true,
"x-cdata": true
}
}

expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props those values wrapped by `CDATA` when passing {'x-cdata': true}", function () {
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<additionalProp><![CDATA[string]]></additionalProp>\n</animals>"
var definition = {
additionalProperties: {
type: "string",
xml: {
"x-cdata": true
}
},
xml: {
name: "animals"
}
}

expect(sut(definition)).toEqual(expected)
})
})
})