Skip to content

Commit

Permalink
Merge pull request #1711 from frantuma/issue-1707b
Browse files Browse the repository at this point in the history
Fix #1707 - correctly remove unused nested definitions
  • Loading branch information
frantuma committed Mar 11, 2016
2 parents ac95489 + f2cc576 commit 4c51b67
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,37 @@ private Swagger removeBrokenReferenceDefinitions (Swagger swagger) {
}

if (swagger.getDefinitions() != null) {
for (String k: swagger.getDefinitions().keySet()) {
Model m = swagger.getDefinitions().get(k);
locateNestedReferencedDefinitions (m, referencedDefinitions);
Set<String> nestedReferencedDefinitions = new TreeSet<String>();
for (String ref : referencedDefinitions){
Model m = swagger.getDefinitions().get(ref);
locateNestedReferencedDefinitions (m.getProperties(), nestedReferencedDefinitions, swagger);
}
referencedDefinitions.addAll(nestedReferencedDefinitions);
swagger.getDefinitions().keySet().retainAll(referencedDefinitions);
}

return swagger;
}

private void locateNestedReferencedDefinitions (Model m, Set<String> referencedDefinitions) {

if (m.getProperties() == null) return;
for (String keyProp: m.getProperties().keySet()) {
Property p = m.getProperties().get(keyProp);
private void locateNestedReferencedDefinitions (Map<String, Property> props, Set<String> nestedReferencedDefinitions, Swagger swagger) {
if (props == null) return;
for (String keyProp: props.keySet()) {
Property p = props.get(keyProp);
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
if (ap.getItems() != null && ap.getItems() instanceof RefProperty) {
RefProperty rp = (RefProperty) ap.getItems();
String simpleRef = rp.getSimpleRef();
referencedDefinitions.add(simpleRef);
nestedReferencedDefinitions.add(simpleRef);
Model m = swagger.getDefinitions().get(simpleRef);
locateNestedReferencedDefinitions (m.getProperties(), nestedReferencedDefinitions, swagger);
}
} else if (p instanceof RefProperty) {
RefProperty rp = (RefProperty) p;
String simpleRef = rp.getSimpleRef();
referencedDefinitions.add(simpleRef);
nestedReferencedDefinitions.add(simpleRef);
Model m = swagger.getDefinitions().get(simpleRef);
locateNestedReferencedDefinitions (m.getProperties(), nestedReferencedDefinitions, swagger);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,24 @@ public void filterAwayBrokenReferenceModelProperties() throws IOException {

assertNotNull(swagger.getDefinitions().get("Order"));
assertNotNull(swagger.getDefinitions().get("NoPropertiesModel"));
assertNotNull(swagger.getDefinitions().get("OrderTag"));
assertNotNull(swagger.getDefinitions().get("Tag"));

final NoOpOperationsFilter noOpfilter = new NoOpOperationsFilter();
Swagger filtered = new SpecFilter().filter(swagger, noOpfilter, null, null, null);

assertNotNull(filtered.getDefinitions().get("Order"));
assertNotNull(swagger.getDefinitions().get("NoPropertiesModel"));
assertNotNull(filtered.getDefinitions().get("NoPropertiesModel"));
assertNotNull(filtered.getDefinitions().get("OrderTag"));
assertNotNull(filtered.getDefinitions().get("Tag"));

final RemoveUnreferencedDefinitionsFilter refFilter = new RemoveUnreferencedDefinitionsFilter();
filtered = new SpecFilter().filter(swagger, refFilter, null, null, null);

assertNull(filtered.getDefinitions().get("Order"));
assertNull(filtered.getDefinitions().get("NoPropertiesModel"));
assertNull(filtered.getDefinitions().get("OrderTag"));
assertNotNull(filtered.getDefinitions().get("Tag"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@
"type": "integer",
"format": "int32"
},
"orderTag": {
"$ref": "#/definitions/OrderTag"
},
"tag": {
"$ref": "#/definitions/Tag"
},
"petId": {
"type": "integer",
"format": "int32"
Expand All @@ -215,6 +221,17 @@
}
},
"NoPropertiesModel": {
},
"OrderTag": {
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string"
}
}
}
}
}

0 comments on commit 4c51b67

Please sign in to comment.