Skip to content

Commit

Permalink
Fix issue with more than two duplicate operationIds
Browse files Browse the repository at this point in the history
  • Loading branch information
shockey committed May 13, 2017
1 parent 2f9380e commit 8dfc734
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions test/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
})
Expand Down

0 comments on commit 8dfc734

Please sign in to comment.