Search in sources :

Example 1 with ExtensibleType

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

the class ApplicationProcessor method createSchema.

private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type, ExtensibleType clazz, Collection<ParameterizedInterfaceModel> classParameterizedTypes) {
    if (schema == null) {
        schema = new SchemaImpl();
    }
    SchemaType schemaType = ModelUtils.getSchemaType(type, context);
    // If the annotated element is the same type as the reference class, return a null schema
    if (schemaType == SchemaType.OBJECT && type.getType() != null && type.getType().equals(clazz)) {
        schema.setType(null);
        schema.setItems(null);
        return schema;
    }
    if (type.getType() == null) {
        ParameterizedInterfaceModel classParameterizedType = findParameterizedModelFromGenerics(clazz, classParameterizedTypes, type);
        String typeName = null;
        if (type.getTypeName() != null) {
            typeName = type.getTypeName();
        }
        if ((typeName == null || Object.class.getName().equals(typeName)) && classParameterizedType != null) {
            typeName = classParameterizedType.getRawInterfaceName();
        }
        schemaType = ModelUtils.getSchemaType(typeName, context);
        if (schema.getType() == null) {
            schema.setType(schemaType);
        }
        Schema containerSchema = schema;
        if (schemaType == SchemaType.ARRAY) {
            containerSchema = new SchemaImpl();
            schema.setItems(containerSchema);
        }
        if (classParameterizedType != null) {
            Collection<ParameterizedInterfaceModel> genericTypes = classParameterizedType.getParametizedTypes();
            if (genericTypes.isEmpty()) {
                if (insertObjectReference(context, containerSchema, classParameterizedType.getRawInterface(), classParameterizedType.getRawInterfaceName())) {
                    containerSchema.setType(null);
                    containerSchema.setItems(null);
                }
            } else if (classParameterizedType.getRawInterface() instanceof ClassModel) {
                visitSchemaClass(containerSchema, null, (ClassModel) classParameterizedType.getRawInterface(), genericTypes, context);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { classParameterizedType.getRawInterface() });
            }
        } else if (!type.getParameterizedTypes().isEmpty()) {
            List<ParameterizedType> genericTypes = type.getParameterizedTypes();
            if (ModelUtils.isMap(typeName, context) && genericTypes.size() == 2) {
                createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
                containerSchema = new SchemaImpl();
                schema.setAdditionalPropertiesSchema(containerSchema);
                createSchema(containerSchema, context, genericTypes.get(1), clazz, classParameterizedTypes);
            } else {
                createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
            }
        } else {
            return createSchema(containerSchema, context, type);
        }
        return schema;
    }
    return createSchema(schema, context, type);
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) List(java.util.List) ArrayList(java.util.ArrayList) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)

Example 2 with ExtensibleType

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

the class ApplicationProcessor method insertObjectReference.

/**
 * Replace the object in the referee with a reference, and create the
 * reference in the API.
 *
 * @param context the API context.
 * @param referee the object containing the reference.
 * @param referenceClass the class of the object being referenced.
 * @return if the reference has been created.
 */
private boolean insertObjectReference(ApiContext context, Reference<?> referee, AnnotatedElement referenceClass, String referenceClassName) {
    // Firstly check if it's been already defined (i.e. config property definition)
    for (Entry<String, Schema> schemaEntry : context.getApi().getComponents().getSchemas().entrySet()) {
        final Schema entryValue = schemaEntry.getValue();
        if (entryValue instanceof SchemaImpl) {
            final SchemaImpl entryValueImpl = (SchemaImpl) entryValue;
            final String implementationClass = entryValueImpl.getImplementation();
            if (implementationClass != null && implementationClass.equals(referenceClassName)) {
                referee.setRef(schemaEntry.getKey());
                return true;
            }
        }
    }
    // If the object is a java core class
    if (referenceClassName == null || referenceClassName.startsWith("java.")) {
        return false;
    }
    // If the object is a Java EE object type
    if (referenceClassName.startsWith("javax.")) {
        return false;
    }
    // Check the class exists in the application
    if (!context.isApplicationType(referenceClassName)) {
        return false;
    }
    if (referenceClass != null && referenceClass instanceof ExtensibleType) {
        ExtensibleType referenceClassType = (ExtensibleType) referenceClass;
        final AnnotationModel schemaAnnotation = context.getAnnotationInfo(referenceClassType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        String schemaName = ModelUtils.getSchemaName(context, referenceClass);
        // Set the reference name
        referee.setRef(schemaName);
        Schema schema = context.getApi().getComponents().getSchemas().get(schemaName);
        if (schema == null) {
            // Create the schema
            if (context.isAllowedType(referenceClassType)) {
                visitSchema(schemaAnnotation, referenceClassType, context);
            } else if (referenceClassType instanceof ClassModel) {
                apiWalker.processAnnotation((ClassModel) referenceClassType, this);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { referenceClassName });
            }
        }
        return true;
    }
    return false;
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType)

Example 3 with ExtensibleType

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

the class ApplicationProcessor method findParameterizedModelFromGenerics.

private ParameterizedInterfaceModel findParameterizedModelFromGenerics(ExtensibleType<? extends ExtensibleType> annotatedElement, Collection<ParameterizedInterfaceModel> parameterizedModels, ParameterizedType genericType) {
    if (parameterizedModels == null || parameterizedModels.isEmpty()) {
        return null;
    }
    List<String> formalParamKeys = new ArrayList<>(annotatedElement.getFormalTypeParameters().keySet());
    int i = 0;
    for (ParameterizedInterfaceModel parameterizedModel : parameterizedModels) {
        if (formalParamKeys.get(i).equals(genericType.getFormalType())) {
            return parameterizedModel;
        }
        i++;
    }
    return null;
}
Also used : ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) ArrayList(java.util.ArrayList)

Example 4 with ExtensibleType

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

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

Aggregations

AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)4 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)3 Schema (org.eclipse.microprofile.openapi.models.media.Schema)3 ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)3 ExtensibleType (org.glassfish.hk2.classmodel.reflect.ExtensibleType)3 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)3 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)3 Annotation (java.lang.annotation.Annotation)2 ArrayList (java.util.ArrayList)2 ParameterizedInterfaceModel (org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel)2 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 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Components (org.eclipse.microprofile.openapi.models.Components)1 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)1 AnnotatedElement (org.glassfish.hk2.classmodel.reflect.AnnotatedElement)1