Skip to content

NumberFormatException caused by property paths from JSR-303 based validation with no index into a collection [SPR-8634] #13276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Aug 24, 2011 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Aug 24, 2011

Andy Wilkinson opened SPR-8634 and commented

When validation of a type in a Collection that can't be indexed, e.g. a Set, fails, Hibernate Validator produces a property path with no index. For example:

public final class Input {

@NotEmpty
@Valid
private final Set<URI> nodes;

...

}

If validation of a URI in nodes fails, the property path in the resulting constraint violation is nodes[]. This causes the logic that processes the constraint violations to blow up with a NumberFormatException:

java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:794)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:717)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
at org.springframework.validation.AbstractBindingResult.rejectValue(AbstractBindingResult.java:105)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:92)
at org.springframework.validation.DataBinder.validate(DataBinder.java:711)
at org.springframework.web.servlet.mvc.method.annotation.support.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:71)
...

Judging by https://forum.hibernate.org/viewtopic.php?f=9&t=1010626 it would appear that the lack of index in the path is expected behaviour for Hibernate Validator. We can probably work around this by using a custom List implementation that also maintains Set semantics, but it'd be nice if Spring coped with the absence of the index more gracefully than it does at the moment.


Affects: 3.0.6, 3.1 M2

Issue Links:

Referenced from: commits 1e2f491

@spring-projects-issues
Copy link
Collaborator Author

Andy Wilkinson commented

Sorry, I forgot to set which version this affects. We're using a recent 3.1.0 snapshot.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Resolved through an alternative code path in SpringValidatorPath if we operate against a BindingResult (which we normally do): We take the JSR-303 provided invalid value then and do not even try to extract the field value ourselves, in contrast to what happens with a standard Errors.rejectValue call in Spring.

Note that the end result will differ in the list of field errors: There will be an entry with a "nodes[]." path in there, not allowing for proper association in web forms or the like. If the latter is necessary, using a different JSR-303 provider is the only option, I'm afraid.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants