Skip to content
Merged
Show file tree
Hide file tree
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 @@ -32,6 +32,8 @@ public class SwaggerDeserializer {
protected static Set<String> BODY_PARAMETER_KEYS = new LinkedHashSet<String>(Arrays.asList("name", "in", "description", "required", "schema"));
protected static Set<String> SECURITY_SCHEME_KEYS = new LinkedHashSet<String>(Arrays.asList("type", "name", "in", "description", "flow", "authorizationUrl", "tokenUrl" , "scopes"));

private final Set<String> operationIDs = new HashSet<>();

public SwaggerDeserializationResult deserialize(JsonNode rootNode) {
SwaggerDeserializationResult result = new SwaggerDeserializationResult();
ParseResult rootParse = new ParseResult();
Expand Down Expand Up @@ -301,7 +303,7 @@ public Operation operation(ObjectNode obj, String location, ParseResult result)
ExternalDocs docs = externalDocs(externalDocs, location, result);
output.setExternalDocs(docs);

value = getString("operationId", obj, false, location, result);
value = getString("operationId", obj, false, location, result, operationIDs);
output.operationId(value);

array = getArray("consumes", obj, false, location, result);
Expand Down Expand Up @@ -1577,6 +1579,10 @@ else if(!v.isValueNode()) {
}

public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result) {
return getString(key, node, required, location, result, null);
}

public String getString(String key, ObjectNode node, boolean required, String location, ParseResult result, Set<String> uniqueValues) {
String value = null;
JsonNode v = node.get(key);
if (node == null || v == null) {
Expand All @@ -1590,6 +1596,10 @@ else if(!v.isValueNode()) {
}
else {
value = v.asText();
if (uniqueValues != null && !uniqueValues.add(value)) {
result.unique(location, "operationId");
result.invalid();
}
}
return value;
}
Expand All @@ -1614,6 +1624,7 @@ protected static class ParseResult {
private Map<Location, JsonNode> unsupported = new LinkedHashMap<Location, JsonNode>();
private Map<Location, String> invalidType = new LinkedHashMap<Location, String>();
private List<Location> missing = new ArrayList<Location>();
private List<Location> unique = new ArrayList<>();

public ParseResult() {
}
Expand All @@ -1626,6 +1637,10 @@ public void extra(String location, String key, JsonNode value) {
extra.put(new Location(location, key), value);
}

public void unique(String location, String key) {
unique.add(new Location(location, key));
}

public void missing(String location, String key) {
missing.add(new Location(location, key));
}
Expand Down Expand Up @@ -1695,6 +1710,11 @@ public List<String> getMessages() {
String message = "attribute " + location + l.key + " is missing";
messages.add(message);
}
for (Location l : unique) {
String location = l.location.equals("") ? "" : l.location + ".";
String message = "attribute " + location + l.key + " is repeated";
messages.add(message);
}
for(Location l : unsupported.keySet()) {
String location = l.location.equals("") ? "" : l.location + ".";
String message = "attribute " + location + l.key + " is unsupported";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,4 +1248,12 @@ public void testIssue844() {
assertEquals(swagger.getSwagger().getPath("/pets/{id}").getGet().getParameters().get(0).getIn(), "header");
}

@Test
public void testIssue258() {
SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("duplicateOperationId.json", null, true);
assertNotNull(result);
assertNotNull(result.getSwagger());
assertEquals(result.getMessages().get(0), "attribute paths.'/pets/{id}'(post).operationId is repeated");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.9-abcd",
"title": "Duplicate operationID"
},
"paths": {
"/pets/{id}": {
"get": {
"operationId": "getPetsById",
"responses": {
"default": {
"description": "error payload"
}
}
},
"post": {
"operationId": "getPetsById",
"responses": {
"default": {
"description": "error payload"
}
}
}
}
}
}