private void processRestMethod( JavaMethod javaMethod, String method, Map<String, ObjectNode> pathMap, String resourcePath, ArrayNode tagArray, ObjectNode definitions) { String fullPath = resourcePath, consumes = "", produces = "", comment = javaMethod.getComment(); DocletTag tag = javaMethod.getTagByName("rsModel"); for (JavaAnnotation annotation : javaMethod.getAnnotations()) { String name = annotation.getType().getName(); if (name.equals(PATH)) { fullPath = resourcePath + "/" + getPath(annotation); fullPath = fullPath.replaceFirst("^//", "/"); } if (name.equals(CONSUMES)) { consumes = getIOType(annotation); } if (name.equals(PRODUCES)) { produces = getIOType(annotation); } } ObjectNode methodNode = mapper.createObjectNode(); methodNode.set("tags", tagArray); addSummaryDescriptions(methodNode, comment); addJsonSchemaDefinition(definitions, tag); addJsonSchemaDefinition(definitions, tag); processParameters(javaMethod, methodNode, method, tag); processConsumesProduces(methodNode, "consumes", consumes); processConsumesProduces(methodNode, "produces", produces); if (tag == null || ((method.toLowerCase().equals("post") || method.toLowerCase().equals("put")) && !(tag.getParameters().size() > 1))) { addResponses(methodNode, tag, false); } else { addResponses(methodNode, tag, true); } ObjectNode operations = pathMap.get(fullPath); if (operations == null) { operations = mapper.createObjectNode(); operations.set(method, methodNode); pathMap.put(fullPath, operations); } else { operations.set(method, methodNode); } }
// Temporary solution to add responses to a method private void addResponses(ObjectNode methodNode, DocletTag tag, boolean responseJson) { ObjectNode responses = mapper.createObjectNode(); methodNode.set("responses", responses); ObjectNode success = mapper.createObjectNode(); success.put("description", "successful operation"); responses.set("200", success); if (tag != null && responseJson) { ObjectNode schema = mapper.createObjectNode(); tag.getParameters().stream().forEach(param -> schema.put("$ref", "#/definitions/" + param)); success.set("schema", schema); } ObjectNode defaultObj = mapper.createObjectNode(); defaultObj.put("description", "Unexpected error"); responses.set("default", defaultObj); }
private void addJsonSchemaDefinition(ObjectNode definitions, DocletTag tag) { File definitionsDirectory = new File(srcDirectory + "/src/main/resources/definitions"); if (tag != null) { tag.getParameters() .stream() .forEach( param -> { try { File config = new File(definitionsDirectory.getAbsolutePath() + "/" + param + ".json"); String lines = Files.readLines(config, Charsets.UTF_8) .stream() .reduce((t, u) -> t + u) .get(); definitions.putPOJO(param, lines); } catch (IOException e) { e.printStackTrace(); } }); } }
// 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); } }