Skip to content

Commit b0cb893

Browse files
committed
added deduplication of operationIds per swagger-api#595
1 parent a596744 commit b0cb893

File tree

7 files changed

+96
-13
lines changed

7 files changed

+96
-13
lines changed

browser/swagger-client.js

Lines changed: 18 additions & 2 deletions
Large diffs are not rendered by default.

browser/swagger-client.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/client.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ SwaggerClient.prototype.buildFromSpec = function (response) {
335335
}
336336

337337
var operationId = self.idFromOp(path, method, operation);
338+
338339
var operationObject = new Operation(self,
339340
operation.scheme,
340341
operationId,
@@ -388,6 +389,8 @@ SwaggerClient.prototype.buildFromSpec = function (response) {
388389
self.apisArray.push(new OperationGroup(tag, operationGroup.description, operationGroup.externalDocs, operationObject));
389390
}
390391

392+
operationId = self.makeUniqueOperationId(operationId, self.apis[apiProperty]);
393+
391394
// Bind tag help
392395
if (!_.isFunction(operationGroup.help)) {
393396
operationGroup.help = _.bind(self.help, operationGroup);
@@ -432,6 +435,28 @@ SwaggerClient.prototype.buildFromSpec = function (response) {
432435
return this;
433436
};
434437

438+
SwaggerClient.prototype.makeUniqueOperationId = function(operationId, api) {
439+
var count = 0;
440+
var name = operationId;
441+
442+
// make unique across this operation group
443+
while(true) {
444+
var matched = false;
445+
_.forEach(api.operations, function (operation) {
446+
if(operation.nickname === name) {
447+
matched = true;
448+
}
449+
})
450+
if(!matched) {
451+
return name;
452+
}
453+
name = operationId + '_' + count;
454+
count ++;
455+
}
456+
457+
return operationId;
458+
}
459+
435460
SwaggerClient.prototype.parseUri = function (uri) {
436461
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
437462
var parts = urlParseRE.exec(uri);
@@ -488,6 +513,7 @@ SwaggerClient.prototype.idFromOp = function (path, httpMethod, op) {
488513
opId = opId.replace(/((_){2,})/g, '_');
489514
opId = opId.replace(/^(_)*/g, '');
490515
opId = opId.replace(/([_])*$/g, '');
516+
491517
return opId;
492518
};
493519

test/client.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ describe('SwaggerClient', function () {
377377
usePromise: true
378378
}).then(function(client) {
379379
var param = client.test.apis.myop.parameters[0];
380-
console.log(param['enum']);
381380
expect(param.enum).toBe(undefined);
382381
expect(param.items.enum).toEqual(['a', 'b']);
383382
done();
@@ -386,7 +385,6 @@ describe('SwaggerClient', function () {
386385
});
387386
});
388387

389-
390388
it('tests https://github.com/swagger-api/swagger-js/issues/535', function(done) {
391389
var spec = {
392390
paths: {
@@ -441,4 +439,46 @@ describe('SwaggerClient', function () {
441439
done(exception);
442440
});
443441
});
442+
443+
444+
it('creates unique operationIds per #595', function(done) {
445+
var spec = {
446+
paths: {
447+
'/foo': {
448+
get: {
449+
operationId: 'test',
450+
parameters: [],
451+
responses: {
452+
'default': {
453+
description: 'success'
454+
}
455+
}
456+
}
457+
},
458+
'/bar': {
459+
get: {
460+
operationId: 'test',
461+
parameters: [],
462+
responses: {
463+
'default': {
464+
description: 'success'
465+
}
466+
}
467+
}
468+
}
469+
}
470+
};
471+
472+
new SwaggerClient({
473+
url: 'http://example.com/petstore.yaml',
474+
spec: spec,
475+
usePromise: true
476+
}).then(function(client) {
477+
expect(client.default.test).toBeA('function');
478+
expect(client.default.test_0).toBeAn('function');
479+
done();
480+
}).catch(function(exception) {
481+
done(exception);
482+
});
483+
});
444484
});

test/macros.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ describe('client macros', function () {
4343
usePromise: true,
4444
parameterMacro: macros.parameter
4545
}).then(function(client) {
46-
var parameters = client.pet.apis.getPetById.parameters;
47-
expect(parameters[0].default).toBe('100');
48-
done();
49-
}).catch(function(exception) {
50-
done(exception);
51-
});
46+
var parameters = client.pet.apis.getPetById.parameters;
47+
expect(parameters[0].default).toBe('100');
48+
done();
49+
}).catch(function(exception) {
50+
done(exception);
51+
});
5252
});
5353
});

test/resolver.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,8 @@ describe('swagger resolver', function () {
10831083
'definitions' : { }
10841084
};
10851085
api.resolve(spec, 'http://localhost:8000/v2/swagger.json', function (spec, unresolved) {
1086-
console.log(JSON.stringify(spec, null, 2));
1086+
expect(spec.definitions).toBeAn('object');
1087+
expect(spec.definitions.inline_model).toBeAn('object');
10871088
done();
10881089
});
10891090
});

test/spec/v1/pet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"summary": "Find pet by ID",
1919
"notes": "Returns a pet based on ID",
2020
"type": "Animals",
21-
"nickname": "getPetById",
21+
"nickname": "getAnimalsById",
2222
"authorizations": {},
2323
"parameters": [
2424
{

0 commit comments

Comments
 (0)