Skip to content

Commit 23524fe

Browse files
committed
Adding scenarios for schemas when type is array
1 parent 40da5c4 commit 23524fe

File tree

5 files changed

+436
-7
lines changed

5 files changed

+436
-7
lines changed

lib/xmlSchemaFaker.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
66
cInden = _.times(inden, _.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
}
1111
if (['integer','string', 'boolean'].includes(schema.type)) {
1212
if (schema.type === 'integer') {
@@ -31,7 +31,7 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
3131
}
3232
else if (schema.type === 'object') {
3333
// go through all properties
34-
var retVal = '\n' + cInden + `<${tagPrefix+name}`, propVal, attributes = [], childNodes = '';
34+
var retVal = '\n' + cInden + `<${tagPrefix}${name}`, propVal, attributes = [], childNodes = '';
3535
if (_.get(schema, 'xml.namespace')) {
3636
let formattedTagPrefix = tagPrefix ?
3737
`:${tagPrefix.slice(0,-1)}` :
@@ -52,17 +52,19 @@ function convertSchemaToXML(name, schema, attribute, indentChar, inden) {
5252
}
5353
retVal += '>';
5454
retVal += childNodes;
55-
retVal += `\n</${tagPrefix+name}>`;
55+
retVal += `\n${cInden}</${tagPrefix+name}>`;
5656
}
5757
else if (schema.type === 'array') {
5858
// schema.items must be an object
5959
var isWrapped = _.get(schema, 'xml.wrapped'),
6060
extraIndent = isWrapped ? 1 : 0;
6161
var arrayElemName = _.get(schema, 'items.xml.name', name, 'arrayItem');
62-
var contents = convertSchemaToXML(arrayElemName, schema.items, false, indentChar, inden + extraIndent) +
63-
convertSchemaToXML(arrayElemName, schema.items, false, indentChar, inden + extraIndent);
62+
var schemaItemsWithXmlProps = _.cloneDeep(schema.items);
63+
schemaItemsWithXmlProps.xml = schema.xml;
64+
var contents = convertSchemaToXML(arrayElemName, schemaItemsWithXmlProps, false, indentChar, inden + extraIndent) +
65+
convertSchemaToXML(arrayElemName, schemaItemsWithXmlProps, false, indentChar, inden + extraIndent);
6466
if (isWrapped) {
65-
return '\n' + cInden + '<' + name + '>' + contents + '\n' + cInden + '</' + name + '>';
67+
return `\n${cInden}<${tagPrefix}${name}>${contents}\n${cInden}</${tagPrefix}${name}>`;
6668
}
6769
else {
6870
return contents;
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+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
"namespace": "urn:ExampleXML"
61+
}
62+
},
63+
"ExampleResponse": {
64+
"type": "array",
65+
"items": {
66+
"type": "object",
67+
"properties": {
68+
"requestInteger": {
69+
"type": "integer"
70+
},
71+
"requestString": {
72+
"type": "string"
73+
},
74+
"requestBoolean": {
75+
"type": "boolean"
76+
}
77+
}
78+
},
79+
"xml": {
80+
"name": "ExampleXMLResponse",
81+
"namespace": "urn:ExampleXML"
82+
}
83+
}
84+
},
85+
"securitySchemes": {
86+
"BasicAuth": {
87+
"type": "http",
88+
"scheme": "basic"
89+
}
90+
}
91+
},
92+
"security": [
93+
{
94+
"BasicAuth": []
95+
}
96+
]
97+
}
98+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
"wrapped": true,
62+
"namespace": "urn:ExampleXML"
63+
}
64+
},
65+
"ExampleResponse": {
66+
"type": "array",
67+
"items": {
68+
"type": "object",
69+
"properties": {
70+
"requestInteger": {
71+
"type": "integer"
72+
},
73+
"requestString": {
74+
"type": "string"
75+
},
76+
"requestBoolean": {
77+
"type": "boolean"
78+
}
79+
}
80+
},
81+
"xml": {
82+
"name": "ExampleXMLResponse",
83+
"prefix": "ex",
84+
"wrapped": true,
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+
}
102+

0 commit comments

Comments
 (0)