8000 Fix StripPrefix and RewritePath filters including servlet context-path by garvit-joshi · Pull Request #4089 · spring-cloud/spring-cloud-gateway · GitHub
[go: up one dir, main page]

Skip to content

Fix StripPrefix and RewritePath filters including servlet context-path#4089

Open
garvit-joshi wants to merge 3 commits intospring-cloud:mainfrom
garvit-joshi:fix/strip-prefix-context-path-4088
Open

Fix StripPrefix and RewritePath filters including servlet context-path#4089
garvit-joshi wants to merge 3 commits intospring-cloud:mainfrom
garvit-joshi:fix/strip-prefix-context-path-4088

Conversation

@garvit-joshi
Copy link
@garvit-joshi garvit-joshi commented Mar 7, 2026

StripPrefix and RewritePath filters in the WebMVC variant operated on the full URI path including the servlet context-path, while the Path route predicate matches against the path without it. This caused incorrect segment counting and path rewriting when server.servlet.context-path is configured.

Add MvcUtils.stripContextPath() to remove the context-path prefix before path manipulation, aligning filter behavior with predicate matching.

Closes #4088

@garvit-joshi garvit-joshi force-pushed the fix/strip-prefix-context-path-4088 branch from 60ae2f3 to c37295f Compare March 7, 2026 18:18
@garvit-joshi garvit-joshi force-pushed the fix/strip-prefix-context-path-4088 branch from c37295f to c13ff9c Compare March 7, 2026 18:21
@garvit-joshi
Copy link
Author

The failing test is in the webflux module and seems unrelated to my changes — I only modified the webmvc module.

@garvit-joshi garvit-joshi force-pushed the fix/strip-prefix-context-path-4088 branch 2 times, most recently from a78302f to b8aeb12 Compare March 12, 2026 16:46
Fixes spring-cloudgh-4088

Co-authored-by: Garvit Joshi <garvitjoshi9@gmail.com>
Signed-off-by: Garvit Joshi <garvitjoshi9@gmail.com>
Signed-off-by: Garvit Joshi <garvitjoshi9@gmail.com>
@garvit-joshi garvit-joshi force-pushed the fix/strip-prefix-context-path-4088 branch from 9e465e3 to 155d4b2 Compare March 12, 2026 17:24
Copy link
Contributor
Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a mismatch in the WebMVC gateway between how Path predicates match (path within the servlet context) and how path-manipulating filters (StripPrefix, RewritePath, PrefixPath) rewrite (previously using the full URI path including the servlet context-path). It introduces an opt-in mechanism to strip the servlet context-path before applying route filters.

Changes:

  • Add MvcUtils.stripContextPath(...) and BeforeFilterFunctions.stripContextPath() to remove the servlet context-path from the request URI before downstream path manipulation.
  • Add spring.cloud.gateway.server.webmvc.strip-context-path property and wire it into route construction via a per-route before(...) processor.
  • Add unit tests covering context-path stripping and expected interactions with StripPrefix, RewritePath, and PrefixPath.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtils.java Adds a utility to strip servlet context-path from a provided path string.
spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctions.java Introduces a new before-filter function that rewrites the request URI by removing the context-path.
spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/RouterFunctionHolderFactory.java Optionally applies the new before-filter to routes when the new property is enabled.
spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcProperties.java Adds the stripContextPath configuration property (default false).
spring-cloud-gateway-server-webmvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctionsTests.java Adds unit tests for stripping context-path and behavior with other path filters.
spring-cloud-gateway-server-webmvc/src/test/java/org/springframework/cloud/gateway/server/mvc/common/MvcUtilsTests.java Adds unit tests for MvcUtils.stripContextPath(...).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +204 to +206
if (stripContextPath) {
builder.before(BeforeFilterFunctions.stripContextPath());
}
Copy link
Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

The new stripContextPath behavior is wired into route construction here, but there’s no test coverage ensuring the property actually results in the BeforeFilterFunctions.stripContextPath() processor being applied (and applied before other route filters). Since there are already tests verifying route construction in GatewayMvcPropertiesBeanDefinitionRegistrarTests, consider adding a focused test that sets spring.cloud.gateway.server.webmvc.strip-context-path=true and asserts the resulting RouterFunction/filter chain (or an integration test using server.servlet.context-path) behaves as intended.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Member
@spencergibb spencergibb Mar 12, 2026

Choose a reason for hiding this comment

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

I'm not sure I like adding filters here. One of my rules building the WebMVC gateway server was no implicit or magic filters. This goes against this rule.

Comment on lines +228 to +230
// Simulate global filter stripping context path first
ServerRequest stripped = BeforeFilterFunctions.stripContextPath().apply(request);
ServerRequest result = BeforeFilterFunctions.stripPrefix(2).apply(stripped);
Copy link
Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

These tests describe the setup as a “global filter stripping context path first”, but the production wiring adds stripContextPath() as a per-route before(...) processor (controlled by spring.cloud.gateway.server.webmvc.strip-context-path). Consider rewording the comment to match the actual behavior so the tests don’t suggest an implementation detail that doesn’t exist.

Copilot uses AI. Check for mistakes.
@ryanjbaxter
Copy link
Contributor

Please add some documentation for the new functionality

Copy link
Member
@spencergibb spencergibb left a comment

Choose a reason for hiding this comment

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

I'm beginning to think we need to support this like we do like adaptCachedBody. If someone uses a predicate like readBody they are required to use adaptCachedBody later. If someone is using a servlet context path, they should use a strip context path filter.

@garvit-joshi
Copy link
Author
garvit-joshi commented Mar 13, 2026

Got it, Should I add something like:

spring.cloud.gateway.server.webmvc.routes[0].filters[0]=StripContextPath
spring.cloud.gateway.server.webmvc.routes[0].filters[1]=StripPrefix=1

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StripPrefix filter includes server.servlet.context-path in segment count (WebMVC variant)

5 participants

0