Skip to content

Commit

Permalink
Avoid tags duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchernev committed Jan 15, 2017
1 parent 103aab6 commit 5d4e599
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions lib/swagger-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
// Dependencies.
var RecursiveIterator = require('recursive-iterator');

/**
* Checks if tag is already contained withing target.
* The tag is an object of type http://swagger.io/specification/#tagObject
* The target, is the part of the swagger specification that holds all tags.
* @function
* @param {object} target - Swagger object place to include the tags data.
* @param {object} tag - Swagger tag object to be included.
* @returns {boolean} Does tag is already present in target
*/
function _tagDuplicated(target, tag) {
// Check input is workable.
if (target && target.length && tag) {
for (var i = 0; i < target.length; i = i + 1) {
var targetTag = target[i];
// The name of the tag to include already exists in the taget.
// Therefore, it's not necessary to be added again.
if (targetTag.name === tag.name) {
return true;
}
}
}

// This will indicate that `tag` is not present in `target`.
return false;
}

/**
* Adds the tags property to a swagger object.
* @function
Expand All @@ -20,10 +46,14 @@ function _attachTags(conf) {

if (Array.isArray(tag)) {
for (var i = 0; i < tag.length; i = i + 1) {
swaggerObject[propertyName].push(tag[i]);
if (!_tagDuplicated(swaggerObject[propertyName], tag[i])) {
swaggerObject[propertyName].push(tag[i]);
}
}
} else {
swaggerObject[propertyName].push(tag);
if (!_tagDuplicated(swaggerObject[propertyName], tag)) {
swaggerObject[propertyName].push(tag);
}
}
}

Expand Down

0 comments on commit 5d4e599

Please sign in to comment.