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

Add support for empty path prefixes

Open NadChel opened this issue 1 year ago • 6 comments

If I set path prefixes dynamically using PrefixPathGatewayFilterFactory, and one of them happens to be an empty string, I get an exception

Caused by: java.lang.IllegalArgumentException: 'uriTemplate' must not be null

It's because its apply() method calls the UriTemplate constructor

	@Override
	public GatewayFilter apply(Config config) {
		return new GatewayFilter() {
			final UriTemplate uriTemplate = new UriTemplate(config.prefix);

which in turn checks the prefix for emptiness (not just nullness, as the exception message suggests)

	public UriTemplate(String uriTemplate) {
		Assert.hasText(uriTemplate, "'uriTemplate' must not be null");

It's super-easy to fix: simply replace

Assert.hasText(uriTemplate, "'uriTemplate' must not be null");

with

Assert.notNull(uriTemplate, "'uriTemplate' must not be null");

I want to point out that RewritePathGatewayFilterFactory does support empty replacements, for example

My Spring Cloud version is 2023.0.0, Spring Boot is at 3.2.1

NadChel avatar Dec 29 '23 14:12 NadChel

use / instead of empty path?

kimmking avatar Jan 14 '24 17:01 kimmking

PRs welcome

spencergibb avatar Mar 11 '24 21:03 spencergibb

@spencergibb I created my first PR here: https://github.com/spring-projects/spring-framework/pull/32432

bsgrd avatar Mar 13 '24 09:03 bsgrd

@spencergibb we consider addressing this in UriTemplate itself for tomorrow's 6.1.5 release, and possibly also for a backport to the 6.0.18 and 5.3.33 releases.

jhoeller avatar Mar 13 '24 10:03 jhoeller

Thanks @jhoeller !

spencergibb avatar Mar 13 '24 11:03 spencergibb