Skip to content

Allow explicitly declared null in providedArgs when no argument resolver matches #35192

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

Closed
wants to merge 2 commits into from

Conversation

yongkaikai
Copy link

Overview

This PR addresses an issue where InvocableHandlerMethod#getMethodArgumentValues throws an IllegalStateException
when a method parameter is not supported by any HandlerMethodArgumentResolver and null is explicitly provided in providedArgs.

What this PR does

  • Adds a fallback check via isParameterDeclaredButNull(...) to tolerate explicit null values
  • Ensures that null in providedArgs is interpreted as a deliberate argument, avoiding spurious resolution failures

Why this is useful

In practice, this improves error handler behavior, especially for @ExceptionHandler where the HandlerMethod can be null.

Related issue

Fixes #35067

Kai added 2 commits July 12, 2025 09:53
…en no suitable argument resolver exists

Previously, when no HandlerMethodArgumentResolver supported a method parameter,
and `null` was explicitly provided in `providedArgs`, the framework would throw an IllegalStateException.

This commit improves the robustness of argument resolution in
`InvocableHandlerMethod#getMethodArgumentValues` by accepting explicitly passed
`null` values, even when no resolver is available.

Issue: spring-projects#35067
…en no suitable argument resolver exists

Previously, when no HandlerMethodArgumentResolver supported a method parameter,
and `null` was explicitly provided in `providedArgs`, the framework would throw an IllegalStateException.

This commit improves the robustness of argument resolution in
`InvocableHandlerMethod#getMethodArgumentValues` by accepting explicitly passed
`null` values, even when no resolver is available.

Issue: spring-projects#35067
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 12, 2025
@@ -218,6 +218,10 @@ public void setMethodValidator(@Nullable MethodValidator methodValidator) {
continue;
}
if (!this.resolvers.supportsParameter(parameter)) {
if (isParameterDeclaredButNull(parameter, providedArgs)) {
args[i] = null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new isParameterDeclaredButNull method checks if a parameter is explicitly set to null in providedArgs and assigns null directly, bypassing the exception. While this improves compatibility in some cases, it may hide the real cause of argument resolution failures, reducing code robustness and maintainability.

@Test
void nullHandlerMethod() throws Exception {
standaloneSetup(new PersonController()).setControllerAdvice(new GlobalExceptionHandler()).build()
.perform(get("/invalid"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only covers the scenario where handlerMethod is null, but does not cover multiple parameters, complex types, or nested parameters. Additional tests are suggested.

@rstoyanchev rstoyanchev self-assigned this Jul 15, 2025
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for giving this a try. However, the proposed solution targets much more than just the original scenario with a HandlerMethod parameter, and would pass null for any parameter type instead of flagging it as unresolved.

@rstoyanchev rstoyanchev added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resolve optional HandlerMethod parameter of @ExceptionHandler method to null
4 participants