Search in sources :

Example 1 with ArrayType

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

the class OpenApiDataObjectScanner method processType.

private Type processType(Type fieldType, TypeResolver typeResolver, Schema schema, PathEntry pathEntry) {
    // If it's a terminal type.
    if (isTerminalType(fieldType)) {
        return fieldType;
    }
    if (fieldType.kind() == Type.Kind.WILDCARD_TYPE) {
        fieldType = TypeUtil.resolveWildcard(fieldType.asWildcardType());
    }
    if (fieldType.kind() == Type.Kind.ARRAY) {
        LOG.debugv("Processing an array {0}", fieldType);
        ArrayType arrayType = fieldType.asArrayType();
        // TODO handle multi-dimensional arrays.
        // Array-type schema
        SchemaImpl arrSchema = new SchemaImpl();
        schema.type(Schema.SchemaType.ARRAY);
        schema.items(arrSchema);
        // Only use component (excludes the special name formatting for arrays).
        TypeUtil.TypeWithFormat typeFormat = TypeUtil.getTypeFormat(arrayType.component());
        arrSchema.setType(typeFormat.getSchemaType());
        arrSchema.setFormat(typeFormat.getFormat().format());
        // If it's not a terminal type, then push for later inspection.
        if (!isTerminalType(arrayType.component()) && indexContains(fieldType)) {
            ClassInfo klazz = getClassByName(fieldType);
            pushPathPair(pathEntry, fieldType, klazz, arrSchema);
        }
        return arrayType;
    }
    if (isA(fieldType, ENUM_TYPE) && indexContains(fieldType)) {
        LOG.debugv("Processing an enum {0}", fieldType);
        ClassInfo enumKlazz = getClassByName(fieldType);
        for (FieldInfo enumField : enumKlazz.fields()) {
            // Ignore the hidden enum array as it's not accessible. Add fields that look like enums (of type enumKlazz)
            if (!enumField.name().endsWith("$VALUES") && TypeUtil.getName(enumField.type()).equals(enumKlazz.name())) {
                // Enum's value fields.
                schema.addEnumeration(enumField.name());
            }
        }
        return STRING_TYPE;
    }
    if (fieldType.kind() == Type.Kind.PARAMETERIZED_TYPE) {
        // Parameterised type (e.g. Foo<A, B>)
        return readParamType(pathEntry, schema, fieldType.asParameterizedType(), typeResolver);
    }
    if (fieldType.kind() == Type.Kind.TYPE_VARIABLE || fieldType.kind() == Type.Kind.UNRESOLVED_TYPE_VARIABLE) {
        // Resolve type variable to real variable.
        return resolveTypeVariable(typeResolver, schema, pathEntry, fieldType);
    }
    // Raw Collection
    if (isA(fieldType, COLLECTION_TYPE)) {
        return ARRAY_TYPE_OBJECT;
    }
    // Raw Map
    if (isA(fieldType, MAP_TYPE)) {
        return OBJECT_TYPE;
    }
    // Simple case: bare class or primitive type.
    if (indexContains(fieldType)) {
        pushFieldToPath(pathEntry, fieldType, schema);
    } else {
        // If the type is not in Jandex then we don't have easy access to it.
        // Future work could consider separate code to traverse classes reachable from this classloader.
        LOG.debugv("Encountered type not in Jandex index that is not well-known type. " + "Will not traverse it: {0}", fieldType);
    }
    return fieldType;
}
Also used : ArrayType(org.jboss.jandex.ArrayType) SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) TypeUtil(org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

ArrayType (org.jboss.jandex.ArrayType)1 ClassInfo (org.jboss.jandex.ClassInfo)1 FieldInfo (org.jboss.jandex.FieldInfo)1 SchemaImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl)1 TypeUtil (org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil)1