Skip to content

Improve And/Or-RequestMatcher/ServerWebExchangeMatcher API#18479

Merged
jzheaux merged 1 commit intospring-projects:mainfrom
ziqin:covariant-matcher
Mar 27, 2026
Merged

Improve And/Or-RequestMatcher/ServerWebExchangeMatcher API#18479
jzheaux merged 1 commit intospring-projects:mainfrom
ziqin:covariant-matcher

Conversation

@ziqin
Copy link
Copy Markdown
Contributor

@ziqin ziqin commented Jan 11, 2026

Currently, the List-receiving constructors of AndRequestMatcher, OrRequestMatcher, AndServerWebExchangeMatcher, and OrServerWebExchangeMatcher don't support covariance, which adds obstacles to users of these APIs. For example, one cannot pass a List<PathPatternRequestMatcher> to OrRequestMatcher(List<RequestMatcher>).

This PR resolves the aforementioned problem. It should not break existing code.

Currently, the List-receiving constructors of AndRequestMatcher,
OrRequestMatcher, AndServerWebExchangeMatcher, and OrServerWebExchangeMatcher
don't support covariance, which adds obstacles to users of these
APIs.  For example, one cannot pass a List<PathPatternRequestMatcher>
to OrRequestMatcher(List<RequestMatcher>).

This commit resolves the aforementioned problem.  It should not
break existing code.

Signed-off-by: Ziqin Wang <ziqin@wangziqin.net>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 11, 2026
@ziqin
Copy link
Copy Markdown
Contributor Author

ziqin commented Jan 16, 2026

I discovered this problem when working on PR #18499, where I had to explicitly specify the return type of builder::matcher as the interface RequestMatcher so that I can get a List<RequestMatcher> instead of List<PathPatternRequestMatcher>.

public ContentSecurityPolicyConfig requireCspMatchers(String... pathPatterns) {
PathPatternRequestMatcher.Builder builder = HeadersConfigurer.this.getRequestMatcherBuilder();
// @formatter:off
return this.requireCspMatcher(new OrRequestMatcher(
Arrays.stream(pathPatterns)
.map((Function<? super String, RequestMatcher>) builder::matcher)
.toList()));
// @formatter:on
}

If this PR is merged, we can simply use Arrays.stream(pathPatterns).map(builder::matcher).toList().

@jzheaux jzheaux self-assigned this Mar 27, 2026
@jzheaux jzheaux added in: web An issue in web modules (web, webmvc) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 27, 2026
@jzheaux jzheaux added this to the 7.1.0-RC1 milestone Mar 27, 2026
@jzheaux jzheaux merged commit acbf64a into spring-projects:main Mar 27, 2026
6 checks passed
@jzheaux
Copy link
Copy Markdown
Contributor

jzheaux commented Mar 27, 2026

Thanks, @ziqin! This is now merged into main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: web An issue in web modules (web, webmvc) type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants