Skip to content

Commit

Permalink
Add check for duplicate operation ids
Browse files Browse the repository at this point in the history
Fixes #68
  • Loading branch information
marcelstoer committed Aug 7, 2018
1 parent bbf1c79 commit 4e6ed4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/validators/spec.js
Expand Up @@ -17,12 +17,13 @@ function validateSpec (api) {
util.debug('Validating against the Swagger 2.0 spec');

var paths = Object.keys(api.paths || {});
var operationIds = [];
paths.forEach(function (pathName) {
var path = api.paths[pathName];
var pathId = '/paths' + pathName;

if (path && pathName.indexOf('/') === 0) {
validatePath(api, path, pathId);
validatePath(api, path, pathId, operationIds);
}
});
var definitions = Object.keys(api.definitions || {});
Expand All @@ -38,16 +39,26 @@ function validateSpec (api) {
/**
* Validates the given path.
*
* @param {SwaggerObject} api - The entire Swagger API object
* @param {object} path - A Path object, from the Swagger API
* @param {string} pathId - A value that uniquely identifies the path
* @param {SwaggerObject} api - The entire Swagger API object
* @param {object} path - A Path object, from the Swagger API
* @param {string} pathId - A value that uniquely identifies the path
* @param {string} operationIds - An array of collected operationIds found in other paths
*/
function validatePath (api, path, pathId) {
function validatePath (api, path, pathId, operationIds) {
swaggerMethods.forEach(function (operationName) {
var operation = path[operationName];
var operationId = pathId + '/' + operationName;

if (operation) {
var declaredOperationId = operation.operationId;
if (declaredOperationId) {
if (operationIds.includes(declaredOperationId)) {
throw ono.syntax('Validation failed. Duplicate operation id \'%s\'', declaredOperationId);
}
else {
operationIds.push(declaredOperationId);
}
}
validateParameters(api, path, pathId, operation, operationId);

var responses = Object.keys(operation.responses || {});
Expand Down
17 changes: 17 additions & 0 deletions test/specs/validate-spec/invalid/duplicate-operation-ids.yaml
@@ -0,0 +1,17 @@
swagger: "2.0"
info:
version: "1.0.0"
title: Invalid API

paths:
/users:
get:
operationId: users
responses:
default:
description: hello world
post:
operationId: users # <---- duplicate
responses:
default:
description: hello world
6 changes: 6 additions & 0 deletions test/specs/validate-spec/validate-spec.spec.js
Expand Up @@ -114,6 +114,12 @@ describe('Invalid APIs (Swagger 2.0 specification validation)', function () {
name: 'schema declares required properties which are inherited (allOf)',
valid: true,
file: 'inherited-required-properties.yaml'
},
{
name: 'duplicate operation IDs',
valid: false,
file: 'duplicate-operation-ids.yaml',
error: 'Validation failed. Duplicate operation id \'users\''
}
];

Expand Down

0 comments on commit 4e6ed4b

Please sign in to comment.