Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for usage of XML examples of type string. #738

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@ -292,7 +293,7 @@
});

// Need to handle collaping of folders
it.skip('Should generate collection with collapsing unnecessary folders ' +

Check warning on line 296 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (14.x)

Unexpected skipped mocha test

Check warning on line 296 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Unexpected skipped mocha test

Check warning on line 296 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Unexpected skipped mocha test
multipleFoldersSpec, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, {}, (err, conversionResult) => {
Expand All @@ -304,7 +305,7 @@
done();
});
});
it.skip('Should collapse child and parent folder when parent has only one child' +

Check warning on line 308 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (14.x)

Unexpected skipped mocha test

Check warning on line 308 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Unexpected skipped mocha test

Check warning on line 308 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Unexpected skipped mocha test
multipleFoldersSpec1, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec1, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
Expand All @@ -318,7 +319,7 @@
done();
});
});
it.skip('Should generate collection without creating folders and having only one request' +

Check warning on line 322 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (14.x)

Unexpected skipped mocha test

Check warning on line 322 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Unexpected skipped mocha test

Check warning on line 322 in test/unit/convertV2.test.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Unexpected skipped mocha test
multipleFoldersSpec2, function(done) {
var openapi = fs.readFileSync(multipleFoldersSpec2, 'utf8');
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
Expand Down Expand Up @@ -1107,6 +1108,25 @@
});
});

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
Loading