Skip to content

Commit 40da5c4

Browse files
committed
Issue#11227 - Resolving boolean elements correctly and fixing closing tag when prefix is provided
Adding test scenarios: - When prefix is provided - When prefix is not provided
1 parent 9c86011 commit 40da5c4

File tree

4 files changed

+276
-5
lines changed

4 files changed

+276
-5
lines changed

lib/xmlSchemaFaker.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
88
if (_.get(schema, 'xml.prefix')) {
99
tagPrefix = schema.xml.prefix + ':';
1010
}
11-
if (['integer','string'].includes(schema.type)) {
11+
if (['integer','string', 'boolean'].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+
}
1821
if (attribute) {
1922
return actualValue;
2023
}
@@ -30,7 +33,10 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
3033
// go through all properties
3134
var retVal = '\n' + cInden + `<${tagPrefix+name}`, propVal, attributes = [], childNodes = '';
3235
if (_.get(schema, 'xml.namespace')) {
33-
retVal += ' xmlns:' + tagPrefix.slice(0,-1) + '="'+schema.xml.namespace+'"'
36+
let formattedTagPrefix = tagPrefix ?
37+
`:${tagPrefix.slice(0,-1)}` :
38+
'';
39+
retVal += ` xmlns${formattedTagPrefix}="${schema.xml.namespace}"`
3440
}
3541
_.forOwn(schema.properties, (value, key) => {
3642
propVal = convertSchemaToXML(key, value, _.get(value, 'xml.attribute'), indentChar, inden + 1);
@@ -46,7 +52,7 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
4652
}
4753
retVal += '>';
4854
retVal += childNodes;
49-
retVal += '\n</' + name + '>';
55+
retVal += `\n</${tagPrefix+name}>`;
5056
}
5157
else if (schema.type === 'array') {
5258
// schema.items must be an object
@@ -108,4 +114,4 @@ a = convertSchemaToXML('Person',{
108114
}, false, 0);
109115
110116
console.log(a);
111-
*/
117+
*/
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
},
60+
"xml": {
61+
"name": "ExampleXMLRequest",
62+
"prefix": "ex",
63+
"namespace": "urn:ExampleXML"
64+
}
65+
},
66+
"ExampleResponse": {
67+
"type": "object",
68+
"required": [
69+
"responseInteger",
70+
"responseString",
71+
"responseBoolean"
72+
],
73+
"properties": {
74+
"responseInteger": {
75+
"type": "integer"
76+
},
77+
"responseString": {
78+
"type": "string"
79+
},
80+
"responseBoolean": {
81+
"type": "boolean"
82+
}
83+
},
84+
"xml": {
85+
"name": "ExampleXMLResponse",
86+
"prefix": "ex",
87+
"namespace": "urn:ExampleXML"
88+
}
89+
}
90+
},
91+
"securitySchemes": {
92+
"BasicAuth": {
93+
"type": "http",
94+
"scheme": "basic"
95+
}
96+
}
97+
},
98+
"security": [
99+
{
100+
"BasicAuth": []
101+
}
102+
]
103+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
},
60+
"xml": {
61+
"name": "ExampleXMLRequest",
62+
"namespace": "urn:ExampleXML"
63+
}
64+
},
65+
"ExampleResponse": {
66+
"type": "object",
67+
"required": [
68+
"responseInteger",
69+
"responseString",
70+
"responseBoolean"
71+
],
72+
"properties": {
73+
"responseInteger": {
74+
"type": "integer"
75+
},
76+
"responseString": {
77+
"type": "string"
78+
},
79+
"responseBoolean": {
80+
"type": "boolean"
81+
}
82+
},
83+
"xml": {
84+
"name": "ExampleXMLResponse",
85+
"namespace": "urn:ExampleXML"
86+
}
87+
}
88+
},
89+
"securitySchemes": {
90+
"BasicAuth": {
91+
"type": "http",
92+
"scheme": "basic"
93+
}
94+
}
95+
},
96+
"security": [
97+
{
98+
"BasicAuth": []
99+
}
100+
]
101+
}

test/unit/base.test.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ describe('CONVERT FUNCTION TESTS ', function() {
6363
specWithAuthApiKey = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthApiKey.yaml'),
6464
specWithAuthDigest = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthDigest.yaml'),
6565
specWithAuthOauth1 = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthOauth1.yaml'),
66-
specWithAuthBasic = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthBasic.yaml');
66+
specWithAuthBasic = path.join(__dirname, VALID_OPENAPI_PATH + '/specWithAuthBasic.yaml'),
67+
xmlRequestAndResponseBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBody.json'),
68+
xmlRequestAndResponseBodyNoPrefix =
69+
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyNoPrefix.json');
6770

6871

6972
it('Should add collection level auth with type as `bearer`' +
@@ -1380,6 +1383,64 @@ describe('CONVERT FUNCTION TESTS ', function() {
13801383
});
13811384
});
13821385
});
1386+
1387+
it('Should convert and resolve xml bodies correctly when prefix is provided', function(done) {
1388+
var openapi = fs.readFileSync(xmlRequestAndResponseBody, 'utf8');
1389+
Converter.convert(
1390+
{ type: 'string', data: openapi },
1391+
{ schemaFaker: true },
1392+
(err, conversionResult) => {
1393+
const resultantRequestBody = conversionResult.output[0].data.item[0].request.body.raw,
1394+
resultantResponseBody = conversionResult.output[0].data.item[0].response[0].body,
1395+
expectedRequestBody = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n' +
1396+
'<ex:ExampleXMLRequest xmlns:ex=\"urn:ExampleXML\">\n' +
1397+
' <requestInteger>(integer)</requestInteger>\n' +
1398+
' <requestString>(string)</requestString>\n' +
1399+
' <requestBoolean>(boolean)</requestBoolean>\n' +
1400+
'</ex:ExampleXMLRequest>',
1401+
expectedResponseBody = '<ex:ExampleXMLResponse xmlns:ex=\"urn:ExampleXML\">\n' +
1402+
' <responseInteger>(integer)</responseInteger>\n' +
1403+
' <responseString>(string)</responseString>\n' +
1404+
' <responseBoolean>(boolean)</responseBoolean>\n' +
1405+
'</ex:ExampleXMLResponse>';
1406+
expect(err).to.be.null;
1407+
expect(conversionResult.result).to.equal(true);
1408+
expect(conversionResult.output.length).to.equal(1);
1409+
expect(resultantRequestBody).to.equal(expectedRequestBody);
1410+
expect(resultantResponseBody).to.equal(expectedResponseBody);
1411+
done();
1412+
}
1413+
);
1414+
});
1415+
1416+
it('Should convert and resolve xml bodies correctly when prefix is not provided', function(done) {
1417+
var openapi = fs.readFileSync(xmlRequestAndResponseBodyNoPrefix, 'utf8');
1418+
Converter.convert(
1419+
{ type: 'string', data: openapi },
1420+
{ schemaFaker: true },
1421+
(err, conversionResult) => {
1422+
const resultantRequestBody = conversionResult.output[0].data.item[0].request.body.raw,
1423+
resultantResponseBody = conversionResult.output[0].data.item[0].response[0].body,
1424+
expectedRequestBody = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n' +
1425+
'<ExampleXMLRequest xmlns=\"urn:ExampleXML\">\n' +
1426+
' <requestInteger>(integer)</requestInteger>\n' +
1427+
' <requestString>(string)</requestString>\n' +
1428+
' <requestBoolean>(boolean)</requestBoolean>\n' +
1429+
'</ExampleXMLRequest>',
1430+
expectedResponseBody = '<ExampleXMLResponse xmlns=\"urn:ExampleXML\">\n' +
1431+
' <responseInteger>(integer)</responseInteger>\n' +
1432+
' <responseString>(string)</responseString>\n' +
1433+
' <responseBoolean>(boolean)</responseBoolean>\n' +
1434+
'</ExampleXMLResponse>';
1435+
expect(err).to.be.null;
1436+
expect(conversionResult.result).to.equal(true);
1437+
expect(conversionResult.output.length).to.equal(1);
1438+
expect(resultantRequestBody).to.equal(expectedRequestBody);
1439+
expect(resultantResponseBody).to.equal(expectedResponseBody);
1440+
done();
1441+
}
1442+
);
1443+
});
13831444
});
13841445

13851446
describe('Converting swagger 2.0 files', function() {

0 commit comments

Comments
 (0)