Skip to content

Commit 031cfcb

Browse files
committed
Merge branch 'feature/validate-transactions-v2-update-path-logic' of github.com:postmanlabs/openapi-to-postman into feature/validate-transactions-v2-update-path-logic
2 parents 389cde4 + cc35f3f commit 031cfcb

File tree

3 files changed

+139
-36
lines changed

3 files changed

+139
-36
lines changed

libV2/helpers/collection/generateSkeletionTreeFromOpenAPI.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ let _ = require('lodash'),
4141
_.forEach(paths, function (completePath) {
4242
let pathSplit = completePath === '/' ? [completePath] : _.compact(completePath.split('/'));
4343

44+
/**
45+
* /user
46+
* /team
47+
* /hi
48+
* /bye
49+
*
50+
* In this scenario, always create a base folder for the path
51+
* and then add and link the request inside the created folder.
52+
*/
4453
if (pathSplit.length === 1) {
4554
let methods = openapi.paths[completePath];
4655

@@ -57,6 +66,20 @@ let _ = require('lodash'),
5766
return;
5867
}
5968

69+
if (!tree.hasNode(`path:folder:${pathSplit[0]}`)) {
70+
tree.setNode(`path:folder:${pathSplit[0]}`, {
71+
type: 'folder',
72+
meta: {
73+
name: pathSplit[0],
74+
path: pathSplit[0],
75+
pathIdentifier: pathSplit[0]
76+
},
77+
data: {}
78+
});
79+
80+
tree.setEdge('root:collection', `path:folder:${pathSplit[0]}`);
81+
}
82+
6083
tree.setNode(`path:request:${pathSplit[0]}:${method}`, {
6184
type: 'request',
6285
data: {},
@@ -67,13 +90,7 @@ let _ = require('lodash'),
6790
}
6891
});
6992

70-
if (tree.hasNode(`path:folder:${pathSplit[0]}`)) {
71-
tree.setEdge(`path:folder:${pathSplit[0]}`, `path:request:${pathSplit[0]}:${method}`);
72-
}
73-
74-
else {
75-
tree.setEdge('root:collection', `path:request:${pathSplit[0]}:${method}`);
76-
}
93+
tree.setEdge(`path:folder:${pathSplit[0]}`, `path:request:${pathSplit[0]}:${method}`);
7794
});
7895
}
7996

@@ -98,6 +115,28 @@ let _ = require('lodash'),
98115
return;
99116
}
100117

118+
/**
119+
* If it is the last node,
120+
* it might happen that this exists as a folder.
121+
*
122+
* If yes add a request inside that folder else
123+
* add as a request on the previous path idendified which will be a folder.
124+
*/
125+
if (!tree.hasNode(`path:folder:${pathIdentifier}`)) {
126+
tree.setNode(`path:folder:${pathIdentifier}`, {
127+
type: 'folder',
128+
meta: {
129+
name: path,
130+
path: path,
131+
pathIdentifier: pathIdentifier
132+
},
133+
data: {}
134+
});
135+
136+
tree.setEdge(index === 0 ? 'root:collection' : `path:folder:${previousPathIdentified}`,
137+
`path:folder:${pathIdentifier}`);
138+
}
139+
101140
tree.setNode(`path:request:${pathIdentifier}:${method}`, {
102141
type: 'request',
103142
data: {},
@@ -108,20 +147,7 @@ let _ = require('lodash'),
108147
}
109148
});
110149

111-
/**
112-
* If it is the last node,
113-
* it might happen that this exists as a folder.
114-
*
115-
* If yes add a request inside that folder else
116-
* add as a request on the previous path idendified which will be a folder.
117-
*/
118-
if (tree.hasNode(`path:folder:${pathIdentifier}`)) {
119-
tree.setEdge(`path:folder:${pathIdentifier}`, `path:request:${pathIdentifier}:${method}`);
120-
}
121-
122-
else {
123-
tree.setEdge(`path:folder:${previousPathIdentified}`, `path:request:${pathIdentifier}:${method}`);
124-
}
150+
tree.setEdge(`path:folder:${pathIdentifier}`, `path:request:${pathIdentifier}:${method}`);
125151
});
126152
}
127153

test/data/valid_openapi/too_many_ref_example.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,89 @@
230230
}
231231
},
232232
"u": {
233+
"type": "object",
234+
"properties": {
235+
"l": {
236+
"$ref": "#/components/schemas/v"
237+
}
238+
}
239+
},
240+
"v": {
241+
"type": "object",
242+
"properties": {
243+
"l": {
244+
"$ref": "#/components/schemas/x"
245+
}
246+
}
247+
},
248+
"x": {
249+
"type": "object",
250+
"properties": {
251+
"l": {
252+
"$ref": "#/components/schemas/y"
253+
}
254+
}
255+
},
256+
"y": {
257+
"type": "object",
258+
"properties": {
259+
"l": {
260+
"$ref": "#/components/schemas/z"
261+
}
262+
}
263+
},
264+
"z": {
265+
"type": "object",
266+
"properties": {
267+
"l": {
268+
"$ref": "#/components/schemas/aa"
269+
}
270+
}
271+
},
272+
"aa": {
273+
"type": "object",
274+
"properties": {
275+
"l": {
276+
"$ref": "#/components/schemas/ab"
277+
}
278+
}
279+
},
280+
281+
"ab": {
282+
"type": "object",
283+
"properties": {
284+
"l": {
285+
"$ref": "#/components/schemas/ac"
286+
}
287+
}
288+
},
289+
290+
"ac": {
291+
"type": "object",
292+
"properties": {
293+
"l": {
294+
"$ref": "#/components/schemas/ad"
295+
}
296+
}
297+
},
298+
"ad": {
299+
"type": "object",
300+
"properties": {
301+
"l": {
302+
"$ref": "#/components/schemas/ae"
303+
}
304+
}
305+
},
306+
"ae": {
233307
"type": "object",
234308
"properties": {
235309
"l": {
236310
"$ref": "#/components/schemas/a"
237311
}
238312
}
239313
},
314+
315+
240316
"a": {
241317
"required": [
242318
"id",

test/unit/convertV2.test.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ describe('The convert v2 Function', function() {
132132

133133
it('Should generate collection conforming to schema for and fail if not valid ' +
134134
tooManyRefs, function(done) {
135-
var openapi = fs.readFileSync(tooManyRefs, 'utf8'),
136-
body;
135+
var openapi = fs.readFileSync(tooManyRefs, 'utf8');
137136
Converter.convertV2({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => {
138137

139138
expect(err).to.be.null;
@@ -142,7 +141,8 @@ describe('The convert v2 Function', function() {
142141
expect(conversionResult.output[0].type).to.equal('collection');
143142
expect(conversionResult.output[0].data).to.have.property('info');
144143
expect(conversionResult.output[0].data).to.have.property('item');
145-
body = conversionResult.output[0].data.item[1].response[0].body;
144+
let body = conversionResult.output[0].data.item[1].response[0].body;
145+
146146
expect(body).to.contain('<Error: Too many levels of nesting to fake this schema>');
147147
done();
148148
});
@@ -1807,21 +1807,22 @@ describe('The convert v2 Function', function() {
18071807
// Make sure that path params are updated and their respective default values
18081808
convertResult.output.forEach(function(element) {
18091809
expect(element.type).to.equal('collection');
1810-
expect(element.data.item[0].item[0].item[0]
1810+
expect(element.data.item[0].item[0]
18111811
.item[0].item[0].request.url.path.indexOf(':ownerId') > -1)
18121812
.to.equal(true);
1813-
expect(element.data.item[0].item[0].item[0]
1813+
expect(element.data.item[0].item[0]
18141814
.item[0].item[0].request.url.path.indexOf(':petId') > -1)
18151815
.to.equal(true);
18161816

1817-
let thisVar = element.data.item[0].item[0].item[0].item[0].item[0].request.url.variable[0];
1817+
let thisVar = element.data.item[0].item[0].item[0].item[0].request.url.variable[0];
18181818

18191819
expect(thisVar.type).to.equal('any');
18201820

18211821
expect(thisVar.value).to.equal('42');
18221822
expect(thisVar.key).to.equal('ownerId');
1823+
1824+
done();
18231825
});
1824-
done();
18251826
});
18261827
});
18271828

@@ -1926,16 +1927,16 @@ describe('The convert v2 Function', function() {
19261927
optimizeConversion: false, stackLimit: 50
19271928
}, (err, result) => {
19281929
const expectedResponseBody1 = JSON.parse(
1929-
result.output[0].data.item[0].item[0].item[0].item[0]
1930+
result.output[0].data.item[0].item[0]
19301931
.item[0].response[0].body
19311932
),
19321933
expectedResponseBody2 = JSON.parse(
19331934
result.output[0].data.item[0].item[0].item[1].item[0].response[0].body
19341935
);
19351936
expect(err).to.be.null;
19361937
expect(result.result).to.be.true;
1937-
expect(expectedResponseBody1.payload).to.be.eql('<boolean>');
1938-
expect(expectedResponseBody2.payload).to.be.an('object')
1938+
expect(expectedResponseBody2.payload).to.be.eql('<boolean>');
1939+
expect(expectedResponseBody1.payload).to.be.an('object')
19391940
.and.to.have.all.keys('content', 'paging');
19401941
});
19411942
});
@@ -1948,7 +1949,7 @@ describe('The convert v2 Function', function() {
19481949
expect(err).to.be.null;
19491950
expect(conversionResult.result).to.equal(true);
19501951

1951-
const response = conversionResult.output[0].data.item[0].item[0].item[0].response[0];
1952+
const response = conversionResult.output[0].data.item[0].item[0].response[0];
19521953
expect(response.body).to.equal('[\n\t{\n\t\t"id": "<long>",\n\t\t"name": "<string>",\n\t\t"tag": "<string>"' +
19531954
'\n\t},\n\t{\n\t\t"id": "<long>",\n\t\t"name": "<string>",\n\t\t"tag": "<string>"' +
19541955
'\n\t}\n]');
@@ -1963,7 +1964,7 @@ describe('The convert v2 Function', function() {
19631964
expect(err).to.be.null;
19641965
expect(conversionResult.result).to.equal(true);
19651966

1966-
const response = conversionResult.output[0].data.item[0].item[0].item[0].response[0];
1967+
const response = conversionResult.output[0].data.item[0].item[0].response[0];
19671968
expect(response.body).to.equal('[\n {\n "id": "<long>",\n "name": "<string>",\n "tag": "<string>' +
19681969
'"\n },\n {\n "id": "<long>",\n "name": "<string>",\n "tag": "<string>"\n }\n]');
19691970
done();
@@ -1984,7 +1985,7 @@ describe('The convert v2 Function', function() {
19841985
expect(conversionResult.output[0].data.auth.apikey[0].value).to.equal('{{apiKeyName}}');
19851986
expect(conversionResult.output[0].data.auth.apikey[1].value).to.equal('{{apiKey}}');
19861987

1987-
const item = conversionResult.output[0].data.item[0].item[0].item[0];
1988+
const item = conversionResult.output[0].data.item[0].item[0];
19881989
expect(item.request.header).to.not.be.ok;
19891990
expect(item.response[0].originalRequest.header[0]).to.be.eql({
19901991
description: {
@@ -2006,7 +2007,7 @@ describe('The convert v2 Function', function() {
20062007
expect(err).to.be.null;
20072008
expect(conversionResult.result).to.equal(true);
20082009

2009-
const item = conversionResult.output[0].data.item[0].item[0].item[0];
2010+
const item = conversionResult.output[0].data.item[0].item[0];
20102011
expect(item.request.header).to.not.be.ok;
20112012
expect(item.response[0].originalRequest.header).to.not.be.ok;
20122013

@@ -2028,7 +2029,7 @@ describe('The convert v2 Function', function() {
20282029
expect(conversionResult.output[0].data.auth.oauth1[0].value).to.equal('{{consumerSecret}}');
20292030
expect(conversionResult.output[0].data.auth.oauth1[1].value).to.equal('{{consumerKey}}');
20302031

2031-
const item = conversionResult.output[0].data.item[0].item[0].item[0];
2032+
const item = conversionResult.output[0].data.item[0].item[0];
20322033

20332034
expect(item.request.header).to.not.be.ok;
20342035
expect(item.response[0].originalRequest.header[0]).to.be.eql({

0 commit comments

Comments
 (0)