private void assertSwagger(Swagger swagger) {
    assertEquals("/", swagger.getBasePath());

    assertEquals(INFO_DESCRIPTION, swagger.getInfo().getDescription());
    assertEquals(INFO_TERMS_OF_SERVICE, swagger.getInfo().getTermsOfService());

    // excluded prefixes
    assertNull(swagger.getPath(ServiceUriPaths.CORE_AUTHZ_USERS));
    assertNull(swagger.getPath(ServiceUriPaths.CORE_AUTHZ_ROLES));

    assertNotNull(swagger.getPath(ServiceUriPaths.CORE_PROCESSES));
    assertNotNull(swagger.getPath(ServiceUriPaths.CORE_CREDENTIALS));

    Path p = swagger.getPath("/cars");
    assertNotNull(p);
    assertNotNull(p.getPost());
    assertNotNull(p.getGet());

    assertNotNull(swagger.getPath("/cars/template"));
    assertNotNull(swagger.getPath("/cars/available"));
    assertNotNull(swagger.getPath("/cars/config"));
    assertNotNull(swagger.getPath("/cars/stats"));
    assertNotNull(swagger.getPath("/cars/subscriptions"));

    assertNotNull(swagger.getPath("/cars/{id}/template"));
    assertNotNull(swagger.getPath("/cars/{id}/available"));
    assertNotNull(swagger.getPath("/cars/{id}/config"));
    assertNotNull(swagger.getPath("/cars/{id}/stats"));
    assertNotNull(swagger.getPath("/cars/{id}/subscriptions"));

    p = swagger.getPath("/cars/{id}");
    assertNotNull(p);
    assertNull(p.getPost());
    assertNull(p.getPatch());
    assertNotNull(p.getGet());
    assertNotNull(p.getPut());

    p = swagger.getPath("/tokens");
    assertNotNull(p);
    assertNotNull(p.getGet());
    assertNotNull(p.getGet().getResponses());
    assertNotNull(p.getPost());
    assertNotNull(p.getPost().getParameters());
    assertNull(p.getPatch());
    assertNull(p.getDelete());

    Model model = swagger.getDefinitions().get(Utils.buildKind(UserToken.class));
    Map<String, Property> properties = model.getProperties();
    assertNull(properties.get(UserToken.FIELD_NAME_INTERNAL_ID));
  }
  public Map<String, List<CodegenOperation>> processPaths(Map<String, Path> paths) {
    Map<String, List<CodegenOperation>> ops = new HashMap<String, List<CodegenOperation>>();

    for (String resourcePath : paths.keySet()) {
      Path path = paths.get(resourcePath);
      processOperation(resourcePath, "get", path.getGet(), ops, path);
      processOperation(resourcePath, "head", path.getHead(), ops, path);
      processOperation(resourcePath, "put", path.getPut(), ops, path);
      processOperation(resourcePath, "post", path.getPost(), ops, path);
      processOperation(resourcePath, "delete", path.getDelete(), ops, path);
      processOperation(resourcePath, "patch", path.getPatch(), ops, path);
      processOperation(resourcePath, "options", path.getOptions(), ops, path);
    }
    return ops;
  }
Beispiel #3
0
  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("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.setDefinitions(definitions);

    // isRemovingUnreferencedDefinitions is not defined in SwaggerSpecFilter to avoid breaking
    // compatibility with
    // existing client filters directly implementing SwaggerSpecFilter.
    if (filter instanceof AbstractSpecFilter) {
      if (((AbstractSpecFilter) filter).isRemovingUnreferencedDefinitions()) {
        clone = removeBrokenReferenceDefinitions(clone);
      }
    }

    return clone;
  }