Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[validation] Warn on duplicate tags
The specification requires that tags are unique. This isn't caught by
swagger-parser, so we will issue a warning if duplicate tag names are
found.
  • Loading branch information
jimschubert committed Apr 27, 2020
commit d1b85d3a43b3984fcb85257e681bc86feeb4d66e
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.tags.Tag;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.validation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* A validator which evaluates an OpenAPI 3.x specification document
Expand Down Expand Up @@ -101,6 +100,27 @@ public ValidationResult validate(OpenAPI specification) {
validationResult.consume(parameterValidations.validate(wrapper));
});

List<Tag> tags = specification.getTags();
if (tags != null && tags.size() > 1) {
Set<String> distinct = new HashSet<>();
Set<String> duplicated = new HashSet<>();
tags.forEach(tag -> {
// add returns false if it already exists…
if (!distinct.add(tag.getName())) {
duplicated.add(tag.getName());
}
});
if (duplicated.size() > 0) {
// From https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields
// A list of tags used by the specification with additional metadata. The order of the tags can be used
// to reflect on their order by the parsing tools. Not all tags that are used by the Operation Object
// must be declared. The tags that are not declared MAY be organized randomly or based on the tools'
// logic. Each tag name in the list MUST be unique.
ValidationRule rule = ValidationRule.warn("Duplicate tags", "The specification requires that tag names are unique.", s -> ValidationRule.Fail.empty());
validationResult.addResult(Validated.invalid(rule, "Duplicated tag(s): " + String.join(",", duplicated)));
}
}

return validationResult;
}
}