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

Itai Frenkel Itai at forter.com
Tue Sep 9 06:03:39 EDT 2014


Opened https://hibernate.atlassian.net/browse/HV-928


thanks :)


________________________________
From: gunnar.morling at googlemail.com <gunnar.morling at googlemail.com> on behalf of Gunnar Morling <gunnar at hibernate.org>
Sent: Thursday, August 21, 2014 2:57 PM
To: Itai Frenkel
Cc: hibernate-dev at lists.jboss.org
Subject: Re: [hibernate-dev] Possible bug in hibernate-validator ValidatorImpl

Hi,

2014-08-21 12:38 GMT+02:00 Itai Frenkel <Itai at forter.com<mailto:Itai at forter.com>>:
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.

If you say sporadic, does it mean validating one and the same POJO sometimes shows the issue and in some other cases it doesn't, without altering any parts of the code?

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.

Interesting, this doesn't seem correct indeed. Without having a closer look I'd say the un-wrapper should be reset on the context between validation of different properties.

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

It sounds like a legitimate bug description, so I suggest you move forward and open an issue in JIRA (https://hibernate.atlassian.net/browse/HV). It would be great if you could attach a complete executable test case which demonstrates the issue.


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;
        }
    }
}

Thanks for reporting this issue, much appreciated!

Cheers,

--Gunnar



_______________________________________________
hibernate-dev mailing list
hibernate-dev at lists.jboss.org<mailto:hibernate-dev at lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/hibernate-dev



More information about the hibernate-dev mailing list