Search in sources :

Example 1 with ApiDescription

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;
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Operation(io.swagger.models.Operation) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) Tag(io.swagger.models.Tag) ApiDescription(io.swagger.model.ApiDescription) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ApiDescription (io.swagger.model.ApiDescription)1 ArrayModel (io.swagger.models.ArrayModel)1 ComposedModel (io.swagger.models.ComposedModel)1 Model (io.swagger.models.Model)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 RefModel (io.swagger.models.RefModel)1 Swagger (io.swagger.models.Swagger)1 Tag (io.swagger.models.Tag)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1