Search in sources :

Example 1 with APIResponseImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl 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 2 with APIResponseImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method readResponse.

/**
 * Reads a APIResponse annotation into a model.
 * @param annotation
 */
private APIResponse readResponse(AnnotationInstance annotation) {
    if (annotation == null) {
        return null;
    }
    LOG.debug("Processing a single @Response annotation.");
    APIResponse response = new APIResponseImpl();
    response.setDescription(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_DESCRIPTION));
    response.setHeaders(readHeaders(annotation.value(OpenApiConstants.PROP_HEADERS)));
    response.setLinks(readLinks(annotation.value(OpenApiConstants.PROP_LINKS)));
    response.setContent(readContent(annotation.value(OpenApiConstants.PROP_CONTENT), ContentDirection.Output));
    response.setRef(JandexUtil.refValue(annotation, RefType.Response));
    return response;
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) APIResponseImpl(org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl)

Example 3 with APIResponseImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiParser method readAPIResponse.

/**
 * Reads a {@link APIResponse} OpenAPI node.
 * @param node
 */
private APIResponse readAPIResponse(JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    APIResponseImpl model = new APIResponseImpl();
    model.setRef(JsonUtil.stringProperty(node, OpenApiConstants.PROP_$REF));
    model.setDescription(JsonUtil.stringProperty(node, OpenApiConstants.PROP_DESCRIPTION));
    model.setHeaders(readHeaders(node.get(OpenApiConstants.PROP_HEADERS)));
    model.setContent(readContent(node.get(OpenApiConstants.PROP_CONTENT)));
    model.setLinks(readLinks(node.get(OpenApiConstants.PROP_LINKS)));
    readExtensions(node, model);
    return model;
}
Also used : APIResponseImpl(org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl)

Aggregations

APIResponseImpl (org.wildfly.swarm.microprofile.openapi.api.models.responses.APIResponseImpl)3 APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)2 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)1 Schema (org.eclipse.microprofile.openapi.models.media.Schema)1 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)1 ClassType (org.jboss.jandex.ClassType)1 Type (org.jboss.jandex.Type)1 ContentImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)1 MediaTypeImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl)1 RefType (org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.RefType)1