Skip to content

fix: read @ResponseStatus from the controller class - #3352

Open
MaxFreedomPollard wants to merge 1 commit into
springdoc:mainfrom
MaxFreedomPollard:response-status-on-controller-class
Open

fix: read @ResponseStatus from the controller class#3352
MaxFreedomPollard wants to merge 1 commit into
springdoc:mainfrom
MaxFreedomPollard:response-status-on-controller-class

Conversation

@MaxFreedomPollard

Copy link
Copy Markdown

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant