-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
@Validated validation group not working on request body of List #32886
Comments
Hi @bclozel, any update on it? |
A workarroud:
and |
This has been discussed many times in this issue tracker, including in #32807. |
@bclozel the issue is not about where to put @validated or @Valid on, but is about @validated not working at all. Please read my description CAREFULLY. Wherever I put the annotation on, the validation did not work. The only way to make it work is to use my workarround |
I did. I have also tried the following: public class VocabularyRequest {
@NotEmpty
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
} @RestController
@Validated
public class TestController {
@PostMapping(path = "/bulk", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Flux<String> createBatch(
@RequestBody @Valid List<VocabularyRequest> requests) {
return Flux.fromIterable(requests).map(VocabularyRequest::getId);
}
} When sending
Thoughts? |
@bclozel then you did not read my desc carefully.
@validated is useful only when using with validation group. Otherwise why i have to use it while i can just use @Valid? |
Does this comment explain better the current situation? |
@bclozel maybe I didnot describe the issue clearly. Let me re-describe:
This
By "works", I mean validation group works. For ex I send Now, changing the Request body from NORMAL DTO/POJO to a List, the
By "not work", I mean validation group does not work. Does not matter I send |
@anaconda875 this could be more of a bean validation question. In the first case, we should be applying bean validation directly on Could you provide a small sample to experiment with? |
I have created a sample repository for one of the use cases I've tested, I'm not sure if this covers @anaconda875 's app. A controller like the following will not validate the parameters, unless you add a spring-framework/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java Line 193 in 36b0702
@RestController
public class TestController {
@PostMapping(path = "/bulk", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Validated(MyGroup.class)
public Flux<String> createBatch(
@RequestBody List<@Valid VocabularyRequest> requests) {
return Flux.fromIterable(requests).map(VocabularyRequest::getId);
}
} I have also tried the following variant, and this fails as expected with a @RestController
public class TestController {
@PostMapping(path = "/bulk", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Validated(MyGroup.class)
public Flux<String> createBatch(
@RequestBody @Valid List<VocabularyRequest> requests) {
return Flux.fromIterable(requests).map(VocabularyRequest::getId);
}
} |
Hi @bclozel @rstoyanchev I have created a sample project. I also have described very clearly at JAVADOC. |
Thanks for the sample @anaconda875
|
@bclozel the issue is about @validated does not work for Collection. My sample DO show this. |
Thanks for your feedback and patience. Instance validationIn the case of a single @PostMapping(consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Mono<VocabularyResponse> create(
@Validated(VocabularyCreation.class) @RequestBody VocabularyRequest request) {
return Mono.empty();
} In the case of @PostMapping(value = "/bulk", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Flux<VocabularyResponse> createBatch(
@Validated(VocabularyCreation.class) @RequestBody List<VocabularyRequest> requests) {
return Flux.empty();
} Again, a signature like Method validationThis leaves us with method validation, see my previous comment: #32886 (comment) The method validation case behaves differently and validates parameters in a different way as it uses At this point, I don't think we can do anything here: instance validation and method validation behave differently. In both cases, we are extracting the right information (including validation groups), the only difference is that the considered root type in the first case is Maybe this should be an enhancement request for Hibernate validation? I haven't seen any behavior describing this in the Bean Validation spec so far. As a result, I'm tempted to decline this issue as I don't see anything actionable from the Spring side. |
Got it, thank you |
Partially superseded by #32964 for the issue we've found while discussing this ticket. Correcting my previous statements here. Method validation is not required, only a constraint annotation is required on the parameter to work. @RestController
@RequestMapping("/vocabularies")
// No @Validated annotation required
public class VocabularyResource {
@PostMapping(value = "/bulk", consumes = APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
// Select validation group
@Validated(VocabularyCreation.class)
public Flux<VocabularyResponse> createBatch(
@RequestBody @Valid List<VocabularyRequest> requests) {
return Flux.empty();
} So the only issue with the problematic use case is that the parameter does not have any constraint annotation on the parameter declaration (in the controller method signature), nor on the type itself (a With that in mind, I don't think it's a problem in Hibernate Validation at all, it's behaving consistently. |
Given:
This
@Validated
works:But this (applied on a List) not:
Change to this, still not work:
If changing to
@RequestBody List<@Validated(VocabularyCreation.class) VocabularyRequest> requests
, got compile error:'@Validated' not applicable to type use
NOTE: By "not working", I mean validation group not working. Not sure if
@Valid
works or not since I didnot observe itThe text was updated successfully, but these errors were encountered: