Skip to content

Commit 9f78464

Browse files
authored
Merge pull request #657 from postmanlabs/fix/issue#11227
Fix/issue#11227
2 parents 89f35d8 + 1922226 commit 9f78464

7 files changed

+732
-19
lines changed

lib/xmlSchemaFaker.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
/* eslint-disable */
22
const _ = require('lodash');
33

4-
function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
4+
function convertSchemaToXML(name, schema, attribute, indentChar, indent) {
55
var tagPrefix = '',
6-
cInden = _.times(inden, _.constant(indentChar)).join('');
6+
cIndent = _.times(indent, _.constant(indentChar)).join('');
77
name = _.get(schema, 'xml.name', name || 'element');
88
if (_.get(schema, 'xml.prefix')) {
9-
tagPrefix = schema.xml.prefix + ':';
9+
tagPrefix = schema.xml.prefix ? `${schema.xml.prefix}:` : '';
1010
}
11-
if (['integer','string'].includes(schema.type)) {
11+
if (['integer','string', 'boolean', 'number'].includes(schema.type)) {
1212
if (schema.type === 'integer') {
1313
actualValue = '(integer)';
1414
}
1515
else if (schema.type === 'string') {
1616
actualValue = '(string)';
1717
}
18+
else if (schema.type === 'boolean') {
19+
actualValue = '(boolean)';
20+
}
21+
else if (schema.type === 'number') {
22+
actualValue = '(number)';
23+
}
1824
if (attribute) {
1925
return actualValue;
2026
}
2127
else {
22-
var retVal = '\n' + cInden + '<' + tagPrefix+name;
28+
var retVal = `\n${cIndent}<${tagPrefix+name}`;
2329
if (_.get(schema, 'xml.namespace')) {
24-
retVal += ' xmlns:' + tagPrefix.slice(0,-1) + '="'+schema.xml.namespace+'"'
30+
retVal += ` xmlns:${tagPrefix.slice(0,-1)}="${schema.xml.namespace}"`
2531
}
26-
retVal += '>' + actualValue + '</' + tagPrefix+name + '>';
32+
retVal += `>${actualValue}</${tagPrefix}${name}>`;
2733
}
2834
}
2935
else if (schema.type === 'object') {
3036
// go through all properties
31-
var retVal = '\n' + cInden + `<${tagPrefix+name}`, propVal, attributes = [], childNodes = '';
37+
var retVal = '\n' + cIndent + `<${tagPrefix}${name}`, propVal, attributes = [], childNodes = '';
3238
if (_.get(schema, 'xml.namespace')) {
33-
retVal += ' xmlns:' + tagPrefix.slice(0,-1) + '="'+schema.xml.namespace+'"'
39+
let formattedTagPrefix = tagPrefix ?
40+
`:${tagPrefix.slice(0,-1)}` :
41+
'';
42+
retVal += ` xmlns${formattedTagPrefix}="${schema.xml.namespace}"`
3443
}
3544
_.forOwn(schema.properties, (value, key) => {
36-
propVal = convertSchemaToXML(key, value, _.get(value, 'xml.attribute'), indentChar, inden + 1);
45+
propVal = convertSchemaToXML(key, value, _.get(value, 'xml.attribute'), indentChar, indent + 1);
3746
if (_.get(value, 'xml.attribute')) {
38-
attributes.push(key + '="' + propVal + '"');
47+
attributes.push(`${key}="${propVal}"`);
3948
}
4049
else {
4150
childNodes += propVal;
@@ -46,17 +55,21 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
4655
}
4756
retVal += '>';
4857
retVal += childNodes;
49-
retVal += '\n</' + name + '>';
58+
retVal += `\n${cIndent}</${tagPrefix}${name}>`;
5059
}
5160
else if (schema.type === 'array') {
5261
// schema.items must be an object
5362
var isWrapped = _.get(schema, 'xml.wrapped'),
54-
extraIndent = isWrapped ? 1 : 0;
55-
var arrayElemName = _.get(schema, 'items.xml.name', name, 'arrayItem');
56-
var contents = convertSchemaToXML(arrayElemName, schema.items, false, indentChar, inden + extraIndent) +
57-
convertSchemaToXML(arrayElemName, schema.items, false, indentChar, inden + extraIndent);
63+
extraIndent = isWrapped ? 1 : 0,
64+
arrayElemName = _.get(schema, 'items.xml.name', name, 'arrayItem'),
65+
schemaItemsWithXmlProps = _.cloneDeep(schema.items),
66+
contents;
67+
68+
schemaItemsWithXmlProps.xml = schema.xml;
69+
contents = convertSchemaToXML(arrayElemName, schemaItemsWithXmlProps, false, indentChar, indent + extraIndent) +
70+
convertSchemaToXML(arrayElemName, schemaItemsWithXmlProps, false, indentChar, indent + extraIndent);
5871
if (isWrapped) {
59-
return '\n' + cInden + '<' + name + '>' + contents + '\n' + cInden + '</' + name + '>';
72+
return `\n${cIndent}<${tagPrefix}${name}>${contents}\n${cIndent}</${tagPrefix}${name}>`;
6073
}
6174
else {
6275
return contents;
@@ -108,4 +121,4 @@ a = convertSchemaToXML('Person',{
108121
}, false, 0);
109122
110123
console.log(a);
111-
*/
124+
*/
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Example REST Service with XML Payloads"
6+
},
7+
"servers": [
8+
{
9+
"url": "localhost:3000"
10+
}
11+
],
12+
"paths": {
13+
"/example": {
14+
"post": {
15+
"responses": {
16+
"200": {
17+
"content": {
18+
"application/xml": {
19+
"schema": {
20+
"$ref": "#/components/schemas/ExampleResponse"
21+
}
22+
}
23+
},
24+
"description": "An example REST service with XML payloads response"
25+
}
26+
},
27+
"description": "An example REST service with XML payloads",
28+
"requestBody": {
29+
"content": {
30+
"application/xml": {
31+
"schema": {
32+
"$ref": "#/components/schemas/ExampleRequest"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"components": {
41+
"schemas": {
42+
"ExampleRequest": {
43+
"type": "object",
44+
"required": [
45+
"requestInteger",
46+
"requestString",
47+
"requestBoolean"
48+
],
49+
"properties": {
50+
"requestInteger": {
51+
"type": "integer"
52+
},
53+
"requestString": {
54+
"type": "string"
55+
},
56+
"requestBoolean": {
57+
"type": "boolean"
58+
},
59+
"requestNumber": {
60+
"type": "number"
61+
}
62+
},
63+
"xml": {
64+
"name": "ExampleXMLRequest",
65+
"prefix": "ex",
66+
"namespace": "urn:ExampleXML"
67+
}
68+
},
69+
"ExampleResponse": {
70+
"type": "object",
71+
"required": [
72+
"responseInteger",
73+
"responseString",
74+
"responseBoolean"
75+
],
76+
"properties": {
77+
"responseInteger": {
78+
"type": "integer"
79+
},
80+
"responseString": {
81+
"type": "string"
82+
},
83+
"responseBoolean": {
84+
"type": "boolean"
85+
},
86+
"responseNumber": {
87+
"type": "number"
88+
}
89+
},
90+
"xml": {
91+
"name": "ExampleXMLResponse",
92+
"prefix": "ex",
93+
"namespace": "urn:ExampleXML"
94+
}
95+
}
96+
},
97+
"securitySchemes": {
98+
"BasicAuth": {
99+
"type": "http",
100+
"scheme": "basic"
101+
}
102+
}
103+
},
104+
"security": [
105+
{
106+
"BasicAuth": []
107+
}
108+
]
109+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Example REST Service with XML Payloads"
6+
},
7+
"servers": [
8+
{
9+
"url": "localhost:3000"
10+
}
11+
],
12+
"paths": {
13+
"/example": {
14+
"post": {
15+
"responses": {
16+
"200": {
17+
"content": {
18+
"application/xml": {
19+
"schema": {
20+
"$ref": "#/components/schemas/ExampleResponse"
21+
}
22+
}
23+
},
24+
"description": "An example REST service with XML payloads response"
25+
}
26+
},
27+
"description": "An example REST service with XML payloads",
28+
"requestBody": {
29+
"content": {
30+
"application/xml": {
31+
"schema": {
32+
"$ref": "#/components/schemas/ExampleRequest"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"components": {
41+
"schemas": {
42+
"ExampleRequest": {
43+
"type": "array",
44+
"items": {
45+
"type": "object",
46+
"properties": {
47+
"requestInteger": {
48+
"type": "integer"
49+
},
50+
"requestString": {
51+
"type": "string"
52+
},
53+
"requestBoolean": {
54+
"type": "boolean"
55+
}
56+
}
57+
},
58+
"xml": {
59+
"name": "ExampleXMLRequest",
60+
"prefix": "ex",
61+
"namespace": "urn:ExampleXML"
62+
}
63+
},
64+
"ExampleResponse": {
65+
"type": "array",
66+
"items": {
67+
"type": "object",
68+
"properties": {
69+
"requestInteger": {
70+
"type": "integer"
71+
},
72+
"requestString": {
73+
"type": "string"
74+
},
75+
"requestBoolean": {
76+
"type": "boolean"
77+
}
78+
}
79+
},
80+
"xml": {
81+
"name": "ExampleXMLResponse",
82+
"prefix": "ex",
83+
"namespace": "urn:ExampleXML"
84+
}
85+
}
86+
},
87+
"securitySchemes": {
88+
"BasicAuth": {
89+
"type": "http",
90+
"scheme": "basic"
91+
}
92+
}
93+
},
94+
"security": [
95+
{
96+
"BasicAuth": []
97+
}
98+
]
99+
}
100+

0 commit comments

Comments
 (0)