Skip to content

Commit 489e6d3

Browse files
committed
Fixed an issue where duplicate collection variables were generated for certain definitions
1 parent ddf71ae commit 489e6d3

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

lib/common/versionUtils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ function getSpecVersion({ type, data, specificationVersion }) {
212212
const openapi30 = getVersionRegexp(VERSION_30),
213213
openapi31 = getVersionRegexp(VERSION_31),
214214
openapi20 = getVersionRegexp(VERSION_20),
215-
is30 = data.match(openapi30),
216-
is31 = data.match(openapi31),
217-
is20 = data.match(openapi20);
215+
is30 = typeof data === 'string' && data.match(openapi30),
216+
is31 = typeof data === 'string' && data.match(openapi31),
217+
is20 = typeof data === 'string' && data.match(openapi20);
218+
218219
let version = DEFAULT_SPEC_VERSION;
219220

220221
if (is30) {

libV2/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ module.exports = {
213213
}
214214
});
215215

216+
// Remove duplicate variables as different requests could end up creating same variables
217+
if (!_.isEmpty(collection.variable)) {
218+
collection.variable = _.uniqBy(collection.variable, 'key');
219+
}
220+
216221
return cb(null, {
217222
result: true,
218223
output: [{
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "MyTitle",
5+
"description": "My Description",
6+
"version": "1.0.0",
7+
"x-ms-generated-by": {
8+
"toolName": "Microsoft.OpenApi.OData",
9+
"toolVersion": "1.0.9.0"
10+
}
11+
},
12+
"paths": {
13+
"/Path1({MyParam})": {
14+
"description": "My path1 description",
15+
"get": {
16+
"tags": [
17+
"MyTag"
18+
],
19+
"summary": "does path1",
20+
"operationId": "Path1",
21+
"parameters": [
22+
{
23+
"name": "MyParam",
24+
"in": "path",
25+
"description": "My Param",
26+
"schema": {
27+
"type": "string",
28+
"nullable": true
29+
}
30+
}
31+
]
32+
}
33+
},
34+
"/Path2({MyParam})": {
35+
"description": "My path2 description",
36+
"get": {
37+
"tags": [
38+
"MyTag"
39+
],
40+
"summary": "does path2",
41+
"operationId": "Path2",
42+
"parameters": [
43+
{
44+
"name": "MyParam",
45+
"in": "path",
46+
"description": "My Param",
47+
"schema": {
48+
"type": "string",
49+
"nullable": true
50+
}
51+
}
52+
]
53+
}
54+
}
55+
}
56+
}

test/unit/convertV2.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ const expect = require('chai').expect,
106106
multiContentTypesMultiExample =
107107
path.join(__dirname, VALID_OPENAPI_PATH, '/multiContentTypesMultiExample.json'),
108108
multiExampleRequestVariousResponse =
109-
path.join(__dirname, VALID_OPENAPI_PATH, '/multiExampleRequestVariousResponse.yaml');
109+
path.join(__dirname, VALID_OPENAPI_PATH, '/multiExampleRequestVariousResponse.yaml'),
110+
duplicateCollectionVars =
111+
path.join(__dirname, VALID_OPENAPI_PATH, '/duplicateCollectionVars.json');
110112

111113

112114
describe('The convert v2 Function', function() {
@@ -2728,4 +2730,22 @@ describe('The convert v2 Function', function() {
27282730
});
27292731
});
27302732
});
2733+
2734+
it('[Github #11884] Should not contain duplicate variables created from requests path', function (done) {
2735+
const openapi = fs.readFileSync(duplicateCollectionVars, 'utf8');
2736+
Converter.convertV2({ type: 'string', data: openapi }, { parametersResolution: 'Example' },
2737+
(err, conversionResult) => {
2738+
expect(err).to.be.null;
2739+
expect(conversionResult.result).to.equal(true);
2740+
expect(conversionResult.output.length).to.equal(1);
2741+
expect(conversionResult.output[0].type).to.equal('collection');
2742+
expect(conversionResult.output[0].data).to.have.property('info');
2743+
expect(conversionResult.output[0].data).to.have.property('item');
2744+
expect(conversionResult.output[0].data).to.have.property('variable');
2745+
expect(conversionResult.output[0].data.variable).to.have.lengthOf(2);
2746+
expect(conversionResult.output[0].data.variable[0]).to.have.property('key', 'baseUrl');
2747+
expect(conversionResult.output[0].data.variable[1]).to.have.property('key', 'MyParam');
2748+
done();
2749+
});
2750+
});
27312751
});

0 commit comments

Comments
 (0)