Optional and Type Annotations

Description

While playing around with Java 8 Optionals and validation I've encountered some cases I think the validation does not behave as intended.

It might be that this is related to the somewhat special meaning of an Optional, but the problem relies in such cases when I want to validate the container (the Optional) separate from the actual value. I've tried to use the new type annotations to do so.

So without further ado I redirect you to my branch containing the test cases showing what I think is not intended behavior. I've added a JavaDoc for the failing test cases.

If you want I can add a textual description for the failing test cases in this ticket, too.

Click here to see test cases concerning this ticket.

Environment

None

Activity

Show:
Hardy Ferentschik
March 18, 2015, 3:05 PM

OptionalTypeAnnotationConstraintTest.java

Hardy Ferentschik
March 18, 2015, 3:06 PM

The described NullPointerExceptions are a bug in OptionalValueUnwrapper (see ).

Hardy Ferentschik
March 18, 2015, 3:55 PM

NullPointerExceptions aside there are 3 failing tests left. The problem with them is that currently we handle "unwrapping" not per constraint, but per property. This would need to change to make your use cases work. I guess we worked under the assumption that either all constraints apply to the wrapper or all to the wrapped value. I need to dig deeper into the code to see how we can support your use case. At first glance your tests looks reasonable.

On a tagent, I noticed that the results differ between validate and validateProperty. There must be another bug in the validateProperty code path.

Nice test case btw, I could just drop it into our code base. We will investigate the remaining failing tests asap.

Lukas Niemeier
March 18, 2015, 10:35 PM
Edited

The problem with them is that currently we handle "unwrapping" not per constraint, but per property. This would need to change to make your use cases work. I guess we worked under the assumption that either all constraints apply to the wrapper or all to the wrapped value.

With the possibility to constrain the wrapper and the value you actually don't need the explicit unwrapping (i.e. UnwrapValidatedValue), do you? That was my first assumption when I discovered that you can constraint on the value's type. But - as I'm new to this topic - I don't know if this would break other use cases.

Thank you for looking into it!

Hardy Ferentschik
March 19, 2015, 12:12 PM

 

With the possibility to constrain the wrapper and the value you actually don't need the explicit unwrapping (i.e. UnwrapValidatedValue), do you?

Right, if type annotations would have been always possible, there would never have been a need for UnwrapValidatedValue. UnwrapValidatedValue is basically a workaround for missing type annotations in pre Java 8. Now that we have type annotations in Java 8, we should aim to make these two things work nicely together (avoiding to break the existing functionality).

Also technically, you still need some concept (implicit or explicit) of "unwrapping". If one annotates the value type of an Optional, one still needs a way to access this value. We can provide some default unwrappers (eg for Optional), but that won't work for custom user types.

Assignee

Davide D'Alto

Reporter

Lukas Niemeier

Labels

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

Priority

Minor
Configure