Skip to content

Copy builder state in HandlerTypePredicate - #37599

Merged
sbrannen merged 1 commit into
spring-projects:mainfrom
Tanishq-Choudhary:handler-type-predicate-builder-copy
Nov 21, 2026
Merged

sbrannen merged 1 commit into
spring-projects:mainfrom
Tanishq-Choudhary:handler-type-predicate-builder-copy

Conversation

@Tanishq-Choudhary

Copy link
Copy Markdown
Contributor

HandlerTypePredicate.Builder#build() passes its own base packages, assignable types and annotations to the predicate, which only wraps them in read-only views. Changing the builder afterwards changes what an already built predicate matches:

HandlerTypePredicate.Builder builder = HandlerTypePredicate.builder().basePackage("org.example.api");
HandlerTypePredicate predicate = builder.build();
predicate.test(String.class); // false

builder.basePackage("java.lang");
predicate.test(String.class); // true

This is the same kind of problem as #37596 for RetryPolicy.Builder, so I followed the polished version of that fix and made the defensive copies in the HandlerTypePredicate constructor. The new test in HandlerTypePredicateTests checks each of the three collections and fails without the change:

HandlerTypePredicateTests > selectorsAreNotChangedByBuilderAfterBuild() FAILED
    Expecting value to be false but was true

I ran ./gradlew :spring-web:check on Windows with JDK 25, plus the WebMVC and WebFlux tests that use HandlerTypePredicate for path prefixes. The only failures were in MultipartHttpMessageWriterTests, which fail the same way on main on this machine.

Prior to this commit, HandlerTypePredicate.Builder#build() passed its
own base packages, assignable types and annotations to the predicate,
which only wrapped them in read-only views. Changing the builder after
build() therefore also changed which handler types an already built
predicate matched.

This commit makes defensive copies in the HandlerTypePredicate
constructor, as DefaultRetryPolicy does since spring-projectsgh-37596.

Signed-off-by: Tanishq Choudhary <tanishqchoudhary007@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 28, 2026
@Tanishq-Choudhary

This comment was marked as outdated.

@sbrannen sbrannen self-assigned this Nov 21, 2026
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 21, 2026
@sbrannen sbrannen changed the title Copy builder state in HandlerTypePredicate Copy builder state in HandlerTypePredicate Nov 21, 2026
@sbrannen sbrannen added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 21, 2026
@sbrannen sbrannen added this to the 7.1.1 milestone Nov 21, 2026
@sbrannen
sbrannen merged commit a5e9021 into spring-projects:main Nov 21, 2026
5 checks passed
sbrannen added a commit that referenced this pull request Nov 21, 2026
@sbrannen

Copy link
Copy Markdown
Member

This has been merged into main in a5e9021 and slightly revised in 7f391bd.

Thanks

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) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants