mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
functions changed:
\t1) convertToPmHeaders. \t2) convertToPmBody. \t3) convertToPmResponse.
This commit is contained in:
176
lib/util.js
176
lib/util.js
@@ -96,6 +96,7 @@ module.exports = {
|
||||
// list of predefined schemas in components
|
||||
components: {},
|
||||
options: {},
|
||||
// enviVariables: [],
|
||||
|
||||
/**
|
||||
* Adds the neccessary server variables to the collection
|
||||
@@ -408,44 +409,23 @@ module.exports = {
|
||||
return helper;
|
||||
},
|
||||
|
||||
|
||||
getResponseHeaders: function(contentTypes, responseHeaders) {
|
||||
var headerArray = [{
|
||||
key: 'Content-Type',
|
||||
value: contentTypes ? (contentTypes[0]) : TEXT_PLAIN
|
||||
}],
|
||||
header;
|
||||
|
||||
if (!responseHeaders) { return headerArray; }
|
||||
|
||||
_.forOwn(responseHeaders, (value, key) => {
|
||||
header = {
|
||||
key: key,
|
||||
value: this.options.schemaFaker ? safeSchemaFaker(value.schema || {}, this.components) : '',
|
||||
description: value.description || ''
|
||||
};
|
||||
headerArray.push(header);
|
||||
});
|
||||
return headerArray;
|
||||
},
|
||||
|
||||
getResponseBody: function(openapi, contentObj) {
|
||||
convertToPmResponseBody: function(contentObj) {
|
||||
var responseBody;
|
||||
if (!contentObj) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (contentObj[APP_JSON]) {
|
||||
responseBody = this.getBodyData(openapi, contentObj[APP_JSON]);
|
||||
responseBody = this.convertToPmBodyData(contentObj[APP_JSON]);
|
||||
}
|
||||
else if (contentObj[APP_XML]) {
|
||||
responseBody = this.getBodyData(openapi, contentObj[APP_XML]);
|
||||
responseBody = this.convertToPmBodyData(contentObj[APP_XML]);
|
||||
}
|
||||
else if (contentObj[APP_JS]) {
|
||||
responseBody = this.getBodyData(openapi, contentObj[APP_XML]);
|
||||
responseBody = this.convertToPmBodyData(contentObj[APP_XML]);
|
||||
}
|
||||
else if (contentObj[TEXT_PLAIN]) {
|
||||
responseBody = this.getBodyData(openapi, contentObj[TEXT_PLAIN]);
|
||||
responseBody = this.convertToPmBodyData(contentObj[TEXT_PLAIN]);
|
||||
}
|
||||
|
||||
return JSON.stringify(responseBody, null, 4);
|
||||
@@ -462,7 +442,11 @@ module.exports = {
|
||||
|
||||
_.forEach(localParams, (param) => {
|
||||
tempParam = param;
|
||||
|
||||
// tempParam = { param: param, stage: 'local' };
|
||||
// if (tempParam.$ref) {
|
||||
// var refArr = tempparam.$ref.split('/').slice(2);
|
||||
// tempParam = { param: openapi.components.parameters[refArr[1]], stage: 'global' };
|
||||
// }
|
||||
if (tempParam.in === 'query') {
|
||||
params.query.push(tempParam);
|
||||
}
|
||||
@@ -477,7 +461,7 @@ module.exports = {
|
||||
return params;
|
||||
},
|
||||
|
||||
getExampleData: function(openapi, exampleObj) {
|
||||
getExampleData: function(exampleObj) {
|
||||
var example,
|
||||
exampleKey;
|
||||
|
||||
@@ -491,7 +475,7 @@ module.exports = {
|
||||
return example;
|
||||
},
|
||||
|
||||
getBodyData: function(openapi, bodyObj) {
|
||||
convertToPmBodyData: function(bodyObj) {
|
||||
var bodyData = '';
|
||||
|
||||
// This part is to remove format:binary from any string-type properties
|
||||
@@ -500,7 +484,7 @@ module.exports = {
|
||||
bodyData = this.options.schemaFaker ? safeSchemaFaker(bodyObj.schema || {}, this.components) : '';
|
||||
}
|
||||
else if (bodyObj.hasOwnProperty('examples')) {
|
||||
bodyData = this.getExampleData(openapi, bodyObj.examples);
|
||||
bodyData = this.getExampleData(bodyObj.examples);
|
||||
// take one of the examples as the body and not all
|
||||
}
|
||||
else if (bodyObj.hasOwnProperty('example')) {
|
||||
@@ -532,6 +516,15 @@ module.exports = {
|
||||
}
|
||||
|
||||
modifiedUrl += '?';
|
||||
// need to set name based on param stage
|
||||
// if (param.stage === 'global') {
|
||||
// this.enviVariables.push({
|
||||
// key: 'url',
|
||||
// value: url,
|
||||
// enabled: true,
|
||||
// type: 'text'
|
||||
// });
|
||||
// }
|
||||
_.forEach(queryParameters, (param) => {
|
||||
// check for existence of schema
|
||||
if (param.hasOwnProperty('schema')) {
|
||||
@@ -611,41 +604,34 @@ module.exports = {
|
||||
return modifiedRequestObj;
|
||||
},
|
||||
|
||||
addHeaders: function(postmanItemObj, headers) {
|
||||
var modifiedPostmanItemObj = postmanItemObj,
|
||||
fakeData,
|
||||
convertToPmHeader: function(header) {
|
||||
var fakeData,
|
||||
reqHeader;
|
||||
if (headers === []) {
|
||||
return modifedPostmanItemObj;
|
||||
|
||||
if (header.hasOwnProperty('schema')) {
|
||||
fakeData = this.options.schemaFaker ? safeSchemaFaker(header.schema || {}, this.components) : '';
|
||||
}
|
||||
else {
|
||||
fakeData = '';
|
||||
}
|
||||
|
||||
_.forEach(headers, (header) => {
|
||||
if (header.hasOwnProperty('schema')) {
|
||||
fakeData = options.schemaFaker ? safeSchemaFaker(header.schema || {}, this.components) : '';
|
||||
}
|
||||
else {
|
||||
fakeData = '';
|
||||
}
|
||||
|
||||
reqHeader = new sdk.Header({
|
||||
key: header.name,
|
||||
value: fakeData
|
||||
});
|
||||
reqHeader.description = header.description;
|
||||
modifiedPostmanItemObj.request.addHeader(reqHeader);
|
||||
reqHeader = new sdk.Header({
|
||||
key: header.name,
|
||||
value: fakeData
|
||||
});
|
||||
reqHeader.description = header.description;
|
||||
|
||||
return modifiedPostmanItemObj;
|
||||
return reqHeader;
|
||||
},
|
||||
|
||||
addRequestBody: function(postmanItemObj, requestBody, openapi) {
|
||||
var modifiedPostmanItemObj = postmanItemObj,
|
||||
contentObj, // content is required
|
||||
convertToPmBody: function(requestBody) {
|
||||
var contentObj, // content is required
|
||||
bodyData,
|
||||
param,
|
||||
paramArray = [],
|
||||
updateOptions = {},
|
||||
reqBody = new sdk.RequestBody(),
|
||||
contentHeader,
|
||||
rDataMode;
|
||||
|
||||
if (!requestBody) {
|
||||
@@ -658,7 +644,7 @@ module.exports = {
|
||||
// handling for the urlencoded media type
|
||||
if (contentObj.hasOwnProperty(URLENCODED)) {
|
||||
rDataMode = 'urlencoded';
|
||||
bodyData = this.getBodyData(openapi, contentObj[URLENCODED]);
|
||||
bodyData = this.convertToPmBodyData(contentObj[URLENCODED]);
|
||||
// create query parameters and add it to the request body object
|
||||
_.forOwn(bodyData, (value, key) => {
|
||||
if (typeof value === 'object') { value = JSON.stringify(value); }
|
||||
@@ -675,17 +661,17 @@ module.exports = {
|
||||
};
|
||||
|
||||
// add a content type header for each media type for the request body
|
||||
modifiedPostmanItemObj.request.addHeader(new sdk.Header({
|
||||
contentHeader = new sdk.Header({
|
||||
key: 'Content-Type',
|
||||
value: URLENCODED
|
||||
}));
|
||||
});
|
||||
|
||||
// update the request body with the options
|
||||
reqBody.update(updateOptions);
|
||||
}
|
||||
else if (contentObj.hasOwnProperty(FORM_DATA)) {
|
||||
rDataMode = 'formdata';
|
||||
bodyData = this.getBodyData(openapi, contentObj[FORM_DATA]);
|
||||
bodyData = this.convertToPmBodyData(contentObj[FORM_DATA]);
|
||||
// create the form parameters and add it to the request body object
|
||||
_.forOwn(bodyData, (value, key) => {
|
||||
if (typeof value === 'object') { value = JSON.stringify(value); }
|
||||
@@ -701,10 +687,10 @@ module.exports = {
|
||||
formdata: paramArray
|
||||
};
|
||||
// add a content type header for the pertaining media type
|
||||
modifiedPostmanItemObj.request.addHeader(new sdk.Header({
|
||||
contentHeader = new sdk.Header({
|
||||
key: 'Content-Type',
|
||||
value: FORM_DATA
|
||||
}));
|
||||
});
|
||||
// update the request body
|
||||
reqBody.update(updateOptions);
|
||||
}
|
||||
@@ -719,44 +705,50 @@ module.exports = {
|
||||
else if (contentObj.hasOwnProperty(TEXT_PLAIN)) { bodyType = TEXT_PLAIN; }
|
||||
else if (contentObj.hasOwnProperty(TEXT_XML)) { bodyType = TEXT_XML; }
|
||||
|
||||
bodyData = this.getBodyData(openapi, contentObj[bodyType]);
|
||||
bodyData = this.convertToPmBodyData(contentObj[bodyType]);
|
||||
|
||||
updateOptions = {
|
||||
mode: rDataMode,
|
||||
raw: JSON.stringify(bodyData, null, 4)
|
||||
};
|
||||
|
||||
modifiedPostmanItemObj.request.addHeader(new sdk.Header({
|
||||
contentHeader = new sdk.Header({
|
||||
key: 'Content-Type',
|
||||
value: bodyType
|
||||
}));
|
||||
});
|
||||
|
||||
reqBody.update(updateOptions);
|
||||
}
|
||||
|
||||
modifiedPostmanItemObj.request.body = reqBody;
|
||||
return modifiedPostmanItemObj;
|
||||
return {
|
||||
body: reqBody,
|
||||
contentHeader: contentHeader
|
||||
};
|
||||
},
|
||||
|
||||
addResponse: function(postmanItemObj, responses, openapi) {
|
||||
var modifedPostmanItemObj = postmanItemObj,
|
||||
response;
|
||||
|
||||
if (!responses) {
|
||||
return postmanItemObj;
|
||||
convertToPmResponse: function(response, code) {
|
||||
var responseHeaders = [],
|
||||
header;
|
||||
_.forOwn(response.headers, (value, key) => {
|
||||
if (key !== 'Content-Type') {
|
||||
header = value;
|
||||
header.name = key;
|
||||
responseHeaders.push(this.convertToPmHeader(header));
|
||||
}
|
||||
});
|
||||
if (response.content && Object.keys(response.content).length > 0) {
|
||||
responseHeaders.push({ name: 'Content-Type', value: Object.keys(response.content)[0] });
|
||||
}
|
||||
else {
|
||||
responseHeaders.push({ name: 'Content-Type', value: TEXT_PLAIN });
|
||||
}
|
||||
|
||||
_.forOwn(responses, (value, code) => {
|
||||
response = new sdk.Response({
|
||||
name: value.description,
|
||||
code: code === 'default' ? 500 : Number(code),
|
||||
header: this.getResponseHeaders(Object.keys(value.content || {}), value.headers),
|
||||
body: this.getResponseBody(openapi, value.content)
|
||||
});
|
||||
modifedPostmanItemObj.responses.add(response);
|
||||
return new sdk.Response({
|
||||
name: response.description,
|
||||
code: code === 'default' ? 500 : Number(code),
|
||||
header: responseHeaders,
|
||||
body: this.convertToPmResponseBody(response.content)
|
||||
});
|
||||
|
||||
return modifedPostmanItemObj;
|
||||
},
|
||||
|
||||
// function to convert an openapi path item to postman item
|
||||
@@ -774,7 +766,8 @@ module.exports = {
|
||||
item,
|
||||
serverObj,
|
||||
displayUrl,
|
||||
reqUrl = '/' + operationItem.path;
|
||||
reqUrl = '/' + operationItem.path,
|
||||
pmBody;
|
||||
|
||||
// handling path templating in request url if any
|
||||
reqUrl = reqUrl.replace(/{/g, ':').replace(/}/g, '');
|
||||
@@ -822,7 +815,7 @@ module.exports = {
|
||||
// using the auth helper
|
||||
if (authHelper.type === 'api-key') {
|
||||
if (authHelper.properties.in === 'header') {
|
||||
item = this.addHeaders(item, [authHelper.properties]);
|
||||
item.request.addHeader(this.convertToPmHeader(authHelper.properties));
|
||||
item.request.auth = {
|
||||
type: 'noauth'
|
||||
};
|
||||
@@ -844,11 +837,24 @@ module.exports = {
|
||||
|
||||
item.request.url.variables = this.convertPathVariables('method', pathVarArray, reqParams.path);
|
||||
|
||||
item = this.addHeaders(item, reqParams.header);
|
||||
// adding headers to request from reqParam
|
||||
_.forEach(reqParams.header, (header) => {
|
||||
item.request.addHeader(this.convertToPmHeader(header));
|
||||
});
|
||||
|
||||
item = this.addRequestBody(item, reqBody, openapi);
|
||||
// adding request body and content type header
|
||||
if (reqBody) {
|
||||
pmBody = this.convertToPmBody(reqBody);
|
||||
item.request.body = pmBody.body;
|
||||
item.request.addHeader(pmBody.contentHeader);
|
||||
}
|
||||
|
||||
item = this.addResponse(item, operation.responses, openapi);
|
||||
// adding responses to request item
|
||||
if (operation.responses) {
|
||||
_.forOwn(operation.responses, (response, code) => {
|
||||
item.responses.add(this.convertToPmResponse(response, code));
|
||||
});
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user