Search in sources :

Example 16 with MediaType

use of org.eclipse.microprofile.openapi.models.media.MediaType 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 17 with MediaType

use of org.eclipse.microprofile.openapi.models.media.MediaType 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 18 with MediaType

use of org.eclipse.microprofile.openapi.models.media.MediaType 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

MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)18 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)5 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)4 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)4 Content (org.eclipse.microprofile.openapi.models.media.Content)4 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)4 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)4 APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)4 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)4 MediaTypeImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl)4 ContentImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)3 ExampleImpl (fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)2 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)2 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)2 APIResponsesImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl)2 Encoding (org.eclipse.microprofile.openapi.models.media.Encoding)2 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)2 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)2 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)1