Search in sources :

Example 11 with MethodModel

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

the class ApplicationProcessor method visitSecurityRequirement.

@Override
public void visitSecurityRequirement(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel) {
        String securityRequirementName = annotation.getValue("name", String.class);
        SecurityRequirement securityRequirement = SecurityRequirementImpl.createInstance(annotation, context);
        if (securityRequirementName != null && !securityRequirementName.isEmpty()) {
            SecurityRequirement model = new SecurityRequirementImpl();
            SecurityRequirementImpl.merge(securityRequirement, model);
            context.getWorkingOperation().addSecurityRequirement(model);
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) SecurityRequirementImpl(fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl) SecurityRequirement(org.eclipse.microprofile.openapi.models.security.SecurityRequirement)

Example 12 with MethodModel

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

the class ApplicationProcessor method visitAPIResponse.

@Override
public void visitAPIResponse(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    APIResponseImpl apiResponse = APIResponseImpl.createInstance(annotation, context);
    Operation workingOperation = context.getWorkingOperation();
    // Handle exception mappers
    if (workingOperation == null) {
        if (element instanceof MethodModel && "toResponse".equals(element.getName())) {
            final MethodModel methodModel = (MethodModel) element;
            final String exceptionType = methodModel.getParameter(0).getTypeName();
            mapException(context, exceptionType, apiResponse);
        } else {
            LOGGER.warning("Unrecognised annotation position at: " + element.shortDesc());
        }
        return;
    }
    APIResponsesImpl.merge(apiResponse, workingOperation.getResponses(), true, context);
    // If an APIResponse has been processed that isn't the default
    String responseCode = apiResponse.getResponseCode();
    if (responseCode != null && !responseCode.isEmpty() && !responseCode.equals(APIResponses.DEFAULT)) {
        // If the element doesn't also contain a response mapping to the default
        AnnotationModel apiResponsesParent = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.responses.APIResponses.class.getName());
        if (apiResponsesParent != null) {
            List<AnnotationModel> apiResponses = apiResponsesParent.getValue("value", List.class);
            if (apiResponses.stream().map(a -> a.getValue("responseCode", String.class)).noneMatch(code -> code == null || code.isEmpty() || code.equals(APIResponses.DEFAULT))) {
                // Then remove the default response
                workingOperation.getResponses().removeAPIResponse(APIResponses.DEFAULT);
            }
        } else {
            workingOperation.getResponses().removeAPIResponse(APIResponses.DEFAULT);
        }
    }
}
Also used : RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) Components(org.eclipse.microprofile.openapi.models.Components) Arrays(java.util.Arrays) ExtensibleImpl(fish.payara.microprofile.openapi.impl.model.ExtensibleImpl) ModelUtils.isVoid(fish.payara.microprofile.openapi.impl.model.util.ModelUtils.isVoid) PathItem(org.eclipse.microprofile.openapi.models.PathItem) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Tag(org.eclipse.microprofile.openapi.models.tags.Tag) ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) DefaultValue(javax.ws.rs.DefaultValue) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) Method(java.lang.reflect.Method) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) Collection(java.util.Collection) AnnotatedElement(org.glassfish.hk2.classmodel.reflect.AnnotatedElement) Set(java.util.Set) WARNING(java.util.logging.Level.WARNING) OpenAPIImpl(fish.payara.microprofile.openapi.impl.model.OpenAPIImpl) ApiVisitor(fish.payara.microprofile.openapi.api.visitor.ApiVisitor) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) List(java.util.List) ServerImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerImpl) TagImpl(fish.payara.microprofile.openapi.impl.model.tags.TagImpl) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) ExternalDocumentation(org.eclipse.microprofile.openapi.models.ExternalDocumentation) Entry(java.util.Map.Entry) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Server(org.eclipse.microprofile.openapi.models.servers.Server) OASProcessor(fish.payara.microprofile.openapi.api.processor.OASProcessor) OpenApiConfiguration(fish.payara.microprofile.openapi.impl.config.OpenApiConfiguration) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) SecurityRequirement(org.eclipse.microprofile.openapi.models.security.SecurityRequirement) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) ModelUtils(fish.payara.microprofile.openapi.impl.model.util.ModelUtils) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SEVERE(java.util.logging.Level.SEVERE) SecurityRequirementImpl(fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl) Operation(org.eclipse.microprofile.openapi.models.Operation) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) OperationImpl(fish.payara.microprofile.openapi.impl.model.OperationImpl) Status(javax.ws.rs.core.Response.Status) FINE(java.util.logging.Level.FINE) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) Types(org.glassfish.hk2.classmodel.reflect.Types) FormParam(javax.ws.rs.FormParam) OpenApiWalker(fish.payara.microprofile.openapi.impl.visitor.OpenApiWalker) PathItemImpl(fish.payara.microprofile.openapi.impl.model.PathItemImpl) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl) APIResponsesImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) EnumType(org.glassfish.hk2.classmodel.reflect.EnumType) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) ExternalDocumentationImpl(fish.payara.microprofile.openapi.impl.model.ExternalDocumentationImpl) Reference(org.eclipse.microprofile.openapi.models.Reference) ApiContext(fish.payara.microprofile.openapi.api.visitor.ApiContext) Collections(java.util.Collections) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) In(org.eclipse.microprofile.openapi.models.parameters.Parameter.In) Type(org.glassfish.hk2.classmodel.reflect.Type) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) Operation(org.eclipse.microprofile.openapi.models.Operation)

Example 13 with MethodModel

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

the class ApplicationProcessor method findOperationParameterFor.

private static Parameter findOperationParameterFor(Parameter parameter, MethodModel annotated, ApiContext context) {
    String name = parameter.getName();
    // If the parameter reference is valid
    if (name != null && !name.isEmpty()) {
        // Get all parameters with the same name
        List<org.glassfish.hk2.classmodel.reflect.Parameter> matchingMethodParameters = annotated.getParameters().stream().filter(x -> name.equals(ModelUtils.getParameterName(context, x))).collect(Collectors.toList());
        // If there is more than one match, filter it further
        In in = parameter.getIn();
        if (matchingMethodParameters.size() > 1 && in != null) {
            // Remove all parameters of the wrong input type
            matchingMethodParameters.removeIf(x -> ModelUtils.getParameterType(context, x) != In.valueOf(in.name()));
        }
        if (matchingMethodParameters.isEmpty()) {
            return null;
        }
        // If there's only one matching parameter, handle it immediately
        String matchingMethodParamName = ModelUtils.getParameterName(context, matchingMethodParameters.get(0));
        // Find the matching operation parameter
        for (Parameter operationParam : context.getWorkingOperation().getParameters()) {
            if (operationParam.getName().equals(matchingMethodParamName)) {
                return operationParam;
            }
        }
    }
    return null;
}
Also used : RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) Components(org.eclipse.microprofile.openapi.models.Components) Arrays(java.util.Arrays) ExtensibleImpl(fish.payara.microprofile.openapi.impl.model.ExtensibleImpl) ModelUtils.isVoid(fish.payara.microprofile.openapi.impl.model.util.ModelUtils.isVoid) PathItem(org.eclipse.microprofile.openapi.models.PathItem) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Tag(org.eclipse.microprofile.openapi.models.tags.Tag) ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) DefaultValue(javax.ws.rs.DefaultValue) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) Method(java.lang.reflect.Method) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) Collection(java.util.Collection) AnnotatedElement(org.glassfish.hk2.classmodel.reflect.AnnotatedElement) Set(java.util.Set) WARNING(java.util.logging.Level.WARNING) OpenAPIImpl(fish.payara.microprofile.openapi.impl.model.OpenAPIImpl) ApiVisitor(fish.payara.microprofile.openapi.api.visitor.ApiVisitor) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) List(java.util.List) ServerImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerImpl) TagImpl(fish.payara.microprofile.openapi.impl.model.tags.TagImpl) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) ExternalDocumentation(org.eclipse.microprofile.openapi.models.ExternalDocumentation) Entry(java.util.Map.Entry) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Server(org.eclipse.microprofile.openapi.models.servers.Server) OASProcessor(fish.payara.microprofile.openapi.api.processor.OASProcessor) OpenApiConfiguration(fish.payara.microprofile.openapi.impl.config.OpenApiConfiguration) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) SecurityRequirement(org.eclipse.microprofile.openapi.models.security.SecurityRequirement) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) ModelUtils(fish.payara.microprofile.openapi.impl.model.util.ModelUtils) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SEVERE(java.util.logging.Level.SEVERE) SecurityRequirementImpl(fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl) Operation(org.eclipse.microprofile.openapi.models.Operation) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) OperationImpl(fish.payara.microprofile.openapi.impl.model.OperationImpl) Status(javax.ws.rs.core.Response.Status) FINE(java.util.logging.Level.FINE) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) Types(org.glassfish.hk2.classmodel.reflect.Types) FormParam(javax.ws.rs.FormParam) OpenApiWalker(fish.payara.microprofile.openapi.impl.visitor.OpenApiWalker) PathItemImpl(fish.payara.microprofile.openapi.impl.model.PathItemImpl) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl) APIResponsesImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) EnumType(org.glassfish.hk2.classmodel.reflect.EnumType) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) ExternalDocumentationImpl(fish.payara.microprofile.openapi.impl.model.ExternalDocumentationImpl) Reference(org.eclipse.microprofile.openapi.models.Reference) ApiContext(fish.payara.microprofile.openapi.api.visitor.ApiContext) Collections(java.util.Collections) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) In(org.eclipse.microprofile.openapi.models.parameters.Parameter.In) Type(org.glassfish.hk2.classmodel.reflect.Type) In(org.eclipse.microprofile.openapi.models.parameters.Parameter.In) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter)

Example 14 with MethodModel

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

the class ApplicationProcessor method visitCallback.

@Override
public void visitCallback(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel) {
        String name = annotation.getValue("name", String.class);
        Callback callbackModel = context.getWorkingOperation().getCallbacks().getOrDefault(name, new CallbackImpl());
        context.getWorkingOperation().addCallback(name, callbackModel);
        CallbackImpl.merge(CallbackImpl.createInstance(annotation, context), callbackModel, true, context);
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)

Example 15 with MethodModel

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

the class ApplicationProcessor method insertDefaultRequestBody.

// PRIVATE METHODS
private RequestBody insertDefaultRequestBody(ApiContext context, Operation operation, MethodModel method) {
    RequestBody requestBody = new RequestBodyImpl();
    // Get the request body type of the method
    org.glassfish.hk2.classmodel.reflect.ParameterizedType bodyType = null;
    for (org.glassfish.hk2.classmodel.reflect.Parameter methodParam : method.getParameters()) {
        if (ModelUtils.isRequestBody(context, methodParam)) {
            bodyType = methodParam;
            break;
        }
    }
    if (bodyType == null) {
        return null;
    }
    // Create the default request body with a wildcard mediatype
    MediaType mediaType = new MediaTypeImpl().schema(createSchema(context, bodyType));
    requestBody.getContent().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, mediaType);
    operation.setRequestBody(requestBody);
    return requestBody;
}
Also used : MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody)

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