Skip to content

Commit fedd1d3

Browse files
authored
Merge pull request #738 from postmanlabs/feature/support-xml-string-examples
Added support for usage of XML examples of type string.
2 parents 5210cd3 + c3b2252 commit fedd1d3

File tree

5 files changed

+106
-4
lines changed

5 files changed

+106
-4
lines changed

lib/xmlSchemaFaker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent, resolve
5050
}
5151
}
5252
else if (schema.type === 'object') {
53-
if (resolveTo === 'example' && typeof schemaExample !== 'undefined') {
53+
// Use mentioned example in string directly as example
54+
if (resolveTo === 'example' && typeof schemaExample === 'string') {
55+
return '\n' + schemaExample;
56+
}
57+
else if (resolveTo === 'example' && typeof schemaExample === 'object') {
5458
const elementName = _.get(schema, 'items.xml.name', name || 'element'),
5559
fakedContent = js2xml({ [elementName]: schemaExample }, indentChar);
5660

@@ -95,7 +99,11 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent, resolve
9599

96100
schemaItemsWithXmlProps.xml = schema.xml;
97101

98-
if (resolveTo === 'example' && typeof schemaExample !== 'undefined') {
102+
// Use mentioned example in string directly as example
103+
if (resolveTo === 'example' && typeof schemaExample === 'string') {
104+
return '\n' + schemaExample;
105+
}
106+
else if (resolveTo === 'example' && typeof schemaExample === 'object') {
99107
const fakedContent = js2xml({ [arrayElemName]: schemaExample }, indentChar);
100108

101109
contents = '\n' + indentContent(fakedContent, cIndent);

libV2/xmlSchemaFaker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent, resolve
5050
}
5151
}
5252
else if (schema.type === 'object') {
53-
if (resolveTo === 'example' && typeof schemaExample !== 'undefined') {
53+
// Use mentioned example in string directly as example
54+
if (resolveTo === 'example' && typeof schemaExample === 'string') {
55+
return '\n' + schemaExample;
56+
}
57+
else if (resolveTo === 'example' && typeof schemaExample === 'object') {
5458
const elementName = _.get(schema, 'items.xml.name', name || 'element'),
5559
fakedContent = js2xml({ [elementName]: schemaExample }, indentChar);
5660

@@ -95,7 +99,11 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent, resolve
9599

96100
schemaItemsWithXmlProps.xml = schema.xml;
97101

98-
if (resolveTo === 'example' && typeof schemaExample !== 'undefined') {
102+
// Use mentioned example in string directly as example
103+
if (resolveTo === 'example' && typeof schemaExample === 'string') {
104+
return '\n' + schemaExample;
105+
}
106+
else if (resolveTo === 'example' && typeof schemaExample === 'object') {
99107
const fakedContent = js2xml({ [arrayElemName]: schemaExample }, indentChar);
100108

101109
contents = '\n' + indentContent(fakedContent, cIndent);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
openapi: 3.0.3
2+
info:
3+
title: My API
4+
version: 1.0.0
5+
contact: {}
6+
servers:
7+
- url: "https://api.server.test/v1"
8+
paths:
9+
/test:
10+
post:
11+
summary: /test
12+
description: /test
13+
operationId: test
14+
requestBody:
15+
content:
16+
text/xml:
17+
schema:
18+
type: array
19+
items:
20+
type: object
21+
properties:
22+
issue:
23+
type: string
24+
description: information about the issue
25+
maxLength: 150
26+
action:
27+
type: string
28+
description: what corrective action needs to be taken to resolve the issue.
29+
maxLength: 150
30+
example: |
31+
<Errors>
32+
<error>
33+
<issue>Mandatory field are missing.</issue>
34+
<action>Resend request with valid values, any one of Hello or World.</action>
35+
</error>
36+
</Errors>
37+
responses:
38+
"200":
39+
description: OK
40+
content:
41+
application/json:
42+
examples:
43+
OK:
44+
value:
45+
Data: Postman
46+
tags: []

test/unit/base.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
5555
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
5656
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'),
5757
xmlrequestBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExample.yaml'),
58+
xmlrequestExampleBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExampleWithString.yaml'),
5859
queryParamWithEnumResolveAsExample =
5960
path.join(__dirname, VALID_OPENAPI_PATH, '/query_param_with_enum_resolve_as_example.json'),
6061
formDataParamDescription = path.join(__dirname, VALID_OPENAPI_PATH, '/form_data_param_description.yaml'),
@@ -1255,6 +1256,25 @@ describe('CONVERT FUNCTION TESTS ', function() {
12551256
});
12561257
});
12571258

1259+
it('Should convert xml request body with complete string example correctly', function(done) {
1260+
const openapi = fs.readFileSync(xmlrequestExampleBody, 'utf8');
1261+
Converter.convert({ type: 'string', data: openapi },
1262+
{ schemaFaker: true, requestParametersResolution: 'Example' }, (err, conversionResult) => {
1263+
expect(err).to.be.null;
1264+
expect(conversionResult.result).to.equal(true);
1265+
expect(conversionResult.output[0].data.item[0].request.body.raw)
1266+
.to.equal(`<?xml version="1.0" encoding="UTF-8"?>
1267+
<Errors>
1268+
<error>
1269+
<issue>Mandatory field are missing.</issue>
1270+
<action>Resend request with valid values, any one of Hello or World.</action>
1271+
</error>
1272+
</Errors>
1273+
`);
1274+
done();
1275+
});
1276+
});
1277+
12581278
it('[Github #518]- integer query params with enum values get default value of NaN' +
12591279
descriptionInBodyParams, function(done) {
12601280
var openapi = fs.readFileSync(queryParamWithEnumResolveAsExample, 'utf8');

test/unit/convertV2.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const expect = require('chai').expect,
4848
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
4949
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'),
5050
xmlrequestBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExample.yaml'),
51+
xmlrequestExampleBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExampleWithString.yaml'),
5152
queryParamWithEnumResolveAsExample =
5253
path.join(__dirname, VALID_OPENAPI_PATH, '/query_param_with_enum_resolve_as_example.json'),
5354
formDataParamDescription = path.join(__dirname, VALID_OPENAPI_PATH, '/form_data_param_description.yaml'),
@@ -1107,6 +1108,25 @@ describe('The convert v2 Function', function() {
11071108
});
11081109
});
11091110

1111+
it('Should convert xml request body with complete string example correctly', function(done) {
1112+
const openapi = fs.readFileSync(xmlrequestExampleBody, 'utf8');
1113+
Converter.convertV2({ type: 'string', data: openapi },
1114+
{ schemaFaker: true, parametersResolution: 'Example' }, (err, conversionResult) => {
1115+
expect(err).to.be.null;
1116+
expect(conversionResult.result).to.equal(true);
1117+
expect(conversionResult.output[0].data.item[0].item[0].request.body.raw)
1118+
.to.equal(`<?xml version="1.0" encoding="UTF-8"?>
1119+
<Errors>
1120+
<error>
1121+
<issue>Mandatory field are missing.</issue>
1122+
<action>Resend request with valid values, any one of Hello or World.</action>
1123+
</error>
1124+
</Errors>
1125+
`);
1126+
done();
1127+
});
1128+
});
1129+
11101130
it('[Github #518]- integer query params with enum values get default value of NaN' +
11111131
descriptionInBodyParams, function(done) {
11121132
var openapi = fs.readFileSync(queryParamWithEnumResolveAsExample, 'utf8');

0 commit comments

Comments
 (0)