Ejemplo n.º 1
0
  public Operation filterOperation(
      SwaggerSpecFilter filter,
      Operation op,
      ApiDescription api,
      Map<String, List<String>> params,
      Map<String, String> cookies,
      Map<String, List<String>> headers) {
    Operation clonedOperation =
        new Operation()
            .summary(op.getSummary())
            .description(op.getDescription())
            .operationId(op.getOperationId())
            .schemes(op.getSchemes())
            .consumes(op.getConsumes())
            .produces(op.getProduces())
            .tags(op.getTags())
            .externalDocs(op.getExternalDocs())
            .vendorExtensions(op.getVendorExtensions())
            .deprecated(op.isDeprecated());

    List<Parameter> clonedParams = new ArrayList<Parameter>();
    if (op.getParameters() != null) {
      for (Parameter param : op.getParameters()) {
        if (filter.isParamAllowed(param, op, api, params, cookies, headers)) {
          clonedParams.add(param);
        }
      }
    }
    clonedOperation.setParameters(clonedParams);
    clonedOperation.setSecurity(op.getSecurity());
    clonedOperation.setResponses(op.getResponses());

    return clonedOperation;
  }
Ejemplo n.º 2
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;
  }
Ejemplo n.º 3
0
  public void processOperation(
      String resourcePath,
      String httpMethod,
      Operation operation,
      Map<String, List<CodegenOperation>> operations,
      Path path) {
    if (operation != null) {
      if (System.getProperty("debugOperations") != null) {
        LOGGER.debug(
            "processOperation: resourcePath= "
                + resourcePath
                + "\t;"
                + httpMethod
                + " "
                + operation
                + "\n");
      }
      List<String> tags = operation.getTags();
      if (tags == null) {
        tags = new ArrayList<String>();
        tags.add("default");
      }

      /*
       build up a set of parameter "ids" defined at the operation level
       per the swagger 2.0 spec "A unique parameter is defined by a combination of a name and location"
        i'm assuming "location" == "in"
      */
      Set<String> operationParameters = new HashSet<String>();
      if (operation.getParameters() != null) {
        for (Parameter parameter : operation.getParameters()) {
          operationParameters.add(generateParameterId(parameter));
        }
      }

      // need to propagate path level down to the operation
      if (path.getParameters() != null) {
        for (Parameter parameter : path.getParameters()) {
          // skip propagation if a parameter with the same name is already defined at the operation
          // level
          if (!operationParameters.contains(generateParameterId(parameter))) {
            operation.addParameter(parameter);
          }
        }
      }

      for (String tag : tags) {
        CodegenOperation co = null;
        try {
          co =
              config.fromOperation(
                  resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
          co.tags = new ArrayList<String>();
          co.tags.add(sanitizeTag(tag));
          config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);

          List<Map<String, List<String>>> securities = operation.getSecurity();
          if (securities == null && swagger.getSecurity() != null) {
            securities = new ArrayList<Map<String, List<String>>>();
            for (SecurityRequirement sr : swagger.getSecurity()) {
              securities.add(sr.getRequirements());
            }
          }
          if (securities == null || securities.isEmpty()) {
            continue;
          }
          Map<String, SecuritySchemeDefinition> authMethods =
              new HashMap<String, SecuritySchemeDefinition>();
          // NOTE: Use only the first security requirement for now.
          // See the "security" field of "Swagger Object":
          //  https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swagger-object
          //  "there is a logical OR between the security requirements"
          if (securities.size() > 1) {
            LOGGER.warn("More than 1 security requirements are found, using only the first one");
          }
          Map<String, List<String>> security = securities.get(0);
          for (String securityName : security.keySet()) {
            SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
            if (securityDefinition != null) {
              if (securityDefinition instanceof OAuth2Definition) {
                OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
                OAuth2Definition oauth2Operation = new OAuth2Definition();
                oauth2Operation.setType(oauth2Definition.getType());
                oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
                oauth2Operation.setFlow(oauth2Definition.getFlow());
                oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
                oauth2Operation.setScopes(new HashMap<String, String>());
                for (String scope : security.get(securityName)) {
                  if (oauth2Definition.getScopes().containsKey(scope)) {
                    oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
                  }
                }
                authMethods.put(securityName, oauth2Operation);
              } else {
                authMethods.put(securityName, securityDefinition);
              }
            }
          }
          if (!authMethods.isEmpty()) {
            co.authMethods = config.fromSecurity(authMethods);
            co.hasAuthMethods = true;
          }
        } catch (Exception ex) {
          String msg =
              "Could not process operation:\n" //
                  + "  Tag: "
                  + tag
                  + "\n" //
                  + "  Operation: "
                  + operation.getOperationId()
                  + "\n" //
                  + "  Resource: "
                  + httpMethod
                  + " "
                  + resourcePath
                  + "\n" //
                  + "  Definitions: "
                  + swagger.getDefinitions()
                  + "\n" //
                  + "  Exception: "
                  + ex.getMessage();
          throw new RuntimeException(msg, ex);
        }
      }
    }
  }