spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

PathRoutePredicateFactory: matchTrailingSlash has no effect since Spring Framework 7.0

Open SinhaAmit opened this issue 5 months ago • 4 comments

Describe the bug In Spring Cloud Gateway 5.0.x (built on Spring Framework 7.0 / Spring Boot 4), the matchTrailingSlash configuration on the Path route predicate no longer has any effect.

The root cause is that Spring Framework 7.0 removed PathPatternParser.setMatchOptionalTrailingSeparator() as part of spring-projects/spring-framework#34036. The Spring Cloud Gateway PathRoutePredicateFactory previously relied on this API to implement trailing slash tolerance.

In the current main branch of Spring Cloud Gateway, the call is commented out with a FIXME:

// FIXME: 5.0.0 setMatchOptionalTrailingSeparator missing
// pathPatternParser.setMatchOptionalTrailingSeparator(config.isMatchTrailingSlash());

This means the Config.matchTrailingSlash property still exists and is configurable, but silently does nothing — which is a breaking behavioral change for users relying on it.

Expected Behavior

When matchTrailingSlash is set to true (either globally or per-route), a path predicate like Path=/api/users/{segment} should match both:

  • /api/users/1
  • /api/users/1/

This was the documented behavior prior to Spring Cloud Gateway 5.0.

Actual Behavior

The path predicate /api/users/{segment} only matches /api/users/1. A request to /api/users/1/ returns 404 regardless of the matchTrailingSlash setting.

Versions Affected

  • Spring Cloud Gateway: 5.0.1
  • Spring Framework: 7.0.7
  • Spring Boot: 4.0.x

Spring's Original Behavior (pre-removal)

From the Spring Framework 6.2.x source (PathPatternParser Javadoc):

"If set to true a PathPattern without a trailing slash will also match request paths with a trailing slash."

The behavior was one-directional:

  • Pattern /api/users matches request /api/users/ ✓
  • Pattern /api/users/ does NOT match request /api/users ✗

The matching was handled internally by individual PathElement implementations during pattern traversal via MatchingContext.isMatchOptionalTrailingSeparator().

Sample Since PathPatternParser no longer supports this natively, Spring Cloud Gateway should implement the trailing slash tolerance at the predicate level. A possible approach:

  1. When matchTrailingSlash is true and the initial PathPattern.matches(path) fails:
  2. If the request path ends with /, strip it and retry the match
  3. Always restore the original path container in the exchange to avoid leaking state to downstream filters

This preserves the original one-directional semantics (patterns without trailing slash match requests with trailing slash) without depending on removed PathPatternParser internals.

SinhaAmit avatar Jun 15 '26 12:06 SinhaAmit

Hi, I would like to help with this issue.

From what I understand, matchTrailingSlash still exists on the Path route predicate, but it no longer has an effect in Spring Cloud Gateway 5.x because Spring Framework 7 removed PathPatternParser#setMatchOptionalTrailingSeparator(boolean).

I can start by adding a focused regression test showing that a route such as /api/users/{id} should match /api/users/1/ when matchTrailingSlash=true, and then restore the previous one-directional behavior at the Gateway predicate level.

Would a contribution in that direction be welcome?

king-407 avatar Jun 16 '26 18:06 king-407

Please wait until after the team has triaged the issue

spencergibb avatar Jun 16 '26 19:06 spencergibb

Hi, is there any update for this issue?

vu00335 avatar Jul 23 '26 10:07 vu00335

We ran into this after upgrading to Spring Boot 4.1.2 / Spring Cloud 2025.1, still present in spring-cloud-gateway-server-webflux 5.0.4 with Spring Framework 7.0.10.

Workaround: list the trailing-slash variant as an explicit second pattern.

Samyssmile avatar Nov 12 '26 09:11 Samyssmile