Search in sources :

Example 1 with MethodModel

use of org.glassfish.hk2.classmodel.reflect.MethodModel 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 MethodModel

use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.

the class ApplicationProcessor method visitExternalDocumentation.

@Override
public void visitExternalDocumentation(AnnotationModel externalDocs, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel) {
        ExternalDocumentation newExternalDocs = new ExternalDocumentationImpl();
        ExternalDocumentationImpl.merge(ExternalDocumentationImpl.createInstance(externalDocs), newExternalDocs, true);
        if (newExternalDocs.getUrl() != null && !newExternalDocs.getUrl().isEmpty()) {
            context.getWorkingOperation().setExternalDocs(newExternalDocs);
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) ExternalDocumentationImpl(fish.payara.microprofile.openapi.impl.model.ExternalDocumentationImpl) ExternalDocumentation(org.eclipse.microprofile.openapi.models.ExternalDocumentation)

Example 3 with MethodModel

use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.

the class ApplicationProcessor method visitTag.

@Override
public void visitTag(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    Tag from = TagImpl.createInstance(annotation, context);
    if (element instanceof MethodModel) {
        final List<Tag> tags = new ArrayList<>();
        tags.addAll(context.getApi().getTags());
        TagImpl.merge(from, context.getWorkingOperation(), true, tags);
        context.getApi().setTags(tags);
    } else {
        Tag newTag = new TagImpl();
        TagImpl.merge(from, newTag, true);
        if (newTag.getName() != null && !newTag.getName().isEmpty()) {
            context.getApi().addTag(newTag);
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) TagImpl(fish.payara.microprofile.openapi.impl.model.tags.TagImpl) ArrayList(java.util.ArrayList) Tag(org.eclipse.microprofile.openapi.models.tags.Tag)

Example 4 with MethodModel

use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.

the class ApplicationProcessor method visitTags.

@Override
public void visitTags(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel) {
        List<AnnotationModel> tags = annotation.getValue("value", List.class);
        if (tags != null) {
            for (AnnotationModel tag : tags) {
                visitTag(tag, element, context);
            }
        }
        List<String> refs = annotation.getValue("refs", List.class);
        if (refs != null) {
            for (String ref : refs) {
                if (ref != null && !ref.isEmpty()) {
                    context.getWorkingOperation().addTag(ref);
                }
            }
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel)

Example 5 with MethodModel

use of org.glassfish.hk2.classmodel.reflect.MethodModel in project Payara by payara.

the class ModelUtils method getSchemaName.

@SuppressWarnings("unchecked")
public static String getSchemaName(ApiContext context, AnnotatedElement type) {
    assert type != null;
    // context and annotation can be null
    final Class<? extends Annotation>[] ANNOTATION_TYPES = new Class[] { org.eclipse.microprofile.openapi.annotations.media.Schema.class, javax.xml.bind.annotation.XmlRootElement.class, javax.xml.bind.annotation.XmlElement.class };
    for (Class<? extends Annotation> annotationType : ANNOTATION_TYPES) {
        AnnotationModel annotationModel;
        // Fetch the element annotations
        if (context != null && type instanceof ExtensibleType) {
            // Fetch the annotation from the cache
            ExtensibleType<?> implementationType = (ExtensibleType<?>) type;
            AnnotationInfo annotationInfo = context.getAnnotationInfo(implementationType);
            annotationModel = annotationInfo.getAnnotation(annotationType);
        } else {
            // Fetch the annotation manually
            annotationModel = type.getAnnotation(annotationType.getName());
        }
        // Fields can be named by their accessors
        if (annotationModel == null) {
            if (type instanceof FieldModel) {
                final FieldModel field = (FieldModel) type;
                final String accessorName = getAccessorName(field.getName());
                for (MethodModel method : field.getDeclaringType().getMethods()) {
                    // Check if it's the accessor
                    if (accessorName.equals(method.getName())) {
                        annotationModel = type.getAnnotation(annotationType.getName());
                        break;
                    }
                }
            }
        }
        // Get the schema name if the annotation exists
        if (annotationModel != null) {
            final String name = annotationModel.getValue("name", String.class);
            if (name != null && !name.isEmpty() && !name.equals("##default")) {
                return name;
            }
        }
    }
    return getSimpleName(type.getName());
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Annotation(java.lang.annotation.Annotation) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) AnnotationInfo(fish.payara.microprofile.openapi.impl.visitor.AnnotationInfo)

Aggregations

MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)18 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)7 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)7 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)7 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)5 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)5 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)4 ExternalDocumentationImpl (fish.payara.microprofile.openapi.impl.model.ExternalDocumentationImpl)3 OperationImpl (fish.payara.microprofile.openapi.impl.model.OperationImpl)3 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)3 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)3 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)3 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)3 SecurityRequirementImpl (fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl)3 ServerImpl (fish.payara.microprofile.openapi.impl.model.servers.ServerImpl)3 TagImpl (fish.payara.microprofile.openapi.impl.model.tags.TagImpl)3 Method (java.lang.reflect.Method)3 Collection (java.util.Collection)3 Components (org.eclipse.microprofile.openapi.models.Components)3 Callback (org.eclipse.microprofile.openapi.models.callbacks.Callback)3