Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[validation] Warn on duplicate tags #6061

Merged
merged 1 commit into from
Apr 29, 2020
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
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;
}
}