fix: read @ResponseStatus from the controller class - #3352
Open
MaxFreedomPollard wants to merge 1 commit into
Open
fix: read @ResponseStatus from the controller class#3352MaxFreedomPollard wants to merge 1 commit into
MaxFreedomPollard wants to merge 1 commit into
Conversation
A @ResponseStatus on a controller class applies to every handler method that controller declares, and Spring returns that status at runtime. Operations on such a controller were documented as 200 instead. GenericResponseService.evaluateResponseStatus takes the method and the handler bean type, and falls back to the bean type when the method itself carries no @ResponseStatus, the same order Spring's own HandlerMethod uses. All four call sites passed methodParameter.getMethod().getClass(), which is java.lang.reflect.Method rather than the controller, so that fallback could never match. They now pass methodParameter.getContainingClass(), which is the handler bean type for a MethodParameter taken from a HandlerMethod and the declaring class otherwise. The guard in buildApiResponses that appends the @ResponseStatus code next to explicit @apiresponse entries looks at the controller class as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A @ResponseStatus on a controller class was ignored, so every operation on that controller was documented as 200 while Spring returns the class-level status at runtime. GenericResponseService passed methodParameter.getMethod().getClass(), which is java.lang.reflect.Method rather than the controller, as the beanType argument of evaluateResponseStatus, so the fallback from the method to the class could never match. It now passes methodParameter.getContainingClass(), the handler bean type, giving the method-then-class order Spring's own HandlerMethod uses, and the guard that appends the @ResponseStatus code next to explicit @apiresponse entries looks at the controller class as well. SpringDocApp273Test under v30 and v31 covers a controller annotated @ResponseStatus(CREATED) with one method that sets ACCEPTED itself, and it fails on main with 200 where 201 is expected and passes with this change. I did not find an existing issue for this one.