-
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
Standalone MockMvc ignores @RestControllerAdvice annotation attributes #25520
Comments
Thanks for raising the issue and providing the example project to debug. Much appreciated! I've determined that there is a bug in the implementation of |
This issue is related to: |
The fix has been merged into Feel free to try it out in an upcoming 5.2.9 or 5.3 M2 snapshot. Thanks again for raising the issue. 👍 |
This commit introduces regression tests for @RestControllerAdvice support in standalone MockMvc configurations. See gh-25520
This commit introduces regression tests for @RestControllerAdvice support in standalone MockMvc configurations. See gh-25520
This commit introduces regression tests for @RestControllerAdvice support in standalone MockMvc configurations. See gh-25520
Thank you! |
Since Spring Framework 5.2, @RestControllerAdvice registered with MockMvc when using MockMvcBuilders.standaloneSetup() has no longer been properly supported if annotation attributes were declared in the @RestControllerAdvice annotation. Prior to 5.2, this was not an issue. The cause for this regression is two-fold. 1. Commit 50c2577 refactored DefaultListableBeanFactory so that findAnnotationOnBean() supports merged annotations; however, that commit did not refactor StaticListableBeanFactory#findAnnotationOnBean() to support merged annotations. 2. Commit 978adbd refactored ControllerAdviceBean so that a merged @ControllerAdvice annotation is only looked up via ApplicationContext#findAnnotationOnBean(). The latter relies on the fact that findAnnotationOnBean() supports merged annotations (e.g., @RestControllerAdvice as a merged instance of @ControllerAdvice). Behind the scenes, MockMvcBuilders.standaloneSetup() creates a StubWebApplicationContext which internally uses a StubBeanFactory which extends StaticListableBeanFactory. Consequently, since the implementation of findAnnotationOnBean() in StaticListableBeanFactory was not updated to support merged annotations like it was in DefaultListableBeanFactory, we only see this regression with the standalone MockMvc support and not with MockMvc support for an existing WebApplicationContext or with standard Spring applications using an ApplicationContext that uses DefaultListableBeanFactory. This commit fixes this regression by supporting merged annotations in StaticListableBeanFactory#findAnnotationOnBean() as well. Closes spring-projectsgh-25520
This commit introduces regression tests for @RestControllerAdvice support in standalone MockMvc configurations. See spring-projectsgh-25520
MockMvc
created byMockMvcBuilders.standaloneSetup()
ignores@RestControllerAdvice
annotation attributes but works well for a@ControllerAdvice
/ResponseBody
pair.Example: https://github.com/thecederick/MockMvc-ignores-RestControllerAdvice-annotation-fields
Failing Configuration
Controller:
Controller advice:
Test:
Working Configuration
Controller advice:
Test:
Default advice
Summary
In the first configuration using the
@RestControllerAdvice
annotation the test fails; with the second one, it passes as expected.The text was updated successfully, but these errors were encountered: