Skip to content

(2.7) Upgrade type detection of Option to use ReferenceType #248

@cowtowncoder

Description

@cowtowncoder

One improvement added in 2.7 (or partially in 2.6) regarding referential types is that type system now has special type, ReferenceType, which allows core databind to do some of handling that is specific to referential types. This can both simplify handling by modules (JDK8 and Guava modules have their own Optionals), and to let databind do some more advanced handling for things like @JsonValue.

Looking at existing handling, looks like Option is handled as a CollectionLikeType. This made some sense (since it is a container of 0 or 1 value(s)) earlier (to retain type parameter), but can now be improved. Looking at Scala code I have some idea how it might work, but just in case, here's how Guava module handles it:

public class GuavaTypeModifier extends TypeModifier
{
    @Override
    public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, TypeFactory typeFactory)
    {
        if (type.isReferenceType() || type.isContainerType()) {
            return type;
        }
        final Class<?> raw = type.getRawClass();
        if (raw == Optional.class) {
            return ReferenceType.upgradeFrom(type, type.containedTypeOrUnknown(0));
        }
        return type;
    }
}

and as far as I can see, it'd just need to change Optional.class to Scala equivalent Option and should work fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions