Search in sources :

Example 1 with APIResponseImpl

use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl 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 2 with APIResponseImpl

use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl 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 3 with APIResponseImpl

use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl in project Payara by payara.

the class ComponentsImpl method merge.

public static void merge(Components from, Components to, boolean override, ApiContext context) {
    if (from == null) {
        return;
    }
    // Handle @Schema
    if (from.getSchemas() != null) {
        for (Entry<String, Schema> fromEntry : from.getSchemas().entrySet()) {
            final String schemaName = fromEntry.getKey();
            if (schemaName != null) {
                final Schema fromSchema = fromEntry.getValue();
                final Schema toSchema = to.getSchemas().getOrDefault(schemaName, new SchemaImpl());
                SchemaImpl.merge(fromSchema, toSchema, override, context);
                to.addSchema(schemaName, toSchema);
            }
        }
    }
    // Handle @Callback
    if (from.getCallbacks() != null) {
        for (String callbackName : from.getCallbacks().keySet()) {
            if (callbackName != null) {
                Callback newCallback = new CallbackImpl();
                CallbackImpl.merge(from.getCallbacks().get(callbackName), newCallback, override, context);
                to.addCallback(callbackName, newCallback);
            }
        }
    }
    // Handle @ExampleObject
    if (from.getExamples() != null) {
        for (String exampleName : from.getExamples().keySet()) {
            if (exampleName != null) {
                Example newExample = new ExampleImpl();
                ExampleImpl.merge(from.getExamples().get(exampleName), newExample, override);
                to.addExample(exampleName, newExample);
            }
        }
    }
    // Handle @Header
    if (from.getHeaders() != null) {
        for (String headerName : from.getHeaders().keySet()) {
            if (headerName != null) {
                Header newHeader = new HeaderImpl();
                HeaderImpl.merge(from.getHeaders().get(headerName), newHeader, override, context);
                to.addHeader(headerName, newHeader);
            }
        }
    }
    // Handle @Link
    if (from.getLinks() != null) {
        for (String linkName : from.getLinks().keySet()) {
            if (linkName != null) {
                Link newLink = new LinkImpl();
                LinkImpl.merge(from.getLinks().get(linkName), newLink, override);
                to.addLink(linkName, newLink);
            }
        }
    }
    // Handle @Parameter
    if (from.getParameters() != null) {
        for (String parameterName : from.getParameters().keySet()) {
            if (parameterName != null) {
                Parameter newParameter = new ParameterImpl();
                ParameterImpl.merge(from.getParameters().get(parameterName), newParameter, override, context);
                to.addParameter(parameterName, newParameter);
            }
        }
    }
    // Handle @RequestBody
    if (from.getRequestBodies() != null) {
        for (String requestBodyName : from.getRequestBodies().keySet()) {
            if (requestBodyName != null) {
                RequestBody newRequestBody = new RequestBodyImpl();
                RequestBodyImpl.merge(from.getRequestBodies().get(requestBodyName), newRequestBody, override, context);
                to.addRequestBody(requestBodyName, newRequestBody);
            }
        }
    }
    // Handle @APIResponse
    if (from.getResponses() != null) {
        for (String responseName : from.getResponses().keySet()) {
            if (responseName != null) {
                APIResponse newResponse = new APIResponseImpl();
                APIResponseImpl.merge(from.getResponses().get(responseName), newResponse, override, context);
                to.addResponse(responseName, newResponse);
            }
        }
    }
    // Handle @SecurityScheme
    if (from.getSecuritySchemes() != null) {
        for (String securitySchemeName : from.getSecuritySchemes().keySet()) {
            if (securitySchemeName != null) {
                SecurityScheme newSecurity = new SecuritySchemeImpl();
                SecuritySchemeImpl.merge(from.getSecuritySchemes().get(securitySchemeName), newSecurity, override);
                to.addSecurityScheme(securitySchemeName, newSecurity);
            }
        }
    }
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl) HeaderImpl(fish.payara.microprofile.openapi.impl.model.headers.HeaderImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) Header(org.eclipse.microprofile.openapi.models.headers.Header) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) Example(org.eclipse.microprofile.openapi.models.examples.Example) LinkImpl(fish.payara.microprofile.openapi.impl.model.links.LinkImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) Link(org.eclipse.microprofile.openapi.models.links.Link) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody)

Example 4 with APIResponseImpl

use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl in project Payara by payara.

the class OperationImpl method createInstance.

public static Operation createInstance(AnnotationModel annotation, ApiContext context) {
    OperationImpl from = new OperationImpl();
    from.setSummary(annotation.getValue("summary", String.class));
    from.setDescription(annotation.getValue("description", String.class));
    AnnotationModel externalDocs = annotation.getValue("externalDocs", AnnotationModel.class);
    if (externalDocs != null) {
        from.setExternalDocs(ExternalDocumentationImpl.createInstance(externalDocs));
    }
    from.setOperationId(annotation.getValue("operationId", String.class));
    extractAnnotations(annotation, context, "parameters", ParameterImpl::createInstance, from::addParameter);
    AnnotationModel requestBody = annotation.getValue("requestBody", AnnotationModel.class);
    if (requestBody != null) {
        from.setRequestBody(RequestBodyImpl.createInstance(requestBody, context));
    }
    extractAnnotations(annotation, context, "responses", "responseCode", APIResponseImpl::createInstance, from.responses::addAPIResponse);
    extractAnnotations(annotation, context, "callbacks", "name", CallbackImpl::createInstance, from::addCallback);
    from.setDeprecated(annotation.getValue("deprecated", Boolean.class));
    extractAnnotations(annotation, context, "security", SecurityRequirementImpl::createInstance, from::addSecurityRequirement);
    extractAnnotations(annotation, context, "servers", ServerImpl::createInstance, from::addServer);
    from.setMethod(annotation.getValue("method", String.class));
    return from;
}
Also used : CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl) ServerImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerImpl) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) SecurityRequirementImpl(fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)

Example 5 with APIResponseImpl

use of fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl in project Payara by payara.

the class ResponseTest method defaultPostVoidSchemaTest.

@Test
public void defaultPostVoidSchemaTest() {
    APIResponses responses = getDocument().getPaths().getPathItem("/test/response/schema").getPOST().getResponses();
    checkResponseCode(responses, 201);
    final APIResponseImpl response = (APIResponseImpl) responses.getAPIResponse("201");
    checkMediaTypesExist(response, APPLICATION_JSON, APPLICATION_XML);
    checkSchemaDescriptions(response, "Custom Response");
}
Also used : APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) Test(org.junit.Test) OpenApiApplicationTest(fish.payara.microprofile.openapi.test.app.OpenApiApplicationTest)

Aggregations

APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)14 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)9 Test (org.junit.Test)7 OpenApiApplicationTest (fish.payara.microprofile.openapi.test.app.OpenApiApplicationTest)6 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)5 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)5 APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)5 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)4 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)4 SecuritySchemeImpl (fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl)4 ExampleImpl (fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)3 LinkImpl (fish.payara.microprofile.openapi.impl.model.links.LinkImpl)3 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)3 APIResponsesImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl)3 SecurityRequirementImpl (fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl)3 ServerImpl (fish.payara.microprofile.openapi.impl.model.servers.ServerImpl)3 Components (org.eclipse.microprofile.openapi.models.Components)3 Callback (org.eclipse.microprofile.openapi.models.callbacks.Callback)3 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)3 Schema (org.eclipse.microprofile.openapi.models.media.Schema)3