Skip to content

Support @RequestBody in interface method when computing "consumes" condition #35086

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

renatomameli
Copy link

@renatomameli renatomameli commented Jun 20, 2025

Problem

Spring currently ignores @RequestBody annotations declared on interface methods when computing the ConsumesCondition. As a result, the required = false flag is not respected, and requests without a body and without a Content-Type fail to match the mapping — even though null should be allowed.

This leads to situations where e.g. application/json is unnecessarily enforced in the ConsumesCondition, despite @RequestBody(required = false) being declared (e.g., on an interface method).

Solution

This change switches to using AnnotatedMethod for inspecting method parameters, which ensures that annotations from both interface and implementation methods are considered. This allows Spring to correctly determine whether a request body is required and to avoid enforcing a Content-Type match when required = false.

Tests

Includes tests verifying correct behavior for:

  • Interface methods with @RequestBody(required = false)
  • Implementations that override with required = true

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 20, 2025
@renatomameli renatomameli force-pushed the fix/requestbody-annotation-from-interface branch from 529b666 to a69ef16 Compare June 20, 2025 08:30
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jun 20, 2025
@renatomameli renatomameli force-pushed the fix/requestbody-annotation-from-interface branch 3 times, most recently from 23d7714 to 8a5bfb0 Compare June 20, 2025 09:58
@renatomameli renatomameli changed the title Fix support for @RequestBody on interface methods Fix consumes handling for interface @RequestBody Jun 23, 2025
@renatomameli renatomameli force-pushed the fix/requestbody-annotation-from-interface branch from 8a5bfb0 to b862eb0 Compare June 23, 2025 08:59
@sickinga
Copy link

Thank you @renatomameli. We really rely on this fix and it would be great if we can get this in an upcoming release! @sbrannen

@sbrannen sbrannen changed the title Fix consumes handling for interface @RequestBody Support @RequestBody in interface method when computing "consumes" condition Jun 23, 2025
@sbrannen
Copy link
Member

Please note that your httpExchangeAnnotation*() test methods do not involve @HttpExchange annotations.

In addition, if we were to support processing of @RequestBody annotations on parameters in interface methods, we would need to do so not only for Spring MVC (as in this PR) but also for Spring WebFlux.

In any case, we will discuss within the team whether we want to make this change, and we'll get back to you.

Cheers

Previously, @RequestBody(required = false) annotations declared
on interface methods were ignored when resolving the consumes
condition. This caused mappings to incorrectly require a request
body with a Content-Type such as application/json, even when no
body was provided.

This change uses AnnotatedMethod to retrieve parameter annotations
from both the implementation and its interfaces, ensuring that the
required flag is respected and body presence is evaluated correctly.

Signed-off-by: Renato Mameli <[email protected]>
@renatomameli renatomameli force-pushed the fix/requestbody-annotation-from-interface branch from b862eb0 to 5035306 Compare June 23, 2025 11:08
@renatomameli
Copy link
Author

Please note that your httpExchangeAnnotation*() test methods do not involve @HttpExchange annotations.

In addition, if we were to support processing of @RequestBody annotations on parameters in interface methods, we would need to do so not only for Spring MVC (as in this PR) but also for Spring WebFlux.

In any case, we will discuss within the team whether we want to make this change, and we'll get back to you.

Cheers

Thanks for the feedback! I’ve renamed them to more accurately reflect their purpose. Looking forward to your team’s decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants