From 8dfc734f3641b821dfc410f19678b7ad01657666 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 12 May 2017 22:55:10 -0700 Subject: [PATCH] Fix issue with more than two duplicate operationIds --- src/interfaces.js | 11 ++++++----- test/interfaces.js | 11 ++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/interfaces.js b/src/interfaces.js index f32a1256e..64b9f5809 100644 --- a/src/interfaces.js +++ b/src/interfaces.js @@ -81,20 +81,21 @@ export function mapTagOperations({spec, cb = nullFn, defaultTag = 'default'}) { const id = opId(operation, pathName, method) const cbResult = cb({spec, pathName, method, operation, operationId: id}) - // Id already exists? - if (typeof tagObj[id] !== 'undefined') { + if(operationIdCounter[id]) { + operationIdCounter[id] = operationIdCounter[id] + 1 + tagObj[`${id}${operationIdCounter[id]}`] = cbResult + } else if (typeof tagObj[id] !== 'undefined') { // Bump counter ( for this operationId ) let originalCounterValue = (operationIdCounter[id] || 1) operationIdCounter[id] = originalCounterValue + 1 // Append _x to the operationId tagObj[`${id}${operationIdCounter[id]}`] = cbResult - // Rename the first operationId, if it exists + // Rename the first operationId let temp = tagObj[id] delete tagObj[id] tagObj[`${id}${originalCounterValue}`] = temp - } - else { + } else { // Assign callback result ( usually a bound function ) tagObj[id] = cbResult } diff --git a/test/interfaces.js b/test/interfaces.js index 6a2f413ff..a8a12eba7 100644 --- a/test/interfaces.js +++ b/test/interfaces.js @@ -353,18 +353,23 @@ describe('intefaces', function () { put: { operationId: 'getOne' }, + post: { + operationId: 'getOne' + } } } } // With - const tags = mapTagOperations({spec, defaultTag: 'hug'}) + let count = 1 + const tags = mapTagOperations({spec, defaultTag: 'hug', cb: () => count++}) // Then expect(tags).toEqual({ hug: { - getOne1: null, - getOne2: null, + getOne1: 1, + getOne2: 2, + getOne3: 3 } }) })