Exemplo n.º 1
0
 // Checks if the annotations has a value of JSON and returns the string
 // that Swagger requires
 private String getIOType(JavaAnnotation annotation) {
   if (annotation.getNamedParameter("value").toString().equals(JSON)) {
     return "application/json";
   } else if (annotation.getNamedParameter("value").toString().equals(OCTET_STREAM)) {
     return "application/octet_stream";
   }
   return "";
 }
Exemplo n.º 2
0
  // Processes parameters of javaMethod and enters the proper key-values into the methodNode
  private void processParameters(JavaMethod javaMethod, ObjectNode methodNode) {
    ArrayNode parameters = mapper.createArrayNode();
    methodNode.set("parameters", parameters);
    boolean required = true;

    for (JavaParameter javaParameter : javaMethod.getParameters()) {
      ObjectNode individualParameterNode = mapper.createObjectNode();
      Optional<JavaAnnotation> optional =
          javaParameter
              .getAnnotations()
              .stream()
              .filter(
                  annotation ->
                      annotation.getType().getName().equals(PATH_PARAM)
                          || annotation.getType().getName().equals(QUERY_PARAM))
              .findAny();
      JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;

      String annotationName = javaParameter.getName();

      if (pathType != null) { // the parameter is a path or query parameter
        individualParameterNode.put(
            "name", pathType.getNamedParameter("value").toString().replace("\"", ""));
        if (pathType.getType().getName().equals(PATH_PARAM)) {
          individualParameterNode.put("in", "path");
        } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
          individualParameterNode.put("in", "query");
        }
        individualParameterNode.put("type", getType(javaParameter.getType()));
      } else { // the parameter is a body parameter
        individualParameterNode.put("name", annotationName);
        individualParameterNode.put("in", "body");

        // TODO add actual hardcoded schemas and a type
        // body parameters must have a schema associated with them
        ArrayNode schema = mapper.createArrayNode();
        individualParameterNode.set("schema", schema);
      }
      for (DocletTag p : javaMethod.getTagsByName("param")) {
        if (p.getValue().contains(annotationName)) {
          try {
            String description = p.getValue().split(" ", 2)[1].trim();
            if (description.contains("optional")) {
              required = false;
            }
            individualParameterNode.put("description", description);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      individualParameterNode.put("required", required);
      parameters.add(individualParameterNode);
    }
  }
Exemplo n.º 3
0
 // If the annotation has a Path tag, returns the value with leading and
 // trailing double quotes and slash removed.
 private String getPath(JavaAnnotation annotation) {
   String path = annotation.getNamedParameter("value").toString();
   return path == null ? null : path.replaceAll("(^[\\\"/]*|[/\\\"]*$)", "");
 }
Exemplo n.º 4
0
 // Checks if the annotations has a value of JSON and returns the string
 // that Swagger requires
 private String getIOType(JavaAnnotation annotation) {
   if (annotation.getNamedParameter("value").toString().equals(JSON)) {
     return "application/json";
   }
   return "";
 }
Exemplo n.º 5
0
  // Processes parameters of javaMethod and enters the proper key-values into the methodNode
  private void processParameters(
      JavaMethod javaMethod, ObjectNode methodNode, String method, DocletTag tag) {
    ArrayNode parameters = mapper.createArrayNode();
    methodNode.set("parameters", parameters);
    boolean required = true;

    for (JavaParameter javaParameter : javaMethod.getParameters()) {
      ObjectNode individualParameterNode = mapper.createObjectNode();
      Optional<JavaAnnotation> optional =
          javaParameter
              .getAnnotations()
              .stream()
              .filter(
                  annotation ->
                      annotation.getType().getName().equals(PATH_PARAM)
                          || annotation.getType().getName().equals(QUERY_PARAM))
              .findAny();
      JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;

      String annotationName = javaParameter.getName();

      if (pathType != null) { // the parameter is a path or query parameter
        individualParameterNode.put(
            "name", pathType.getNamedParameter("value").toString().replace("\"", ""));
        if (pathType.getType().getName().equals(PATH_PARAM)) {
          individualParameterNode.put("in", "path");
        } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
          individualParameterNode.put("in", "query");
        }
        individualParameterNode.put("type", getType(javaParameter.getType()));
      } else { // the parameter is a body parameter
        individualParameterNode.put("name", annotationName);
        individualParameterNode.put("in", "body");

        // Adds the reference to the Json model for the input
        // that goes in the post or put operation
        if (tag != null
            && (method.toLowerCase().equals("post") || method.toLowerCase().equals("put"))) {
          ObjectNode schema = mapper.createObjectNode();
          tag.getParameters()
              .stream()
              .forEach(
                  param -> {
                    schema.put("$ref", "#/definitions/" + param);
                  });
          individualParameterNode.set("schema", schema);
        }
      }
      for (DocletTag p : javaMethod.getTagsByName("param")) {
        if (p.getValue().contains(annotationName)) {
          try {
            String description = p.getValue().split(" ", 2)[1].trim();
            if (description.contains("optional")) {
              required = false;
            }
            individualParameterNode.put("description", description);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      individualParameterNode.put("required", required);
      parameters.add(individualParameterNode);
    }
  }