Search in sources :

Example 1 with ParameterizedType

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

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

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

the class ApplicationProcessor method createSchema.

private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type) {
    String typeName = type.getTypeName();
    List<ParameterizedType> genericTypes = type.getParameterizedTypes();
    SchemaType schemaType = ModelUtils.getSchemaType(type, context);
    if (schema == null) {
        schema = new SchemaImpl();
        schema.setType(schemaType);
    }
    // Set the subtype if it's an array (for example an array of ints)
    if (schemaType == SchemaType.ARRAY) {
        if (type.isArray()) {
            schemaType = ModelUtils.getSchemaType(type.getTypeName(), context);
            schema.setType(schemaType);
        } else if (!genericTypes.isEmpty()) {
            // should be something Iterable
            schema.setItems(createSchema(context, genericTypes.get(0)));
        }
    }
    // If the schema is an object, insert the reference
    if (schemaType == SchemaType.OBJECT) {
        if (insertObjectReference(context, schema, type.getType(), typeName)) {
            schema.setType(null);
            schema.setItems(null);
        }
    }
    if (type instanceof AnnotatedElement) {
        AnnotatedElement element = (AnnotatedElement) type;
        final AnnotationModel schemaAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class.getName());
        if (schemaAnnotation != null) {
            SchemaImpl.merge(SchemaImpl.createInstance(schemaAnnotation, context), schema, false, context);
        }
    }
    return schema;
}
Also used : ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotatedElement(org.glassfish.hk2.classmodel.reflect.AnnotatedElement) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)

Example 4 with ParameterizedType

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

Aggregations

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