use of io.swagger.model.ApiDescription in project swagger-core by swagger-api.
the class SpecFilter method filter.
public Swagger filter(Swagger swagger, SwaggerSpecFilter filter, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
Swagger clone = new Swagger();
clone.info(swagger.getInfo()).tags(swagger.getTags() == null ? null : new ArrayList<Tag>(swagger.getTags())).host(swagger.getHost()).basePath(swagger.getBasePath()).schemes(swagger.getSchemes()).consumes(swagger.getConsumes()).produces(swagger.getProduces()).externalDocs(swagger.getExternalDocs()).vendorExtensions(swagger.getVendorExtensions());
final Set<String> filteredTags = new HashSet<String>();
final Set<String> allowedTags = new HashSet<String>();
for (String resourcePath : swagger.getPaths().keySet()) {
Path path = swagger.getPaths().get(resourcePath);
Map<String, Operation> ops = new HashMap<String, Operation>();
ops.put("get", path.getGet());
ops.put("head", path.getHead());
ops.put("put", path.getPut());
ops.put("post", path.getPost());
ops.put("delete", path.getDelete());
ops.put("patch", path.getPatch());
ops.put("options", path.getOptions());
Path clonedPath = new Path();
for (String key : ops.keySet()) {
Operation op = ops.get(key);
if (op != null) {
ApiDescription desc = new ApiDescription(resourcePath, key);
final Set<String> tags;
if (filter.isOperationAllowed(op, desc, params, cookies, headers)) {
clonedPath.set(key, filterOperation(filter, op, desc, params, cookies, headers));
tags = allowedTags;
} else {
tags = filteredTags;
}
if (op.getTags() != null) {
tags.addAll(op.getTags());
}
}
}
if (!clonedPath.isEmpty()) {
clone.path(resourcePath, clonedPath);
}
}
final List<Tag> tags = clone.getTags();
filteredTags.removeAll(allowedTags);
if (tags != null && !filteredTags.isEmpty()) {
for (Iterator<Tag> it = tags.iterator(); it.hasNext(); ) {
if (filteredTags.contains(it.next().getName())) {
it.remove();
}
}
if (clone.getTags().isEmpty()) {
clone.setTags(null);
}
}
Map<String, Model> definitions = filterDefinitions(filter, swagger.getDefinitions(), params, cookies, headers);
clone.setSecurityDefinitions(swagger.getSecurityDefinitions());
clone.setSecurity(swagger.getSecurity());
clone.setDefinitions(definitions);
// existing client filters directly implementing SwaggerSpecFilter.
if (filter instanceof AbstractSpecFilter) {
if (((AbstractSpecFilter) filter).isRemovingUnreferencedDefinitions()) {
clone = removeBrokenReferenceDefinitions(clone);
}
}
return clone;
}
Aggregations