Search in sources :

Example 1 with FieldModel

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

the class ApplicationProcessor method getArraySchema.

private static SchemaImpl getArraySchema(AnnotatedElement element, ApiContext context) {
    SchemaImpl arraySchema = new SchemaImpl();
    List<ParameterizedType> parameterizedType;
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        parameterizedType = parameter.getParameterizedTypes();
    } else {
        FieldModel field = (FieldModel) element;
        parameterizedType = field.getParameterizedTypes();
    }
    arraySchema.setType(ModelUtils.getSchemaType(parameterizedType.get(0).getTypeName(), context));
    return arraySchema;
}
Also used : ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 2 with FieldModel

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

the class ApplicationProcessor method addParameter.

private static void addParameter(AnnotatedElement element, ApiContext context, String name, In in, Boolean required) {
    Boolean hidden = false;
    AnnotationModel paramAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.parameters.Parameter.class.getName());
    if (paramAnnotation != null) {
        hidden = paramAnnotation.getValue("hidden", Boolean.class);
    }
    if (hidden != null && hidden) {
        return;
    }
    Parameter newParameter = new ParameterImpl();
    newParameter.setName(name);
    newParameter.setIn(in);
    newParameter.setRequired(required);
    SchemaImpl schema = new SchemaImpl();
    String defaultValue = getDefaultValueIfPresent(element);
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        schema.setType(ModelUtils.getSchemaType(parameter.getTypeName(), context));
    } else {
        FieldModel field = (FieldModel) element;
        schema.setType(ModelUtils.getSchemaType(field.getTypeName(), context));
    }
    if (schema.getType() == SchemaType.ARRAY) {
        schema.setItems(getArraySchema(element, context));
        if (defaultValue != null) {
            schema.getItems().setDefaultValue(defaultValue);
        }
    } else if (defaultValue != null) {
        schema.setDefaultValue(defaultValue);
    }
    newParameter.setSchema(schema);
    final Operation workingOperation = context.getWorkingOperation();
    if (workingOperation != null) {
        for (Parameter parameter : workingOperation.getParameters()) {
            final String parameterName = parameter.getName();
            if (parameterName != null && parameterName.equals(newParameter.getName())) {
                ParameterImpl.merge(newParameter, parameter, false, context);
                return;
            }
        }
        workingOperation.addParameter(newParameter);
    } else {
        LOGGER.log(SEVERE, "Couldn''t add {0} parameter, \"{1}\" to the OpenAPI Document. This is usually caused by declaring parameter under a method with an unsupported annotation.", new Object[] { newParameter.getIn(), newParameter.getName() });
    }
}
Also used : Operation(org.eclipse.microprofile.openapi.models.Operation) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 3 with FieldModel

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

Example 4 with FieldModel

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

the class AnnotationInfo method init.

private void init(ExtensibleType<? extends ExtensibleType> type) {
    // recurse first so that re-stated annotations "override"
    ExtensibleType<? extends ExtensibleType> supertype = type.getParent();
    if (supertype != null) {
        init(supertype);
    }
    for (InterfaceModel implementedInterface : type.getInterfaces()) {
        if (implementedInterface != null && implementedInterface != type) {
            init(implementedInterface);
        }
    }
    // collect annotations
    putAll(type.getAnnotations(), typeAnnotations);
    if (type instanceof ClassModel) {
        for (FieldModel field : ((ClassModel) type).getFields()) {
            putAll(field.getAnnotations(), fieldAnnotations.computeIfAbsent(field.getName(), key -> new ConcurrentHashMap<>()));
        }
    }
    for (MethodModel method : type.getMethods()) {
        putAll(method.getAnnotations(), methodAnnotations.computeIfAbsent(getSignature(method), key -> new ConcurrentHashMap<>()));
        for (Parameter parameter : method.getParameters()) {
            putAll(parameter.getAnnotations(), methodParameterAnnotations.computeIfAbsent(getIdentifier(parameter), key -> new ConcurrentHashMap<>()));
        }
    }
}
Also used : InterfaceModel(org.glassfish.hk2.classmodel.reflect.InterfaceModel) Collection(java.util.Collection) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AnnotatedElement(org.glassfish.hk2.classmodel.reflect.AnnotatedElement) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) Parameter(org.glassfish.hk2.classmodel.reflect.Parameter) Field(java.lang.reflect.Field) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) Method(java.lang.reflect.Method) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) InterfaceModel(org.glassfish.hk2.classmodel.reflect.InterfaceModel) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) Parameter(org.glassfish.hk2.classmodel.reflect.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with FieldModel

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

the class OpenApiWalker method processAnnotation.

@SuppressWarnings("unchecked")
public final void processAnnotation(ClassModel annotatedClass, ApiVisitor visitor) {
    AnnotationInfo annotations = context.getAnnotationInfo(annotatedClass);
    processAnnotation((E) annotatedClass, annotations, visitor, new OpenApiContext(context, annotatedClass));
    for (final MethodModel method : annotatedClass.getMethods()) {
        processAnnotation((E) method, annotations, visitor, new OpenApiContext(context, method));
    }
    for (final FieldModel field : annotatedClass.getFields()) {
        processAnnotation((E) field, annotations, visitor, new OpenApiContext(context, field));
    }
    for (final MethodModel method : annotatedClass.getMethods()) {
        for (org.glassfish.hk2.classmodel.reflect.Parameter parameter : method.getParameters()) {
            processAnnotation((E) parameter, annotations, visitor, new OpenApiContext(context, method));
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Aggregations

FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)9 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)6 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)5 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)3 Annotation (java.lang.annotation.Annotation)2 Components (org.eclipse.microprofile.openapi.models.Components)2 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)2 ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)2 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)2 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)1 AnnotationInfo (fish.payara.microprofile.openapi.impl.visitor.AnnotationInfo)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CookieParam (javax.ws.rs.CookieParam)1 DELETE (javax.ws.rs.DELETE)1 HeaderParam (javax.ws.rs.HeaderParam)1