Skip to content
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

Validate values in top-level Map parameters [SPR-14615] #19182

Open
spring-projects-issues opened this issue Aug 23, 2016 · 6 comments
Open

Validate values in top-level Map parameters [SPR-14615] #19182

spring-projects-issues opened this issue Aug 23, 2016 · 6 comments
Labels
in: web type: enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Martin Asenov opened SPR-14615 and commented

Class with RestController annotation
Method with RequestMapping annotation, method is POST
Map<String, CustomDto> is a parameter, annotated with @Valid and @RequestBody
CustomDto has @NotNull on one of its fields.
The input passes even though a CustomDto entry is not valid (null value on the @NotNull field)
When I try to change the Map with a single CustomDto, it is validated correctly, but when a map, it is not.


Affects: 4.2.5

Issue Links:

  • #16519 Bean Validation invocation API for use with individual values and constraints
  • #18007 @Validated support on Iterables (and implementors)
  • #16917 Add support for validating a collection of objects

1 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Juergen Hoeller commented

There is no standard support for validating top-level Map or Collection objects in case of Bean Validation. We may consider introducing custom support for it.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Martin Asenov commented

@Juergen Hoeller ok, but how do I workaround this for now? Any suggestions?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Juergen Hoeller commented

You could inject an @Autowired Validator into your controller class and programmatically call Validator.validate on each of your map values.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Martin Asenov commented

thanks, I'll give it a try, even though it is not comprehensible to me why such basic functionality is not supported.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Juergen Hoeller commented

Agreed, it is unintuitive that this doesn't work. This is partially due to the JavaBean nature of both the Bean Validation spec and Spring's data binding, where collections and maps are only defined to be nested within top-level objects.

Simply speaking, we're just passing the given object to javax.validation.Validator.validate, and that method never finds any constraint validation on collection or map objects. If Hibernate Validator doesn't consider support for this directly, we can only manually iterate the collection values and adapt the constraint violation metadata accordingly, even if the won't be able to strictly adhere to the Bean Validation API contract then (e.g. in terms of property path traversal and invalid value handling). We can consider that for 5.0 where several Bean Validation related enhancements are waiting already.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 23, 2016

Martin Asenov commented

I coulnd't get the suggested approach to work too.

@Autowired
private Validator validator;

@RequestMapping(value = "/", method = RequestMethod.POST)
public void postMethod(@RequestBody Map<String, CustomDto> rules, Errors errors) {
    rules.values().forEach(value -> {validator.validate(value, errors)});
}

stiill doesn't detect the missing @NotNull field. Am I missing something?

Thanks

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

No branches or pull requests

2 participants