@Override
  public void preprocessSwagger(Swagger swagger) {
    if (swagger != null && swagger.getPaths() != null) {
      for (String pathname : swagger.getPaths().keySet()) {
        Path path = swagger.getPath(pathname);
        if (path.getOperations() != null) {
          for (Operation operation : path.getOperations()) {
            boolean hasFormParameters = false;
            for (Parameter parameter : operation.getParameters()) {
              if (parameter instanceof FormParameter) {
                hasFormParameters = true;
              }
            }

            String defaultContentType =
                hasFormParameters ? "application/x-www-form-urlencoded" : "application/json";
            String contentType =
                operation.getConsumes() == null || operation.getConsumes().isEmpty()
                    ? defaultContentType
                    : operation.getConsumes().get(0);
            String accepts = getAccept(operation);
            operation.setVendorExtension("x-contentType", contentType);
            operation.setVendorExtension("x-accepts", accepts);
          }
        }
      }
    }
  }
Example #2
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;
  }