Search in sources :

Example 1 with MediaType

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

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

use of org.eclipse.microprofile.openapi.models.media.MediaType in project wildfly-swarm by wildfly-swarm.

the class FilterUtil method filterContent.

/**
 * Filters the given model.
 * @param filter
 * @param model
 */
private static void filterContent(OASFilter filter, Content model) {
    if (model == null) {
        return;
    }
    Collection<String> keys = new ArrayList<>(model.keySet());
    for (String key : keys) {
        MediaType childModel = model.get(key);
        filterMediaType(filter, childModel);
    }
}
Also used : ArrayList(java.util.ArrayList) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType)

Example 4 with MediaType

use of org.eclipse.microprofile.openapi.models.media.MediaType in project Payara by payara.

the class ApplicationProcessor method insertDefaultResponse.

/**
 * Creates a new {@link APIResponse} to model the default response of a
 * {@link Method}, and inserts it into the {@link Operation} responses.
 *
 * @param context the API context.
 * @param operation the {@link Operation} to add the default response to.
 * @param method the {@link Method} to model the default response on.
 * @return the newly created {@link APIResponse}.
 */
private void insertDefaultResponse(ApiContext context, OperationImpl operation, MethodModel method) {
    final APIResponsesImpl responses = new APIResponsesImpl();
    operation.setResponses(responses);
    // Add the default response
    APIResponse defaultResponse = new APIResponseImpl();
    responses.addAPIResponse(APIResponses.DEFAULT, defaultResponse);
    defaultResponse.setDescription("Default Response.");
    // Configure the default response with a wildcard mediatype
    MediaType mediaType = new MediaTypeImpl().schema(createSchema(context, method.getReturnType()));
    defaultResponse.getContent().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, mediaType);
    // Add responses for the applicable declared exceptions
    for (String exceptionType : method.getExceptionTypes()) {
        final APIResponseImpl mappedResponse = (APIResponseImpl) context.getMappedExceptionResponses().get(exceptionType);
        if (mappedResponse != null) {
            final String responseCode = mappedResponse.getResponseCode();
            if (responseCode != null) {
                responses.addAPIResponse(responseCode, mappedResponse);
            }
        }
        operation.addExceptionType(exceptionType);
    }
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) APIResponsesImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)

Example 5 with MediaType

use of org.eclipse.microprofile.openapi.models.media.MediaType in project Payara by payara.

the class ApplicationProcessor method visitFormParam.

@Override
public void visitFormParam(AnnotationModel param, AnnotatedElement element, ApiContext context) {
    // Find the aggregate schema type of all the parameters
    SchemaType formSchemaType = null;
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        List<org.glassfish.hk2.classmodel.reflect.Parameter> parameters = ((org.glassfish.hk2.classmodel.reflect.Parameter) element).getMethod().getParameters();
        for (org.glassfish.hk2.classmodel.reflect.Parameter methodParam : parameters) {
            if (methodParam.getAnnotation(FormParam.class.getName()) != null) {
                formSchemaType = ModelUtils.getParentSchemaType(formSchemaType, ModelUtils.getSchemaType(methodParam, context));
            }
        }
    }
    final Operation workingOperation = context.getWorkingOperation();
    if (workingOperation != null) {
        // If there's no request body, fill out a new one right down to the schema
        if (workingOperation.getRequestBody() == null) {
            workingOperation.setRequestBody(new RequestBodyImpl().content(new ContentImpl().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, new MediaTypeImpl().schema(new SchemaImpl()))));
        }
        for (MediaType mediaType : workingOperation.getRequestBody().getContent().getMediaTypes().values()) {
            final Schema schema = mediaType.getSchema();
            if (schema != null) {
                schema.setType(formSchemaType);
            }
        }
    }
}
Also used : Schema(org.eclipse.microprofile.openapi.models.media.Schema) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) Operation(org.eclipse.microprofile.openapi.models.Operation) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType)

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