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

Closed
spring-projects-issues opened this issue Aug 23, 2016 · 7 comments
Closed

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

spring-projects-issues opened this issue Aug 23, 2016 · 7 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

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:

1 votes, 3 watchers

@spring-projects-issues
Copy link
Collaborator Author

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

Martin Asenov commented

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

@spring-projects-issues
Copy link
Collaborator Author

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

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

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

@rstoyanchev
Copy link
Contributor

This is now supported with Bean Validation and the built-in Spring web method validation #30645.

@rstoyanchev rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2023
@rstoyanchev rstoyanchev added the status: superseded An issue that has been superseded by another label Dec 21, 2023
@jhoeller jhoeller removed this from the General Backlog milestone Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants