Search in sources :

Example 1 with ContentImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiParser method readContent.

/**
 * Reads a {@link Content} OpenAPI node.
 * @param node
 */
private Content readContent(JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    ContentImpl model = new ContentImpl();
    for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
        String fieldName = fieldNames.next();
        model.addMediaType(fieldName, readMediaType(node.get(fieldName)));
    }
    return model;
}
Also used : ContentImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)

Example 2 with ContentImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method readContent.

/**
 * Reads a single Content annotation into a model.  The value in this case is an array of
 * Content annotations.
 * @param value
 */
private Content readContent(AnnotationValue value, ContentDirection direction) {
    if (value == null) {
        return null;
    }
    LOG.debug("Processing a single @Content annotation.");
    Content content = new ContentImpl();
    AnnotationInstance[] nestedArray = value.asNestedArray();
    for (AnnotationInstance nested : nestedArray) {
        String contentType = JandexUtil.stringValue(nested, OpenApiConstants.PROP_MEDIA_TYPE);
        MediaType mediaTypeModel = readMediaType(nested);
        if (contentType == null) {
            // If the content type is not provided in the @Content annotation, then
            // we assume it applies to all the jax-rs method's @Consumes or @Produces
            String[] mimeTypes = {};
            if (direction == ContentDirection.Input && currentConsumes != null) {
                mimeTypes = currentConsumes;
            }
            if (direction == ContentDirection.Output && currentProduces != null) {
                mimeTypes = currentProduces;
            }
            if (direction == ContentDirection.Parameter) {
                mimeTypes = OpenApiConstants.DEFAULT_PARAMETER_MEDIA_TYPES;
            }
            for (String mimeType : mimeTypes) {
                content.addMediaType(mimeType, mediaTypeModel);
            }
        } else {
            content.addMediaType(contentType, mediaTypeModel);
        }
    }
    return content;
}
Also used : Content(org.eclipse.microprofile.openapi.models.media.Content) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) ContentImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 3 with ContentImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl 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 4 with ContentImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl in project wildfly-swarm by wildfly-swarm.

the class ModelUtil method setRequestBodySchema.

/**
 * Sets the given {@link Schema} on the given {@link RequestBody}.
 * @param requestBody
 * @param schema
 * @param mediaTypes
 */
public static void setRequestBodySchema(RequestBody requestBody, Schema schema, String[] mediaTypes) {
    Content content = requestBody.getContent();
    if (content == null) {
        content = new ContentImpl();
        requestBody.setContent(content);
    }
    if (content.isEmpty()) {
        String[] requestBodyTypes = OpenApiConstants.DEFAULT_REQUEST_BODY_TYPES;
        if (mediaTypes != null && mediaTypes.length > 0) {
            requestBodyTypes = mediaTypes;
        }
        for (String mediaTypeName : requestBodyTypes) {
            MediaType mediaType = new MediaTypeImpl();
            mediaType.setSchema(schema);
            content.addMediaType(mediaTypeName, mediaType);
        }
        return;
    }
    for (String mediaTypeName : content.keySet()) {
        MediaType mediaType = content.get(mediaTypeName);
        mediaType.setSchema(schema);
    }
}
Also used : Content(org.eclipse.microprofile.openapi.models.media.Content) MediaTypeImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) ContentImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)

Aggregations

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