private void _addReturnElement(Element methodElement, JavaMethod javaMethod) throws Exception { Type returns = javaMethod.getReturns(); if (returns == null) { return; } String returnsValue = returns.getValue(); if (returnsValue.equals("void")) { return; } Element returnElement = methodElement.addElement("return"); DocletTag[] returnDocletTags = javaMethod.getTagsByName("return"); String comment = StringPool.BLANK; if (returnDocletTags.length > 0) { DocletTag returnDocletTag = returnDocletTags[0]; comment = GetterUtil.getString(returnDocletTag.getValue()); DocUtil.add(returnElement, "required", true); } comment = _trimMultilineText(comment); Element commentElement = returnElement.addElement("comment"); commentElement.addCDATA(comment); }
private String extractParameterValue(DocletTag javaParameterTag, String name) { if (!javaParameterTag.getValue().startsWith(name + " ")) { return javaParameterTag.getValue(); } else { return javaParameterTag.getValue().substring(name.length() + 1); } }
private void _addThrowsElement( Element methodElement, Type exception, DocletTag[] throwsDocletTags) { String name = exception.getJavaClass().getName(); String value = null; for (DocletTag throwsDocletTag : throwsDocletTags) { String curValue = throwsDocletTag.getValue(); if (!curValue.startsWith(name)) { continue; } else { curValue = value; break; } } Element throwsElement = methodElement.addElement("throws"); DocUtil.add(throwsElement, "name", name); DocUtil.add(throwsElement, "type", exception.getValue()); if (value != null) { value = value.substring(name.length()); } Element commentElement = throwsElement.addElement("comment"); commentElement.addCDATA(_getCDATA(value)); }
private void _addDocletElements( Element parentElement, AbstractJavaEntity abstractJavaEntity, String name) { DocletTag[] docletTags = abstractJavaEntity.getTagsByName(name); for (DocletTag docletTag : docletTags) { String value = docletTag.getValue(); if (name.equals("author") || name.equals("see") || name.equals("since") || name.equals("version")) { /*if (value.startsWith("Raymond Aug")) { value = "Raymond Aug\u00c3\u00a9"; }*/ DocUtil.add(parentElement, name, value); } else { Element element = parentElement.addElement(name); element.addCDATA(value); } } }
/** * Get ITestNGMethod author(s) string, or class author(s) if no method author is present. Default * return value is "unknown". * * @param className * @param method * @return * @author hzjingcheng */ private String getAuthors(String className, ITestNGMethod method) { JavaClass cls = builder.getClassByName(className); DocletTag[] authors = cls.getTagsByName("author"); // get class authors as default author name String allAuthors = ""; if (authors.length == 0) { allAuthors = "unknown"; } else { for (DocletTag author : authors) { allAuthors += author.getValue() + " "; } } // get method author name JavaMethod[] mtds = cls.getMethods(); for (JavaMethod mtd : mtds) { if (mtd.getName().equals(method.getMethodName())) { authors = mtd.getTagsByName("author"); if (authors.length != 0) { allAuthors = ""; for (DocletTag author : authors) { allAuthors += author.getValue() + " "; } } break; } } return allAuthors.trim(); }
private void _addParamElement( Element methodElement, JavaParameter javaParameter, DocletTag[] paramDocletTags) { String name = javaParameter.getName(); String type = javaParameter.getType().getValue(); String value = null; for (DocletTag paramDocletTag : paramDocletTags) { String curValue = paramDocletTag.getValue(); if (!curValue.startsWith(name)) { continue; } else { curValue = value; break; } } Element paramElement = methodElement.addElement("param"); DocUtil.add(paramElement, "name", name); DocUtil.add(paramElement, "type", type); if (value != null) { value = value.substring(name.length()); } Element commentElement = paramElement.addElement("comment"); commentElement.addCDATA(_getCDATA(value)); }
// 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); } }
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); } }
private void _addDocletElements( Element parentElement, AbstractJavaEntity abstractJavaEntity, String name) throws Exception { DocletTag[] docletTags = abstractJavaEntity.getTagsByName(name); for (DocletTag docletTag : docletTags) { String value = docletTag.getValue(); value = _trimMultilineText(value); value = StringUtil.replace(value, " </", "</"); Element element = parentElement.addElement(name); element.addCDATA(value); } if ((docletTags.length == 0) && name.equals("author")) { Element element = parentElement.addElement(name); element.addCDATA(ServiceBuilder.AUTHOR); } }
// 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); } }