[hibernate-dev] Possible bug in hibernate-validator ValidatorImpl

Itai Frenkel Itai at forter.com
Thu Aug 21 06:38:41 EDT 2014


Hello,

TL;DR: Need an opinion if to open a JIRA (since reproduction is very tricky)


I am using the @UnwrapValidatedValue with a custom Guava OptionalUnwrapper and sometimes OptionalUnwrapper#getValidatedValueType() is called also for non-Optional<> fields.


The bug reproduction is sporadic and tricky. I managed to pinpoint it to ValidatorImpl#setValidatedValueHandlerToValueContextIfPresent() that does not perform valueContext.setValidatedValueHandler( null ) when there is no need for an unwrapper. Since valueContext is re-used between fields, it is possible for the OptionalUnwrapper to be first called for Optional fields and then re-used for non-Optional fields with the same validatedValueHandler of the Optional field.


If the code owner could quickly comment on this, it would be great.


below are some code snippets I used.


public class SomePOJO {

    @DecimalMin(value="0", inclusive=true)
    public Integer x = -1;

    @UnwrapValidatedValue
    @DecimalMin(value="1", inclusive=true)
    public Optional<Integer> y = Optional.of(-1);

    @DecimalMin(value="0", inclusive=true)
    public Integer z = -1;
}

public class OptionalUnwrapper extends ValidatedValueUnwrapper<Optional<?>> {

    private final TypeResolver typeResolver;

    public OptionalUnwrapper() {
        typeResolver = new TypeResolver();
    }

    @Override
    public Object handleValidatedValue(Optional<?> optional) {
        return optional == null? null : optional.orNull();
    }

    @Override
    public Type getValidatedValueType(Type valueType) {

        final TypeToken<?> typeToken = TypeToken.of(valueType);
        if (typeToken.getRawType().isAssignableFrom(Optional.class)) {
            Type rawType = typeToken.resolveType(Optional.class.getTypeParameters()[0]).getType();
            return rawType;
        }
        else {
            Type rawType = typeToken.getType();
            Preconditions.checkArgument(false, "type " + rawType + " is not supported by " + this.getClass() );
            return null;
        }
    }
}




More information about the hibernate-dev mailing list