Search in sources :

Example 1 with ParameterizedInterfaceModel

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

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

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

the class ApplicationProcessor method visitSchemaClass.

private Schema visitSchemaClass(Schema schema, AnnotationModel schemaAnnotation, ClassModel clazz, Collection<ParameterizedInterfaceModel> parameterizedInterfaces, ApiContext context) {
    // Get the schema object name
    String schemaName = ModelUtils.getSchemaName(context, clazz);
    // Add a new schema
    if (schema == null) {
        final Components components = context.getApi().getComponents();
        schema = components.getSchemas().getOrDefault(schemaName, new SchemaImpl());
        components.addSchema(schemaName, schema);
    }
    // If there is an annotation, parse its configuration
    if (schemaAnnotation != null) {
        SchemaImpl.merge(SchemaImpl.createInstance(schemaAnnotation, context), schema, false, context);
    }
    for (FieldModel field : clazz.getFields()) {
        final String fieldName = field.getName();
        Boolean hidden = false;
        AnnotationModel fieldSchemaAnnotation = field.getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class.getName());
        if (fieldSchemaAnnotation != null) {
            hidden = fieldSchemaAnnotation.getValue("hidden", Boolean.class);
        }
        if (!Boolean.TRUE.equals(hidden) && !field.isTransient() && !fieldName.startsWith("this$")) {
            final Schema existingProperty = schema.getProperties().get(fieldName);
            final Schema newProperty = createSchema(null, context, field, clazz, parameterizedInterfaces);
            if (existingProperty != null) {
                SchemaImpl.merge(existingProperty, newProperty, true, context);
            }
            schema.addProperty(fieldName, newProperty);
        }
    }
    if (schema.getType() == null) {
        schema.setType(ModelUtils.getSchemaType(clazz.getName(), context));
    }
    // If there is an extending class, add the data
    final ClassModel superClass = clazz.getParent();
    if (superClass != null && !superClass.getName().startsWith("java.")) {
        // Get the parent annotation
        AnnotationModel parentSchemAnnotation = context.getAnnotationInfo(superClass).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        ParameterizedInterfaceModel parameterizedInterface = clazz.getParameterizedInterface(superClass);
        if (parameterizedInterface == null) {
            // Create a schema for the parent
            final Schema parentSchema = visitSchemaClass(null, parentSchemAnnotation, superClass, Collections.emptyList(), context);
            // Get the superclass schema name
            String parentSchemaName = ModelUtils.getSchemaName(context, superClass);
            // Link the schemas
            schema.addAllOf(new SchemaImpl().ref(parentSchemaName));
            // Add all the parent schema properties
            for (Entry<String, Schema> property : parentSchema.getProperties().entrySet()) {
                schema.addProperty(property.getKey(), property.getValue());
            }
        } else {
            visitSchemaClass(schema, parentSchemAnnotation, superClass, parameterizedInterface.getParametizedTypes(), context);
        }
    }
    return schema;
}
Also used : Schema(org.eclipse.microprofile.openapi.models.media.Schema) Components(org.eclipse.microprofile.openapi.models.Components) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Aggregations

ParameterizedInterfaceModel (org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel)3 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)2 ArrayList (java.util.ArrayList)2 Schema (org.eclipse.microprofile.openapi.models.media.Schema)2 ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)2 List (java.util.List)1 Components (org.eclipse.microprofile.openapi.models.Components)1 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)1 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)1 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)1