Skip to content

Commit

Permalink
Merge pull request #738 from postmanlabs/feature/support-xml-string-e…
Browse files Browse the repository at this point in the history
…xamples

Added support for usage of XML examples of type string.
  • Loading branch information
VShingala committed Jun 27, 2023
2 parents 5210cd3 + c3b2252 commit fedd1d3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/xmlSchemaFaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent, resolve
}
}
else if (schema.type === 'object') {
if (resolveTo === 'example' && typeof schemaExample !== 'undefined') {
// Use mentioned example in string directly as example
if (resolveTo === 'example' && typeof schemaExample === 'string') {
return '\n' + schemaExample;
}
else if (resolveTo === 'example' && typeof schemaExample === 'object') {
const elementName = _.get(schema, 'items.xml.name', name || 'element'),
fakedContent = js2xml({ [elementName]: schemaExample }, indentChar);

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

schemaItemsWithXmlProps.xml = schema.xml;

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

contents = '\n' + indentContent(fakedContent, cIndent);
Expand Down
12 changes: 10 additions & 2 deletions libV2/xmlSchemaFaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent, resolve
}
}
else if (schema.type === 'object') {
if (resolveTo === 'example' && typeof schemaExample !== 'undefined') {
// Use mentioned example in string directly as example
if (resolveTo === 'example' && typeof schemaExample === 'string') {
return '\n' + schemaExample;
}
else if (resolveTo === 'example' && typeof schemaExample === 'object') {
const elementName = _.get(schema, 'items.xml.name', name || 'element'),
fakedContent = js2xml({ [elementName]: schemaExample }, indentChar);

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

schemaItemsWithXmlProps.xml = schema.xml;

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

contents = '\n' + indentContent(fakedContent, cIndent);
Expand Down
46 changes: 46 additions & 0 deletions test/data/valid_openapi/xmlExampleWithString.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
openapi: 3.0.3
info:
title: My API
version: 1.0.0
contact: {}
servers:
- url: "https://api.server.test/v1"
paths:
/test:
post:
summary: /test
description: /test
operationId: test
requestBody:
content:
text/xml:
schema:
type: array
items:
type: object
properties:
issue:
type: string
description: information about the issue
maxLength: 150
action:
type: string
description: what corrective action needs to be taken to resolve the issue.
maxLength: 150
example: |
<Errors>
<error>
<issue>Mandatory field are missing.</issue>
<action>Resend request with valid values, any one of Hello or World.</action>
</error>
</Errors>
responses:
"200":
description: OK
content:
application/json:
examples:
OK:
value:
Data: Postman
tags: []
20 changes: 20 additions & 0 deletions test/unit/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('CONVERT FUNCTION TESTS ', function() {
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'),
xmlrequestBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExample.yaml'),
xmlrequestExampleBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExampleWithString.yaml'),
queryParamWithEnumResolveAsExample =
path.join(__dirname, VALID_OPENAPI_PATH, '/query_param_with_enum_resolve_as_example.json'),
formDataParamDescription = path.join(__dirname, VALID_OPENAPI_PATH, '/form_data_param_description.yaml'),
Expand Down Expand Up @@ -1255,6 +1256,25 @@ describe('CONVERT FUNCTION TESTS ', function() {
});
});

it('Should convert xml request body with complete string example correctly', function(done) {
const openapi = fs.readFileSync(xmlrequestExampleBody, 'utf8');
Converter.convert({ type: 'string', data: openapi },
{ schemaFaker: true, requestParametersResolution: 'Example' }, (err, conversionResult) => {
expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(conversionResult.output[0].data.item[0].request.body.raw)
.to.equal(`<?xml version="1.0" encoding="UTF-8"?>
<Errors>
<error>
<issue>Mandatory field are missing.</issue>
<action>Resend request with valid values, any one of Hello or World.</action>
</error>
</Errors>
`);
done();
});
});

it('[Github #518]- integer query params with enum values get default value of NaN' +
descriptionInBodyParams, function(done) {
var openapi = fs.readFileSync(queryParamWithEnumResolveAsExample, 'utf8');
Expand Down
20 changes: 20 additions & 0 deletions test/unit/convertV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const expect = require('chai').expect,
valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'),
petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'),
xmlrequestBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExample.yaml'),
xmlrequestExampleBody = path.join(__dirname, VALID_OPENAPI_PATH, '/xmlExampleWithString.yaml'),
queryParamWithEnumResolveAsExample =
path.join(__dirname, VALID_OPENAPI_PATH, '/query_param_with_enum_resolve_as_example.json'),
formDataParamDescription = path.join(__dirname, VALID_OPENAPI_PATH, '/form_data_param_description.yaml'),
Expand Down Expand Up @@ -1107,6 +1108,25 @@ describe('The convert v2 Function', function() {
});
});

it('Should convert xml request body with complete string example correctly', function(done) {
const openapi = fs.readFileSync(xmlrequestExampleBody, 'utf8');
Converter.convertV2({ type: 'string', data: openapi },
{ schemaFaker: true, parametersResolution: 'Example' }, (err, conversionResult) => {
expect(err).to.be.null;
expect(conversionResult.result).to.equal(true);
expect(conversionResult.output[0].data.item[0].item[0].request.body.raw)
.to.equal(`<?xml version="1.0" encoding="UTF-8"?>
<Errors>
<error>
<issue>Mandatory field are missing.</issue>
<action>Resend request with valid values, any one of Hello or World.</action>
</error>
</Errors>
`);
done();
});
});

it('[Github #518]- integer query params with enum values get default value of NaN' +
descriptionInBodyParams, function(done) {
var openapi = fs.readFileSync(queryParamWithEnumResolveAsExample, 'utf8');
Expand Down

0 comments on commit fedd1d3

Please sign in to comment.