Search in sources :

Example 11 with APIResponses

use of org.eclipse.microprofile.openapi.models.responses.APIResponses in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method processJaxRsMethod.

/**
 * Process a single JAX-RS method to produce an OpenAPI Operation.
 * @param openApi
 * @param resource
 * @param method
 * @param methodAnno
 * @param methodType
 * @param resourceTags
 */
private void processJaxRsMethod(OpenAPIImpl openApi, ClassInfo resource, MethodInfo method, AnnotationInstance methodAnno, HttpMethod methodType, Set<String> resourceTags) {
    LOG.debugf("Processing jax-rs method: {0}", method.toString());
    // Figure out the path for the operation.  This is a combination of the App, Resource, and Method @Path annotations
    String path;
    if (method.hasAnnotation(OpenApiConstants.DOTNAME_PATH)) {
        AnnotationInstance pathAnno = method.annotation(OpenApiConstants.DOTNAME_PATH);
        String methodPath = pathAnno.value().asString();
        path = makePath(this.currentAppPath, this.currentResourcePath, methodPath);
    } else {
        path = makePath(this.currentAppPath, this.currentResourcePath);
    }
    // Get or create a PathItem to hold the operation
    PathItem pathItem = ModelUtil.paths(openApi).get(path);
    if (pathItem == null) {
        pathItem = new PathItemImpl();
        ModelUtil.paths(openApi).addPathItem(path, pathItem);
    }
    // Figure out the current @Produces and @Consumes (if any)
    currentConsumes = null;
    currentProduces = null;
    AnnotationInstance consumesAnno = method.annotation(OpenApiConstants.DOTNAME_CONSUMES);
    if (consumesAnno == null) {
        consumesAnno = JandexUtil.getClassAnnotation(method.declaringClass(), OpenApiConstants.DOTNAME_CONSUMES);
    }
    AnnotationInstance producesAnno = method.annotation(OpenApiConstants.DOTNAME_PRODUCES);
    if (producesAnno == null) {
        producesAnno = JandexUtil.getClassAnnotation(method.declaringClass(), OpenApiConstants.DOTNAME_PRODUCES);
    }
    if (consumesAnno != null) {
        AnnotationValue annotationValue = consumesAnno.value();
        if (annotationValue != null) {
            currentConsumes = annotationValue.asStringArray();
        } else {
            currentConsumes = OpenApiConstants.DEFAULT_CONSUMES;
        }
    }
    if (producesAnno != null) {
        AnnotationValue annotationValue = producesAnno.value();
        if (annotationValue != null) {
            currentProduces = annotationValue.asStringArray();
        } else {
            currentProduces = OpenApiConstants.DEFAULT_PRODUCES;
        }
    }
    Operation operation = new OperationImpl();
    // ///////////////////////////////////////
    if (method.hasAnnotation(OpenApiConstants.DOTNAME_OPERATION)) {
        AnnotationInstance operationAnno = method.annotation(OpenApiConstants.DOTNAME_OPERATION);
        // If the operation is marked as hidden, just bail here because we don't want it as part of the model.
        if (operationAnno.value(OpenApiConstants.PROP_HIDDEN) != null && operationAnno.value(OpenApiConstants.PROP_HIDDEN).asBoolean()) {
            return;
        }
        // Otherwise, set various bits of meta-data from the values in the @Operation annotation
        operation.setSummary(JandexUtil.stringValue(operationAnno, OpenApiConstants.PROP_SUMMARY));
        operation.setDescription(JandexUtil.stringValue(operationAnno, OpenApiConstants.PROP_DESCRIPTION));
        operation.setOperationId(JandexUtil.stringValue(operationAnno, OpenApiConstants.PROP_OPERATION_ID));
        operation.setDeprecated(JandexUtil.booleanValue(operationAnno, OpenApiConstants.PROP_DEPRECATED));
    }
    // Process tags - @Tag and @Tags annotations combines with the resource tags we've already found (passed in)
    // ///////////////////////////////////////
    boolean hasOpTags = false;
    Set<String> tags = new HashSet<>();
    if (method.hasAnnotation(OpenApiConstants.DOTNAME_TAG)) {
        hasOpTags = true;
        AnnotationInstance tagAnno = method.annotation(OpenApiConstants.DOTNAME_TAG);
        if (JandexUtil.isRef(tagAnno)) {
            String tagRef = JandexUtil.stringValue(tagAnno, OpenApiConstants.PROP_REF);
            tags.add(tagRef);
        } else if (JandexUtil.isEmpty(tagAnno)) {
        // Nothing to do here.  The @Tag() was empty.
        } else {
            Tag tag = readTag(tagAnno);
            if (tag.getName() != null) {
                openApi.addTag(tag);
                tags.add(tag.getName());
            }
        }
    }
    if (method.hasAnnotation(OpenApiConstants.DOTNAME_TAGS)) {
        hasOpTags = true;
        AnnotationInstance tagsAnno = method.annotation(OpenApiConstants.DOTNAME_TAGS);
        AnnotationValue tagsArrayVal = tagsAnno.value();
        if (tagsArrayVal != null) {
            AnnotationInstance[] tagsArray = tagsArrayVal.asNestedArray();
            for (AnnotationInstance tagAnno : tagsArray) {
                if (JandexUtil.isRef(tagAnno)) {
                    String tagRef = JandexUtil.stringValue(tagAnno, OpenApiConstants.PROP_REF);
                    tags.add(tagRef);
                } else {
                    Tag tag = readTag(tagAnno);
                    if (tag.getName() != null) {
                        openApi.addTag(tag);
                        tags.add(tag.getName());
                    }
                }
            }
        }
        List<String> listValue = JandexUtil.stringListValue(tagsAnno, OpenApiConstants.PROP_REFS);
        if (listValue != null) {
            tags.addAll(listValue);
        }
    }
    if (!hasOpTags) {
        tags.addAll(resourceTags);
    }
    if (!tags.isEmpty()) {
        operation.setTags(new ArrayList<>(tags));
    }
    // Process @Parameter annotations
    // ///////////////////////////////////////
    List<AnnotationInstance> parameterAnnotations = JandexUtil.getRepeatableAnnotation(method, OpenApiConstants.DOTNAME_PARAMETER, OpenApiConstants.DOTNAME_PARAMETERS);
    for (AnnotationInstance annotation : parameterAnnotations) {
        Parameter parameter = readParameter(annotation);
        if (parameter == null) {
            // Param was hidden
            continue;
        }
        AnnotationTarget target = annotation.target();
        // If the target is METHOD_PARAMETER, then the @Parameter is on one of the method's arguments (THIS ONE WE CARE ABOUT)
        if (target != null && target.kind() == Kind.METHOD_PARAMETER) {
            In in = parameterIn(target.asMethodParameter());
            parameter.setIn(in);
            // if the Parameter model we read does *NOT* have a Schema at this point, then create one from the method argument's type
            if (!ModelUtil.parameterHasSchema(parameter)) {
                Type paramType = JandexUtil.getMethodParameterType(method, target.asMethodParameter().position());
                Schema schema = typeToSchema(paramType);
                ModelUtil.setParameterSchema(parameter, schema);
            }
        } else {
        // TODO if the @Parameter is on the method, we could perhaps find the one it refers to by name
        // and use its type to create a Schema (if missing)
        }
        operation.addParameter(parameter);
    }
    // Now process any jax-rs parameters that were NOT annotated with @Parameter (do not yet exist in the model)
    List<Type> parameters = method.parameters();
    for (int idx = 0; idx < parameters.size(); idx++) {
        JaxRsParameterInfo paramInfo = JandexUtil.getMethodParameterJaxRsInfo(method, idx);
        if (paramInfo != null && !ModelUtil.operationHasParameter(operation, paramInfo.name)) {
            Type paramType = parameters.get(idx);
            Parameter parameter = new ParameterImpl();
            parameter.setName(paramInfo.name);
            parameter.setIn(paramInfo.in);
            parameter.setRequired(true);
            Schema schema = typeToSchema(paramType);
            parameter.setSchema(schema);
            operation.addParameter(parameter);
        }
    }
    // TODO @Parameter can be located on a field - what does that mean?
    // TODO need to handle the case where we have @BeanParam annotations
    // Process any @RequestBody annotation
    // ///////////////////////////////////////
    // note: the @RequestBody annotation can be found on a method argument *or* on the method
    List<AnnotationInstance> requestBodyAnnotations = JandexUtil.getRepeatableAnnotation(method, OpenApiConstants.DOTNAME_REQUEST_BODY, null);
    for (AnnotationInstance annotation : requestBodyAnnotations) {
        RequestBody requestBody = readRequestBody(annotation);
        // TODO if the method argument type is Request, don't generate a Schema!
        if (!ModelUtil.requestBodyHasSchema(requestBody)) {
            Type requestBodyType = null;
            if (annotation.target().kind() == AnnotationTarget.Kind.METHOD_PARAMETER) {
                requestBodyType = JandexUtil.getMethodParameterType(method, annotation.target().asMethodParameter().position());
            } else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) {
                requestBodyType = JandexUtil.getRequestBodyParameterClassType(method);
            }
            if (requestBodyType != null) {
                Schema schema = typeToSchema(requestBodyType);
                ModelUtil.setRequestBodySchema(requestBody, schema, currentConsumes);
            }
        }
        operation.setRequestBody(requestBody);
    }
    // method declares that it @Consumes data
    if (operation.getRequestBody() == null && currentConsumes != null) {
        Type requestBodyType = JandexUtil.getRequestBodyParameterClassType(method);
        if (requestBodyType != null) {
            Schema schema = typeToSchema(requestBodyType);
            if (schema != null) {
                RequestBody requestBody = new RequestBodyImpl();
                ModelUtil.setRequestBodySchema(requestBody, schema, currentConsumes);
                operation.setRequestBody(requestBody);
            }
        }
    }
    // Process @APIResponse annotations
    // ///////////////////////////////////////
    List<AnnotationInstance> apiResponseAnnotations = JandexUtil.getRepeatableAnnotation(method, OpenApiConstants.DOTNAME_API_RESPONSE, OpenApiConstants.DOTNAME_API_RESPONSES);
    for (AnnotationInstance annotation : apiResponseAnnotations) {
        String responseCode = JandexUtil.stringValue(annotation, OpenApiConstants.PROP_RESPONSE_CODE);
        if (responseCode == null) {
            responseCode = APIResponses.DEFAULT;
        }
        APIResponse response = readResponse(annotation);
        APIResponses responses = ModelUtil.responses(operation);
        responses.addApiResponse(responseCode, response);
    }
    // If there are no responses from annotations, try to create a response from the method return value.
    if (operation.getResponses() == null || operation.getResponses().isEmpty()) {
        createResponseFromJaxRsMethod(method, operation);
    }
    // Process @SecurityRequirement annotations
    // /////////////////////////////////////////
    List<AnnotationInstance> securityRequirementAnnotations = JandexUtil.getRepeatableAnnotation(method, OpenApiConstants.DOTNAME_SECURITY_REQUIREMENT, OpenApiConstants.DOTNAME_SECURITY_REQUIREMENTS);
    securityRequirementAnnotations.addAll(JandexUtil.getRepeatableAnnotation(resource, OpenApiConstants.DOTNAME_SECURITY_REQUIREMENT, OpenApiConstants.DOTNAME_SECURITY_REQUIREMENTS));
    for (AnnotationInstance annotation : securityRequirementAnnotations) {
        SecurityRequirement requirement = readSecurityRequirement(annotation);
        if (requirement != null) {
            operation.addSecurityRequirement(requirement);
        }
    }
    // Process @Callback annotations
    // ///////////////////////////////////////
    List<AnnotationInstance> callbackAnnotations = JandexUtil.getRepeatableAnnotation(method, OpenApiConstants.DOTNAME_CALLBACK, OpenApiConstants.DOTNAME_CALLBACKS);
    Map<String, Callback> callbacks = new LinkedHashMap<>();
    for (AnnotationInstance annotation : callbackAnnotations) {
        String name = JandexUtil.stringValue(annotation, OpenApiConstants.PROP_NAME);
        if (name == null && JandexUtil.isRef(annotation)) {
            name = JandexUtil.nameFromRef(annotation);
        }
        if (name != null) {
            callbacks.put(name, readCallback(annotation));
        }
        if (!callbacks.isEmpty()) {
            operation.setCallbacks(callbacks);
        }
    }
    // Process @Server annotations
    // /////////////////////////////////
    List<AnnotationInstance> serverAnnotations = JandexUtil.getRepeatableAnnotation(method, OpenApiConstants.DOTNAME_SERVER, OpenApiConstants.DOTNAME_SERVERS);
    if (serverAnnotations.isEmpty()) {
        serverAnnotations.addAll(JandexUtil.getRepeatableAnnotation(method.declaringClass(), OpenApiConstants.DOTNAME_SERVER, OpenApiConstants.DOTNAME_SERVERS));
    }
    for (AnnotationInstance annotation : serverAnnotations) {
        Server server = readServer(annotation);
        operation.addServer(server);
    }
    // /////////////////////////////////////////
    switch(methodType) {
        case DELETE:
            pathItem.setDELETE(operation);
            break;
        case GET:
            pathItem.setGET(operation);
            break;
        case HEAD:
            pathItem.setHEAD(operation);
            break;
        case OPTIONS:
            pathItem.setOPTIONS(operation);
            break;
        case PATCH:
            pathItem.setPATCH(operation);
            break;
        case POST:
            pathItem.setPOST(operation);
            break;
        case PUT:
            pathItem.setPUT(operation);
            break;
        case TRACE:
            pathItem.setTRACE(operation);
            break;
        default:
            break;
    }
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) Server(org.eclipse.microprofile.openapi.models.servers.Server) In(org.eclipse.microprofile.openapi.models.parameters.Parameter.In) Schema(org.eclipse.microprofile.openapi.models.media.Schema) Operation(org.eclipse.microprofile.openapi.models.Operation) LinkedHashMap(java.util.LinkedHashMap) PathItem(org.eclipse.microprofile.openapi.models.PathItem) OperationImpl(org.wildfly.swarm.microprofile.openapi.api.models.OperationImpl) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) HashSet(java.util.HashSet) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody) AnnotationTarget(org.jboss.jandex.AnnotationTarget) RequestBodyImpl(org.wildfly.swarm.microprofile.openapi.api.models.parameters.RequestBodyImpl) ClassType(org.jboss.jandex.ClassType) RefType(org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.RefType) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) Type(org.jboss.jandex.Type) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) ParameterImpl(org.wildfly.swarm.microprofile.openapi.api.models.parameters.ParameterImpl) AnnotationValue(org.jboss.jandex.AnnotationValue) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) Tag(org.eclipse.microprofile.openapi.models.tags.Tag) JaxRsParameterInfo(org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.JaxRsParameterInfo) PathItemImpl(org.wildfly.swarm.microprofile.openapi.api.models.PathItemImpl) AnnotationInstance(org.jboss.jandex.AnnotationInstance) SecurityRequirement(org.eclipse.microprofile.openapi.models.security.SecurityRequirement)

Example 12 with APIResponses

use of org.eclipse.microprofile.openapi.models.responses.APIResponses in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method readCallbackOperationResponses.

/**
 * Reads an array of APIResponse annotations into an {@link APIResponses} model.
 * @param value
 */
private APIResponses readCallbackOperationResponses(AnnotationValue value) {
    if (value == null) {
        return null;
    }
    LOG.debug("Processing a list of @APIResponse annotations into an APIResponses model.");
    APIResponses responses = new APIResponsesImpl();
    AnnotationInstance[] nestedArray = value.asNestedArray();
    for (AnnotationInstance nested : nestedArray) {
        String responseCode = JandexUtil.stringValue(nested, OpenApiConstants.PROP_RESPONSE_CODE);
        if (responseCode != null) {
            responses.put(responseCode, readResponse(nested));
        }
    }
    return responses;
}
Also used : APIResponsesImpl(org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponsesImpl) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 13 with APIResponses

use of org.eclipse.microprofile.openapi.models.responses.APIResponses in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method createResponseFromJaxRsMethod.

/**
 * Called when a jax-rs method's APIResponse annotations have all been processed but
 * no response was actually created for the operation.  This method will create a response
 * from the method information and add it to the given operation.  It will try to do this
 * by examining the method's return value and the type of operation (GET, PUT, POST, DELETE).
 *
 * If there is a return value of some kind (a non-void return type) then the response code
 * is assumed to be 200.
 *
 * If there not a return value (void return type) then either a 201 or 204 is returned,
 * depending on the type of request.
 *
 * TODO generate responses for each checked exception?
 * @param method
 * @param operation
 */
private void createResponseFromJaxRsMethod(MethodInfo method, Operation operation) {
    Type returnType = method.returnType();
    Schema schema;
    APIResponses responses;
    APIResponse response;
    ContentImpl content;
    if (returnType.kind() == Type.Kind.VOID) {
        String code = "204";
        if (method.hasAnnotation(OpenApiConstants.DOTNAME_POST)) {
            code = "201";
        }
        responses = ModelUtil.responses(operation);
        response = new APIResponseImpl();
        responses.addApiResponse(code, response);
    } else {
        schema = typeToSchema(returnType);
        responses = ModelUtil.responses(operation);
        response = new APIResponseImpl();
        content = new ContentImpl();
        String[] produces = this.currentProduces;
        if (produces == null || produces.length == 0) {
            produces = OpenApiConstants.DEFAULT_PRODUCES;
        }
        for (String producesType : produces) {
            MediaType mt = new MediaTypeImpl();
            mt.setSchema(schema);
            content.addMediaType(producesType, mt);
        }
        response.setContent(content);
        responses.addApiResponse("200", response);
    }
}
Also used : ClassType(org.jboss.jandex.ClassType) RefType(org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.RefType) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) Type(org.jboss.jandex.Type) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) MediaTypeImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) APIResponseImpl(org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl) ContentImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)

Example 14 with APIResponses

use of org.eclipse.microprofile.openapi.models.responses.APIResponses in project wildfly-swarm by wildfly-swarm.

the class MergeUtil method mergeMaps.

/**
 * Merges two Maps.  Any values missing from Map1 but present in Map2 will be added.  If a value
 * is present in both maps, it will be overridden or merged.
 * @param values1
 * @param values2
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Map mergeMaps(Map values1, Map values2) {
    if (values1 == null && values2 == null) {
        return null;
    }
    if (values1 != null && values2 == null) {
        return values1;
    }
    if (values1 == null && values2 != null) {
        return values2;
    }
    for (Object key : values2.keySet()) {
        if (values1.containsKey(key)) {
            Object pval1 = values1.get(key);
            Object pval2 = values2.get(key);
            if (pval1 instanceof Map) {
                values1.put(key, mergeMaps((Map) pval1, (Map) pval2));
            } else if (pval1 instanceof List) {
                values1.put(key, mergeLists((List) pval1, (List) pval2));
            } else if (pval1 instanceof Constructible) {
                values1.put(key, mergeObjects(pval1, pval2));
            } else {
                values1.put(key, pval2);
            }
        } else {
            Object pval2 = values2.get(key);
            values1.put(key, pval2);
        }
    }
    if (values1 instanceof Constructible) {
        if (values1 instanceof Reference) {
            Reference ref1 = (Reference) values1;
            Reference ref2 = (Reference) values2;
            if (ref2.getRef() != null) {
                ref1.setRef(ref2.getRef());
            }
        }
        if (values1 instanceof Extensible) {
            Extensible extensible1 = (Extensible) values1;
            Extensible extensible2 = (Extensible) values2;
            extensible1.setExtensions(mergeMaps(extensible1.getExtensions(), extensible2.getExtensions()));
        }
        if (values1 instanceof APIResponses) {
            APIResponses responses1 = (APIResponses) values1;
            APIResponses responses2 = (APIResponses) values2;
            responses1.defaultValue(mergeObjects(responses1.getDefault(), responses2.getDefault()));
        }
    }
    return values1;
}
Also used : Constructible(org.eclipse.microprofile.openapi.models.Constructible) Extensible(org.eclipse.microprofile.openapi.models.Extensible) Reference(org.eclipse.microprofile.openapi.models.Reference) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)14 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)9 Test (org.junit.Test)8 OpenApiApplicationTest (fish.payara.microprofile.openapi.test.app.OpenApiApplicationTest)7 Schema (org.eclipse.microprofile.openapi.models.media.Schema)5 APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)5 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)4 List (java.util.List)3 Operation (org.eclipse.microprofile.openapi.models.Operation)3 PathItem (org.eclipse.microprofile.openapi.models.PathItem)3 Callback (org.eclipse.microprofile.openapi.models.callbacks.Callback)3 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)3 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)3 SecurityRequirement (org.eclipse.microprofile.openapi.models.security.SecurityRequirement)3 Server (org.eclipse.microprofile.openapi.models.servers.Server)3 Tag (org.eclipse.microprofile.openapi.models.tags.Tag)3 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)2 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)2 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)2 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)2