api-layer icon indicating copy to clipboard operation
api-layer copied to clipboard

Default route path replacement is different in v3 and v2

Open pablocarle opened this issue 7 months ago • 0 comments

Is your feature request related to a problem? Please describe.

In API ML v3 with Spring Cloud Gateway, the behaviour when routing with/without trailing slashes changed from the v2 version.

The API Catalog, for example, in direct calls has a 302 redirect from the https://host:catlaog-port/apicatalog to https://host:catalog-port/apicatalog/ which makes the browser stand on the right base URL for followup requests, especially the one bootstrapping the react application.

Frontends such as a the API Catalog need to have the URL with trailing slash when going through the API Gateway (https://:) in v3.

The problem comes from the default implementation in Spring Cloud Gateway's RewritePathGatewayFilterFactory. Which when it removes the route part of the URL (ex. ui/v1) it doesn't respect whether the original client's URL had a trailing slash or not, it always keeps an ending slash.

In a real example, if I request to go to Catalog through gateway via URL https://localhost:<gw_port>/apicatalog/ui/v1 (without trailing slash), gateway will route to https://localhost:<catalog_port>/apicatalog/ which is also correct, but since the browser still thinks it's in https://localhost:<gw_port>/apicatalog/ui/v1, it will try to load https://localhost:<gw_port>/apicatalog/ui/static... which is not covered in the original routes.

Describe the solution you'd like

In my opinion the ideal solution would be to keep the behaviour as in v2, to avoidany breaking change to onboarding services. This should be implementing either option b or c from the following alternatives.

Describe alternatives you've considered a. Adding a route for UIs in /ui is a possible workaround

b. Another option which would keep the behaviour as in API ML v2 is to either replace the default RewritePathGatewayFilterFactory with a custom implementation which supports regex patterns for both with and without trailing slash.

c. Spring also recommends having a redirection filter to make sure it's handled properly, as configuration options were deprecated in Spring Framework 6.

Additional context RewritePathGatewayFilterFactory is the default Filter in Spring Cloud Gateway that handles the path replacement

2024-07-17 11:24:20.804 <ZWEAGW1:DiscoveryClient-CacheRefreshExecutor-0:71860> user DEBUG ((o.s.c.g.r.RouteDefinitionRouteLocator)) RouteDefinition localhost:apicatalog:10014:ui-v1 applying filter {regexp=/apicatalog/ui/
v1/?(?<remaining>.*), replacement=/apicatalog/${remaining}} to RewritePath

pablocarle avatar Jul 17 '24 12:07 pablocarle