Search in sources :

Example 1 with MediaTypeImpl

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

the class OpenApiParser method readMediaType.

/**
 * Reads a {@link MediaType} OpenAPI node.
 * @param node
 * @return
 */
private MediaType readMediaType(JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    MediaTypeImpl model = new MediaTypeImpl();
    model.setSchema(readSchema(node.get(OpenApiConstants.PROP_SCHEMA)));
    model.setExample(readObject(node.get(OpenApiConstants.PROP_EXAMPLE)));
    model.setExamples(readExamples(node.get(OpenApiConstants.PROP_EXAMPLES)));
    model.setEncoding(readEncodings(node.get(OpenApiConstants.PROP_ENCODING)));
    readExtensions(node, model);
    return model;
}
Also used : MediaTypeImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl)

Example 2 with MediaTypeImpl

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

the class OpenApiAnnotationScanner method readMediaType.

/**
 * Reads a single Content annotation into a {@link MediaType} model.
 * @param nested
 */
private MediaType readMediaType(AnnotationInstance annotation) {
    if (annotation == null) {
        return null;
    }
    LOG.debug("Processing a single @Content annotation as a MediaType.");
    MediaType mediaType = new MediaTypeImpl();
    mediaType.setExamples(readExamples(annotation.value(OpenApiConstants.PROP_EXAMPLES)));
    mediaType.setSchema(readSchema(annotation.value(OpenApiConstants.PROP_SCHEMA)));
    mediaType.setEncoding(readEncodings(annotation.value(OpenApiConstants.PROP_ENCODING)));
    return mediaType;
}
Also used : MediaTypeImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType)

Example 3 with MediaTypeImpl

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

the class ModelUtil method setParameterSchema.

/**
 * Sets the given {@link Schema} on the given {@link Parameter}.  This is tricky
 * because the paramater may EITHER have a schema property or it may have a
 * {@link Content} child which itself has zero or more {@link MediaType} children
 * which will contain the {@link Schema}.
 *
 * The OpenAPI specification requires that a parameter have *either* a schema
 * or a content, but not both.
 * @param parameter
 * @param schema
 */
public static void setParameterSchema(Parameter parameter, Schema schema) {
    if (schema == null) {
        return;
    }
    if (parameter.getContent() == null) {
        parameter.schema(schema);
        return;
    }
    Content content = parameter.getContent();
    if (content.isEmpty()) {
        String[] defMediaTypes = OpenApiConstants.DEFAULT_PARAMETER_MEDIA_TYPES;
        for (String mediaTypeName : defMediaTypes) {
            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)

Example 4 with MediaTypeImpl

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

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

MediaTypeImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl)5 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)4 Content (org.eclipse.microprofile.openapi.models.media.Content)2 ContentImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)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 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