Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ _.forEach(endpointsParsed, function (endpointParsed, i) {
//GENERATOR-------------------------------- */
let endpointsPostman = [];
const endpoints = require('./src/generator/endpoints.js')(endpointsParsed);
const cloneDeep = _.cloneDeep;

_.forEach(endpoints, function (endpoint, i) {
for (let index = 0; index < endpoint.count; index++) {
_.forEach(endpoints, function (originalEndpoint, i) {
for (let index = 0; index < originalEndpoint.count; index++) {
let endpoint = cloneDeep(originalEndpoint);
endpoint = require('./src/generator/testStatus.js')(endpoint);
endpoint = require('./src/generator/testBody.js')(endpoint, configurationFile);
endpoint = require('./src/generator/contentType.js')(endpoint);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi2postman",
"version": "2.2.4",
"version": "2.2.5",
"description": "openapi2postman",
"bin": {
"o2p": "index.js"
Expand Down
22 changes: 17 additions & 5 deletions src/parser/openapi3/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const _ = require('lodash')
module.exports = function() {

const MAX_DEPTH_LEVEL = 20;

let seenSchemas = new WeakSet();

return function get(verb, path, bodyResponse) {
if (!_.isObject(global.definition.paths)) {
require('../../utils/error.js')('paths is required')
Expand Down Expand Up @@ -110,6 +111,17 @@ module.exports = function() {
}

function replaceAllOfs(schema){
if (!_.isObject(schema)) return schema;

// Detectar ciclos por identidad
if (seenSchemas.has(schema)) {
return {
type: "string",
description: "Circular schema avoided"
};
}
seenSchemas.add(schema);

let result = {}
for (let i in schema) {
if (i === 'allOf' && _.isArray(schema[i])){
Expand Down Expand Up @@ -151,11 +163,11 @@ module.exports = function() {
}
result = _.merge(result, merged)
} else if ( _.isArray(schema[i]) && i !== 'required') {
const arrayResult = []
for (let k in schema[i]) {
arrayResult.push(replaceAllOfs(schema[i][k]))
if (schema[i].every(v => !_.isObject(v))) {
result[i] = [...schema[i]];
} else {
result[i] = schema[i].map(item => replaceAllOfs(item));
}
result[i] = arrayResult
} else if ( _.isObject(schema[i]) && i !== 'required') {
// result.type = 'object';
result[i] = _.merge(result[i],replaceAllOfs(schema[i]))
Expand Down
14 changes: 11 additions & 3 deletions src/parser/openapi3/queryParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ module.exports = function() {
// parameters = replaceRefs(parameters);
let queryParams = _.filter(parameters, ['in', 'query'])
const result = []
_.forEach(queryParams, function(queryParam) {
_.forEach(queryParams, function(queryParam) {
const param = queryParam.schema ? queryParam : getContentProperty(queryParam);
result.push({
name: queryParam.name,
type: queryParam.schema.type,
type: param.schema.type,
required : queryParam.required,
example: getExamples(queryParam)
example: getExamples(param)
});
});
return result
Expand Down Expand Up @@ -68,4 +69,11 @@ module.exports = function() {
}
}

function getContentProperty(query){
const queryContent = query.content;
for (const key in queryContent) {
return queryContent[key];
}
}

}()
9 changes: 5 additions & 4 deletions src/parser/openapiAuthorizationDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ module.exports = function() {

}
const data = parseUrl(definition.flows)
return generateDefinition(data)
const authKey = _.keys(global.definition.security[0])[0]
return generateDefinition(data,authKey)
}

function generateDefinition(data){
function generateDefinition(data,auth){
const postmanCollection = {
info: {
_postman_id: "2e397e19-7819-4425-bbb8-e2b7283336a4",
Expand All @@ -37,9 +38,9 @@ module.exports = function() {
" pm.response.to.have.status(200);",
"});",
"",
"var json = JSON.parse(responseBody);",
"var json = pm.response.json();",
"",
"pm.environment.set(\"OAuth2\", \"Bearer \"+json.data.access_token);"
`pm.environment.set(\"${auth}\", \"Bearer \"+json.data.access_token);`
],
type: "text/javascript"
}
Expand Down
15 changes: 15 additions & 0 deletions src/parser/swagger2/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
const _ = require('lodash');

module.exports = function() {

let seenSchemas = new WeakSet();


return function get(verb, path, bodyResponse) {
if (!_.isObject(global.definition.paths)) {
Expand Down Expand Up @@ -38,6 +41,18 @@ module.exports = function() {
};

function replaceRefs(schema){

if (!_.isObject(schema)) return schema;

// Detectar ciclos por identidad
if (seenSchemas.has(schema)) {
return {
type: "string",
description: "Circular schema avoided"
};
}
seenSchemas.add(schema);

let result = {};
for (let i in schema) {
if (i === '$ref'){
Expand Down
12 changes: 12 additions & 0 deletions src/parser/swagger2/pathParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const _ = require('lodash');

module.exports = function() {
let seenSchemas = new WeakSet();

return function get(verb,path){
if (!_.isObject(global.definition.paths)) {
Expand All @@ -23,6 +24,17 @@ module.exports = function() {
}

function replaceRefs(schema){
if (!_.isObject(schema)) return schema;

// Detectar ciclos por identidad
if (seenSchemas.has(schema)) {
return {
type: "string",
description: "Circular schema avoided"
};
}
seenSchemas.add(schema);

let result = {};
for (let i in schema) {
if (i === '$ref'){
Expand Down
4 changes: 3 additions & 1 deletion src/swagger2json/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ module.exports = function() {
if (!swagger.items){
require('../utils/error.js')('There is a array without items');
}
if(swagger.items.oneOf) delete swagger.items.oneOf;
if(swagger.items.anyOf) delete swagger.items.anyOf;
if (!swagger.items.properties){
swagger.items.properties = _.cloneDeep(swagger.items);
swagger.items.properties = _.cloneDeep(swagger.items);
}
if (!(swagger.items.properties.type && typeof swagger.items.properties.type === 'string')){
let keys = _.keys(swagger.items.properties);
Expand Down
21 changes: 15 additions & 6 deletions src/swagger2json/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ const _ = require('lodash');
module.exports = function() {

return function get(swagger, name, parent,index){
if (typeof swagger !== 'object' || swagger === null) {
swagger = { type: 'string' };
}

if (!swagger.type && swagger.properties){
swagger.type = 'object';
} else if (swagger.oneOf) {
swagger.type = 'oneOf';
} else if (swagger.anyOf) {
swagger.type = 'anyOf';
if (!swagger.type) {
if (swagger.properties) {
swagger.type = 'object';
} else if (swagger.items) {
swagger.type = 'array';
} else if (swagger.oneOf) {
swagger.type = 'oneOf';
} else if (swagger.anyOf) {
swagger.type = 'anyOf';
} else {
swagger.type = 'string';
}
}

let wrongParam = false;
Expand Down
16 changes: 13 additions & 3 deletions src/swagger2json/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const _ = require('lodash');
module.exports = function() {

return function get(swagger,parent){
if (!swagger.properties){
require('../utils/error.js')(`There is an object without properties: ${swagger}`);
}

validationProperties(swagger);

let notInclude = false;
if (parent && global.requiredParamsCatch && _.has(swagger, 'required')){
if(!global.configurationFile.minimal_endpoints){
Expand All @@ -35,6 +35,7 @@ module.exports = function() {
}
const object = {};
_.forEach(swagger.properties,function(property,name){
if(!property.type) return object;
if (notInclude === false || notInclude !== name){
let newParent = parent ? parent+'.'+name : undefined;
object[name] = require('./index.js')(property,name,newParent);
Expand All @@ -43,4 +44,13 @@ module.exports = function() {
return object;
};

function validationProperties(swagger){
if(!swagger.properties || Object.keys(swagger.properties).length === 0){
if(swagger.additionalProperties && swagger.additionalProperties !== false){
return {}
}
require('../utils/error.js')(`There is an object without properties: ${JSON.stringify(swagger)}`);
}
}

}()