Search in sources :

Example 16 with AnnotatedElement

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

the class MapInjectionResolver method getParamField.

/**
 * Get the value of the field.  This value is defined in the
 * annotated Param declaration.  For example:
 * <code>
 * @Param(optional=true)
 * String name="server"
 * </code>
 * The Field, name's value, "server" is returned.
 *
 * @param component command class object
 * @param annotated annotated element
 * @return the annotated Field value
 */
// package-private, for testing
static Object getParamField(final Object component, final AnnotatedElement annotated) {
    try {
        if (annotated instanceof Field) {
            final Field field = (Field) annotated;
            AccessController.doPrivileged(new PrivilegedAction<Object>() {

                @Override
                public Object run() {
                    field.setAccessible(true);
                    return null;
                }
            });
            return ((Field) annotated).get(component);
        }
    } catch (Exception e) {
        // return null instead.
        return null;
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) MultiException(org.glassfish.hk2.api.MultiException)

Example 17 with AnnotatedElement

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

the class ApplicationProcessor method visitConsumes.

@Override
public void visitConsumes(AnnotationModel consumes, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel && context.getWorkingOperation() != null) {
        RequestBody requestBody = context.getWorkingOperation().getRequestBody();
        if (requestBody != null) {
            // Find the wildcard return type
            if (requestBody.getContent() != null && requestBody.getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD) != null) {
                MediaType wildcardMedia = requestBody.getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD);
                // Copy the wildcard return type to the valid request body types
                List<String> mediaTypes = consumes.getValue("value", List.class);
                for (String mediaType : mediaTypes) {
                    requestBody.getContent().addMediaType(getContentType(mediaType), wildcardMedia);
                }
                // If there is an @Consumes, remove the wildcard
                requestBody.getContent().removeMediaType(javax.ws.rs.core.MediaType.WILDCARD);
            }
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody)

Example 18 with AnnotatedElement

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

the class ApplicationProcessor method visitSchemaFieldOrMethod.

private void visitSchemaFieldOrMethod(AnnotationModel schemaAnnotation, AnnotatedElement fieldOrMethod, ExtensibleType<?> declaringType, String typeName, ApiContext context) {
    assert (fieldOrMethod instanceof FieldModel) || (fieldOrMethod instanceof MethodModel);
    Boolean hidden = schemaAnnotation.getValue("hidden", Boolean.class);
    if (hidden == null || !hidden) {
        // Get the schema object name
        String schemaName = ModelUtils.getSchemaName(context, fieldOrMethod);
        SchemaImpl schema = SchemaImpl.createInstance(schemaAnnotation, context);
        // Get the parent schema object name
        String parentName = null;
        AnnotationModel classSchemaAnnotation = context.getAnnotationInfo(declaringType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        if (classSchemaAnnotation != null) {
            parentName = classSchemaAnnotation.getValue("name", String.class);
        }
        if (parentName == null || parentName.isEmpty()) {
            parentName = declaringType.getSimpleName();
        }
        // Get or create the parent schema object
        final Components components = context.getApi().getComponents();
        Schema parentSchema = components.getSchemas().getOrDefault(parentName, new SchemaImpl());
        components.addSchema(parentName, parentSchema);
        Schema property = parentSchema.getProperties().getOrDefault(schemaName, new SchemaImpl());
        parentSchema.addProperty(schemaName, property);
        if (schema.isRequired()) {
            parentSchema.addRequired(schemaName);
        }
        if (property.getRef() == null) {
            property.setType(ModelUtils.getSchemaType(typeName, context));
        }
        SchemaImpl.merge(schema, property, true, context);
    }
}
Also used : Components(org.eclipse.microprofile.openapi.models.Components) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 19 with AnnotatedElement

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

the class ApplicationProcessor method visitServer.

@Override
public void visitServer(AnnotationModel server, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel) {
        Server newServer = new ServerImpl();
        context.getWorkingOperation().addServer(newServer);
        ServerImpl.merge(ServerImpl.createInstance(server, context), newServer, true);
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) Server(org.eclipse.microprofile.openapi.models.servers.Server) ServerImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerImpl)

Example 20 with AnnotatedElement

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

Aggregations

MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)14 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)8 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)8 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)5 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)4 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)4 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)3 ExternalDocumentationImpl (fish.payara.microprofile.openapi.impl.model.ExternalDocumentationImpl)2 OperationImpl (fish.payara.microprofile.openapi.impl.model.OperationImpl)2 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)2 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)2 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)2 SecurityRequirementImpl (fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl)2 ServerImpl (fish.payara.microprofile.openapi.impl.model.servers.ServerImpl)2 TagImpl (fish.payara.microprofile.openapi.impl.model.tags.TagImpl)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2