Search in sources :

Example 1 with ParameterizedType

use of org.jboss.jandex.ParameterizedType in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method readParamType.

private Type readParamType(PathEntry pathEntry, Schema schema, ParameterizedType pType, TypeResolver typeResolver) {
    LOG.debugv("Processing parameterized type {0}", pType);
    // If it's a collection, we should treat it as an array.
    if (isA(pType, COLLECTION_TYPE)) {
        // TODO maybe also Iterable?
        LOG.debugv("Processing Java Collection. Will treat as an array.");
        SchemaImpl arraySchema = new SchemaImpl();
        schema.type(Schema.SchemaType.ARRAY);
        schema.items(arraySchema);
        // Should only have one arg for collection.
        Type arg = pType.arguments().get(0);
        if (isTerminalType(arg)) {
            TypeUtil.TypeWithFormat terminalType = TypeUtil.getTypeFormat(arg);
            arraySchema.type(terminalType.getSchemaType());
            arraySchema.format(terminalType.getFormat().format());
        } else if (arg.kind() == Type.Kind.TYPE_VARIABLE || arg.kind() == Type.Kind.UNRESOLVED_TYPE_VARIABLE || arg.kind() == Type.Kind.WILDCARD_TYPE) {
            Type resolved = resolveTypeVariable(typeResolver, arraySchema, pathEntry, arg);
            if (indexContains(resolved)) {
                arraySchema.type(Schema.SchemaType.OBJECT);
            }
        } else if (indexContains(arg)) {
            arraySchema.type(Schema.SchemaType.OBJECT);
            pushFieldToPath(pathEntry, arg, arraySchema);
        }
        // Representing collection as JSON array
        return ARRAY_TYPE_OBJECT;
    } else if (isA(pType, MAP_TYPE)) {
        LOG.debugv("Processing Map. Will treat as an object.");
        schema.type(Schema.SchemaType.OBJECT);
        if (pType.arguments().size() == 2) {
            Type valueType = pType.arguments().get(1);
            SchemaImpl propsSchema = new SchemaImpl();
            if (isTerminalType(valueType)) {
                TypeUtil.TypeWithFormat tf = TypeUtil.getTypeFormat(valueType);
                propsSchema.setType(tf.getSchemaType());
                propsSchema.setFormat(tf.getFormat().format());
            } else if (valueType.kind() == Type.Kind.TYPE_VARIABLE || valueType.kind() == Type.Kind.UNRESOLVED_TYPE_VARIABLE || valueType.kind() == Type.Kind.WILDCARD_TYPE) {
                Type resolved = resolveTypeVariable(typeResolver, propsSchema, pathEntry, valueType);
                if (indexContains(resolved)) {
                    propsSchema.type(Schema.SchemaType.OBJECT);
                }
            } else if (indexContains(valueType)) {
                propsSchema.type(Schema.SchemaType.OBJECT);
                pushFieldToPath(pathEntry, valueType, propsSchema);
            }
            schema.additionalProperties(propsSchema);
        }
        return OBJECT_TYPE;
    } else {
        // This attempts to allow us to resolve the types generically.
        ClassInfo klazz = getClassByName(pType);
        // Build mapping of class's type variables (e.g. A, B) to resolved variables
        // Resolved variables could be any type (e.g. String, another param type, etc)
        PathEntry pair = new PathEntry(pathEntry, klazz, pType, schema);
        path.push(pair);
        return pType;
    }
}
Also used : SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) PrimitiveType(org.jboss.jandex.PrimitiveType) Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) ArrayType(org.jboss.jandex.ArrayType) TypeUtil(org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with ParameterizedType

use of org.jboss.jandex.ParameterizedType in project wildfly-swarm by wildfly-swarm.

the class TypeResolver method buildParamTypeResolutionMap.

private static Map<String, Type> buildParamTypeResolutionMap(ClassInfo klazz, ParameterizedType parameterizedType) {
    List<TypeVariable> typeVariables = klazz.typeParameters();
    List<Type> arguments = parameterizedType.arguments();
    if (arguments.size() != typeVariables.size()) {
        LOG.errorv("Unanticipated mismatch between type arguments and type variables \n" + "Args: {0}\n Vars:{1}", arguments, typeVariables);
    }
    Map<String, Type> resolutionMap = new LinkedHashMap<>();
    for (int i = 0; i < arguments.size(); i++) {
        TypeVariable typeVar = typeVariables.get(i);
        Type arg = arguments.get(i);
        resolutionMap.put(typeVar.identifier(), arg);
    }
    return resolutionMap;
}
Also used : Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) TypeVariable(org.jboss.jandex.TypeVariable) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ParameterizedType (org.jboss.jandex.ParameterizedType)2 Type (org.jboss.jandex.Type)2 LinkedHashMap (java.util.LinkedHashMap)1 ArrayType (org.jboss.jandex.ArrayType)1 ClassInfo (org.jboss.jandex.ClassInfo)1 PrimitiveType (org.jboss.jandex.PrimitiveType)1 TypeVariable (org.jboss.jandex.TypeVariable)1 SchemaImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl)1 TypeUtil (org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil)1