Search in sources :

Example 1 with APIResponse

use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project wildfly-swarm by wildfly-swarm.

the class OpenApiParser method readResponses.

/**
 * Reads the {@link APIResponse} OpenAPI nodes.
 * @param node
 */
private Map<String, APIResponse> readResponses(JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    Map<String, APIResponse> models = new LinkedHashMap<>();
    for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
        String fieldName = fieldNames.next();
        JsonNode childNode = node.get(fieldName);
        models.put(fieldName, readAPIResponse(childNode));
    }
    return models;
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with APIResponse

use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project wildfly-swarm by wildfly-swarm.

the class FilterUtil method filterAPIResponses.

/**
 * Filters the given models.
 * @param filter
 * @param models
 */
private static void filterAPIResponses(OASFilter filter, Map<String, APIResponse> models) {
    if (models == null) {
        return;
    }
    Collection<String> keys = new ArrayList<>(models.keySet());
    for (String key : keys) {
        APIResponse model = models.get(key);
        filterAPIResponse(filter, model);
        if (filter.filterAPIResponse(model) == null) {
            models.remove(key);
        }
    }
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) ArrayList(java.util.ArrayList)

Example 3 with APIResponse

use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project Payara by payara.

the class ApplicationProcessor method visitAPIResponseSchema.

@Override
public void visitAPIResponseSchema(AnnotationModel apiResponseSchema, AnnotatedElement element, ApiContext context) {
    final APIResponseImpl response = APIResponseImpl.createInstance(apiResponseSchema, context);
    final OperationImpl operation = (OperationImpl) context.getWorkingOperation();
    // Handle exception mappers
    if (operation == null) {
        if (element instanceof MethodModel && "toResponse".equals(element.getName())) {
            final MethodModel methodModel = (MethodModel) element;
            final String exceptionType = methodModel.getParameter(0).getTypeName();
            mapException(context, exceptionType, response);
        } else {
            LOGGER.warning("Unrecognised annotation position at: " + element.shortDesc());
        }
        return;
    }
    // If response code hasn't been specified
    String responseCode = response.getResponseCode();
    if (responseCode == null || responseCode.isEmpty()) {
        assert element instanceof MethodModel;
        final MethodModel method = (MethodModel) element;
        if (isVoid(method.getReturnType())) {
            if (HttpMethod.POST.equals(operation.getMethod())) {
                responseCode = "201";
            } else if (Arrays.asList(method.getArgumentTypes()).contains("javax.ws.rs.container.AsyncResponse")) {
                responseCode = "200";
            } else {
                responseCode = "204";
            }
        } else {
            responseCode = "200";
        }
    }
    response.setResponseCode(responseCode);
    // If the response description hasn't been specified
    final String responseDescription = response.getDescription();
    if (responseDescription == null || responseDescription.isEmpty()) {
        try {
            final int statusInt = Integer.parseInt(responseCode);
            final Status status = Status.fromStatusCode(statusInt);
            if (status != null) {
                response.setDescription(status.getReasonPhrase());
            }
        } catch (NumberFormatException ex) {
            LOGGER.log(Level.FINE, "Unrecognised status code, description will be empty", ex);
        }
    }
    final APIResponses responses = operation.getResponses();
    // Remove the default response
    final APIResponse defaultResponse = responses.getAPIResponse(APIResponses.DEFAULT);
    if (defaultResponse != null) {
        responses.removeAPIResponse(APIResponses.DEFAULT);
        responses.addAPIResponse(responseCode, defaultResponse);
    }
    // Add the generated response
    APIResponsesImpl.merge(response, responses, true, context);
}
Also used : Status(javax.ws.rs.core.Response.Status) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) OperationImpl(fish.payara.microprofile.openapi.impl.model.OperationImpl) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)

Example 4 with APIResponse

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

use of org.eclipse.microprofile.openapi.models.responses.APIResponse in project Payara by payara.

the class ApplicationProcessor method visitProduces.

@Override
public void visitProduces(AnnotationModel produces, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel && context.getWorkingOperation() != null) {
        for (APIResponse response : context.getWorkingOperation().getResponses().getAPIResponses().values()) {
            if (response != null) {
                // Find the wildcard return type
                if (response.getContent() != null && response.getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD) != null) {
                    MediaType wildcardMedia = response.getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD);
                    // Merge the wildcard return type with the valid response types
                    // This keeps the specific details of a reponse type that has a schema
                    List<String> mediaTypes = produces.getValue("value", List.class);
                    for (String mediaType : mediaTypes) {
                        MediaType held = response.getContent().getMediaType(getContentType(mediaType));
                        if (held == null) {
                            response.getContent().addMediaType(getContentType(mediaType), wildcardMedia);
                        } else {
                            MediaTypeImpl.merge(held, wildcardMedia, true);
                        }
                    }
                    // If there is an @Produces, remove the wildcard
                    response.getContent().removeMediaType(javax.ws.rs.core.MediaType.WILDCARD);
                }
            }
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType)

Aggregations

APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)14 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)6 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)5 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)5 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)5 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)4 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)4 Callback (org.eclipse.microprofile.openapi.models.callbacks.Callback)4 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)4 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)3 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)3 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)3 APIResponsesImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl)3 SecuritySchemeImpl (fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl)3 LinkedHashMap (java.util.LinkedHashMap)3 Operation (org.eclipse.microprofile.openapi.models.Operation)3 PathItem (org.eclipse.microprofile.openapi.models.PathItem)3 SecurityRequirement (org.eclipse.microprofile.openapi.models.security.SecurityRequirement)3 SecurityScheme (org.eclipse.microprofile.openapi.models.security.SecurityScheme)3