Only non-null input resets scheme specific part

Closes gh-24444
This commit is contained in:
Rossen Stoyanchev 2020-01-30 06:16:43 +00:00
parent 688167a7df
commit cc4261c30b
2 changed files with 29 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -551,7 +551,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
@Override @Override
public UriComponentsBuilder host(@Nullable String host) { public UriComponentsBuilder host(@Nullable String host) {
this.host = host; this.host = host;
resetSchemeSpecificPart(); if (host != null) {
resetSchemeSpecificPart();
}
return this; return this;
} }
@ -559,14 +561,18 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
public UriComponentsBuilder port(int port) { public UriComponentsBuilder port(int port) {
Assert.isTrue(port >= -1, "Port must be >= -1"); Assert.isTrue(port >= -1, "Port must be >= -1");
this.port = String.valueOf(port); this.port = String.valueOf(port);
resetSchemeSpecificPart(); if (port > -1) {
resetSchemeSpecificPart();
}
return this; return this;
} }
@Override @Override
public UriComponentsBuilder port(@Nullable String port) { public UriComponentsBuilder port(@Nullable String port) {
this.port = port; this.port = port;
resetSchemeSpecificPart(); if (port != null) {
resetSchemeSpecificPart();
}
return this; return this;
} }
@ -604,11 +610,11 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
String value = matcher.group(3); String value = matcher.group(3);
queryParam(name, (value != null ? value : (StringUtils.hasLength(eq) ? "" : null))); queryParam(name, (value != null ? value : (StringUtils.hasLength(eq) ? "" : null)));
} }
resetSchemeSpecificPart();
} }
else { else {
this.queryParams.clear(); this.queryParams.clear();
} }
resetSchemeSpecificPart();
return this; return this;
} }
@ -617,8 +623,8 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
this.queryParams.clear(); this.queryParams.clear();
if (query != null) { if (query != null) {
query(query); query(query);
resetSchemeSpecificPart();
} }
resetSchemeSpecificPart();
return this; return this;
} }
@ -651,6 +657,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
public UriComponentsBuilder queryParams(@Nullable MultiValueMap<String, String> params) { public UriComponentsBuilder queryParams(@Nullable MultiValueMap<String, String> params) {
if (params != null) { if (params != null) {
this.queryParams.addAll(params); this.queryParams.addAll(params);
resetSchemeSpecificPart();
} }
return this; return this;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -769,6 +769,21 @@ public class UriComponentsBuilderTests {
assertThat(uriComponents.getQueryParams().get("bar").get(0)).isNull(); assertThat(uriComponents.getQueryParams().get("bar").get(0)).isNull();
} }
@Test // gh-24444
public void opaqueUriDoesNotResetOnNullInput() throws URISyntaxException {
URI uri = new URI("urn:ietf:wg:oauth:2.0:oob");
UriComponents result = UriComponentsBuilder.fromUri(uri)
.host(null)
.port(-1)
.port(null)
.queryParams(null)
.replaceQuery(null)
.query(null)
.build();
assertThat(result.toUri()).isEqualTo(uri);
}
@Test @Test
public void relativeUrls() { public void relativeUrls() {
String baseUrl = "https://example.com"; String baseUrl = "https://example.com";