-
Notifications
You must be signed in to change notification settings - Fork 565
@Valid
is not supported on @BasePathAwareController
#967
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
Comments
Oliver Drotbohm commented Do you have a sample project or test case to reproduce the issue? |
Bob Tiernay commented I will create one and attach |
Bob Tiernay commented Here you are: https://github.com/btiernay/DATAREST-593 Cheers |
Bob Tiernay commented I should also ask for clarification on the relationship between |
Oliver Drotbohm commented This is basically as expected:
So what you see is basically caused by the difference between a manually implemented controller using the repositories and thus triggering low level validation and the Spring Data REST controllers that trigger events on the resource level |
Bob Tiernay commented Okay, that makes a lot of sense. Thanks for explaining |
Bob Tiernay commented On second thought, shouldn't there be something in the Data REST stack that translates |
Andrea Ratto commented I would like to see this issue accepted and fixed: I refactored a controller to be a In my case validation on db save is too late: the update has been published on a queue |
Oliver Drotbohm commented I am not sure what you think should be accepted here. As I outlined in my comment above this is basically working as expected. What exactly is it that you think is not working? Are you using |
Andrea Ratto commented Basically I think that @RequestMapping(value = "/path", method = RequestMethod.POST)
public post(@Valid @RequestBody Enitity entity) {
doStuff(entity); // this line should not be reached if the entity is not valid.
} |
Oliver Drotbohm commented You're not writing a controller with Spring Data REST, it's all internal. So besides the implementation aspect, what would you like to see happening by default? Currently we require developers to activate JSR-303 support explicitly as SD REST works with domain objects and – while being a decent choice to validate DTOs – JSR-303 is a rather poor one for validating domain objects. The explicit activation requirement is basically a hint in terms of: we know it's a sub-obtimal idea, but you seem to insist on doing this |
Andrea Ratto commented Specifically I am updating our API to HAL, leveraging Spring Data Rest for reads, but providing all write methods with my custom implementation that involves sending events to rabbitmq, rather than writing directly. Thanks to I am deleting all our old custom request handlers for reads and porting the remaining code from Some of these methods for example take complex request objects and update multiple entities at once. These request object are annotated and were validated thanks to the So it surely feels I am writing a controller (in fact I am refactoring controllers) and, just like the bug description says, that an expectation was broken. If I were to swap back the I would expect |
Andrea Ratto commented ERRATA: by ResourceAssembler I meant PersistentEntityResourceAssembler |
Thibaud Lepretre commented Andrea Ratto you can create your own |
Andrea Ratto commented
|
Yuriy Yunikov commented I've created a workaround for the issue using custom RequestBodyAdviceAdapter which works for me well. Check out my answer on StackOverflow |
Daniele Renda commented Some update on this? I'm experiencing the same problem. I saw the workaround of |
Sergei Poznanski commented I agree - this is extremely necessary functionality! It must be implemented. Often we have to implement custom method in RepositoryRestController (and get access to injected PersistentEntityResourceAssembler), but we cannot do it because Bean Validation doesn't work in RepositoryRestController (as well as in BasePathAwareController). To workaround this situation we can use InitBinder in our controller: @Autowired private LocalValidatorFactoryBean beanValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addValidators(beanValidator);
} But in this case the custom validators stop working. Well, we try to add them to InitBinder: @Autowired private LocalValidatorFactoryBean beanValidator;
@Autowired private CustomValidator customValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addValidators(beanValidator, customValidator);
} But in this case Custom Validator is invoked for every DTO object, marked with It's horrible guys!.. ) |
Daniele Renda commented
Using the initBinder the response is: {
"timestamp": "2017-11-30T15:44:21.066+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"Size.restEmail.to",
"Size.to",
"Size.[Ljava.lang.String;",
"Size"
],
"arguments": [
{
"codes": [
"restEmail.to",
"to"
],
"arguments": null,
"defaultMessage": "to",
"code": "to"
},
2147483647,
1
],
"defaultMessage": "Il numero di destinatari deve essere compreso maggiore di [1]. Correggere i valori e ripetere l'operazione. ",
"objectName": "restEmail",
"field": "to",
"rejectedValue": [],
"bindingFailure": false,
"code": "Size"
}
],
"message": "Validation failed for object='restEmail'. Error count: 1",
"path": "/api/v1/emails"
} instead of the standard SDR response: {
"errors": [
{
"entity": "Email",
"property": "to",
"invalidValue": [],
"message": "Il numero di destinatari deve essere compreso maggiore di [1]. Correggere i valori e ripetere loperazione. "
}
]
} Is there a way to uniform the first response to the SDR default one? |
Sergei Poznanski commented
|
Daniele Renda commented Any update about this issue? Thanks |
Bogdan Samondros commented +1. Was faced with this case today. Very sstrange that no customization for SDR. It's realy painful |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Doesn't work for @BasePathAwareController as well. Looks like Spring Data REST doesn't inject LocalValidatorFactoryBean instance into ExtendedServletRequestDataBinder while it resolves arguments and creates new binder. |
Faced the same issue |
Really painful issue. We are thinking to not use spring data rest because of this issue. |
@bean
} protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer(
} // RepositoryRestMvcConfiguration#repositoryExporterHandlerAdapter, not autowired @bean
} My solution is: @configuration
} its work! Is it possible to modify the source code in this way:
|
Fixes: #967. Original pull request: 2108.
Fixes: #967. Original pull request: 2108.
@Valid
is not supported on @BasePathAwareController
Bob Tiernay opened DATAREST-593 and commented
@Valid
annotations are not respected on@RepositoryRestController
annotated controllers as they are with@Controller
and@RestController
classes. This breaks with convention and expectation of developers.Affects: 2.3 GA (Fowler)
Issue Links:
@BasePathAwareController
disables DTO validation("is duplicated by")
15 votes, 18 watchers
The text was updated successfully, but these errors were encountered: