Search in sources :

Example 1 with JavaDocTag

use of org.jboss.forge.roaster.model.JavaDocTag in project herd by FINRAOS.

the class RestControllerProcessor method processRestControllerMethod.

/**
 * Processes a method in a REST controller which represents an endpoint, that is, it is annotated with RequestMapping.
 *
 * @param method the method.
 * @param classRequestMapping the parent.
 * @param tagName the tag name.
 * @param methodSource the method source information.
 *
 * @throws MojoExecutionException if any errors were encountered.
 */
// CollectionUtils doesn't work with generics.
@SuppressWarnings("unchecked")
private void processRestControllerMethod(Method method, RequestMapping classRequestMapping, String tagName, MethodSource<JavaClassSource> methodSource) throws MojoExecutionException {
    log.debug("Processing method \"" + method.getName() + "\".");
    // Build a map of each parameter name to its description from the method Javadoc (i.e. all @param and @return values).
    Map<String, String> methodParamDescriptions = new HashMap<>();
    JavaDocSource<MethodSource<JavaClassSource>> javaDocSource = methodSource.getJavaDoc();
    List<JavaDocTag> tags = javaDocSource.getTags();
    for (JavaDocTag javaDocTag : tags) {
        processJavaDocTag(javaDocTag, methodParamDescriptions);
    }
    List<String> produces = Collections.emptyList();
    List<String> consumes = Collections.emptyList();
    List<RequestMethod> requestMethods = Collections.emptyList();
    List<String> uris = Collections.emptyList();
    // If a class request mapping exists, use it as the default.
    if (classRequestMapping != null) {
        produces = CollectionUtils.arrayToList(classRequestMapping.produces());
        consumes = CollectionUtils.arrayToList(classRequestMapping.consumes());
        requestMethods = CollectionUtils.arrayToList(classRequestMapping.method());
        uris = CollectionUtils.arrayToList(classRequestMapping.value());
    }
    // Get the API Operation and see if this endpoint is hidden.
    ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
    boolean hidden = apiOperation != null && apiOperation.hidden();
    // Only process methods that have a RequestMapping annotation.
    RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class);
    if ((methodRequestMapping != null) && (!hidden)) {
        log.debug("Method \"" + method.getName() + "\" is a RequestMapping.");
        // Override values with method level ones if present.
        requestMethods = getClassOrMethodValue(requestMethods, CollectionUtils.arrayToList(methodRequestMapping.method()));
        uris = getClassOrMethodValue(uris, CollectionUtils.arrayToList(methodRequestMapping.value()));
        produces = getClassOrMethodValue(produces, CollectionUtils.arrayToList(methodRequestMapping.produces()));
        consumes = getClassOrMethodValue(consumes, CollectionUtils.arrayToList(methodRequestMapping.consumes()));
        // Perform validation.
        if (requestMethods.isEmpty()) {
            log.warn("No request method defined for method \"" + method.getName() + "\". Skipping...");
            return;
        }
        if (uris.isEmpty()) {
            log.warn("No URI defined for method \"" + method.getName() + "\". Skipping...");
            return;
        }
        if (uris.size() > 1) {
            log.warn(uris.size() + " URI's found for method \"" + method.getName() + "\". Only processing the first one.");
        }
        if (requestMethods.size() > 1) {
            log.warn(uris.size() + " request methods found for method \"" + method.getName() + "\". Only processing the first one.");
        }
        String uri = uris.get(0).trim();
        Path path = swagger.getPath(uri);
        if (path == null) {
            path = new Path();
            swagger.path(uri, path);
        }
        // Get the method summary from the ApiOperation annotation or use the method name if the annotation doesn't exist.
        String methodSummary = method.getName();
        if (apiOperation != null) {
            methodSummary = apiOperation.value();
        }
        Operation operation = new Operation();
        operation.tag(tagName);
        operation.summary(methodSummary);
        if (javaDocSource.getText() != null) {
            // Process the method description.
            Javadoc javadoc = (Javadoc) javaDocSource.getInternal();
            List<TagElement> tagList = javadoc.tags();
            StringBuilder stringBuilder = new StringBuilder();
            for (TagElement tagElement : tagList) {
                // else available (e.g. @link, etc.).
                if (tagElement.getTagName() == null) {
                    processFragments(tagElement.fragments(), stringBuilder);
                }
            }
            // The string builder has the final method text to use.
            operation.description(stringBuilder.toString());
            setOperationId(tagName, method, operation);
        }
        if (!produces.isEmpty()) {
            operation.setProduces(produces);
        }
        if (!consumes.isEmpty()) {
            operation.setConsumes(consumes);
        }
        // HTTP method MUST be lower cased
        path.set(requestMethods.get(0).name().toLowerCase(), operation);
        // names.
        for (ParameterSource<JavaClassSource> parameterSource : methodSource.getParameters()) {
            processRestMethodParameter(parameterSource, operation, methodParamDescriptions);
        }
        // Process the return value.
        processRestMethodReturnValue(method.getReturnType(), operation, methodParamDescriptions.get("@return"));
    } else {
        log.debug("Skipping method \"" + method.getName() + "\" because it is either not a RequestMapping or it is hidden.");
    }
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) JavaDocTag(org.jboss.forge.roaster.model.JavaDocTag) Javadoc(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Javadoc) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ApiOperation(io.swagger.annotations.ApiOperation) Operation(io.swagger.models.Operation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ApiOperation(io.swagger.annotations.ApiOperation) TagElement(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.TagElement)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 HashMap (java.util.HashMap)1 Javadoc (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Javadoc)1 TagElement (org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.TagElement)1 JavaDocTag (org.jboss.forge.roaster.model.JavaDocTag)1 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)1 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1