Browse Source

Fix NPE when calling NettyHeadersAdapter.add()

Prior to this commit, the `NettyHeadersAdapter` would directly delegate
the `add()` and `set()` calls to the adapted
`io.netty.handler.codec.http.HttpHeaders`. This implementation rejects
`null` values with exceptions.

This commit aligns the behavior here with other implementations, by not
rejecting null values but simply ignoring them.

Fixes gh-26274
pull/26280/head
Brian Clozel 4 years ago
parent
commit
83c19cd60e
  1. 8
      spring-web/src/main/java/org/springframework/http/client/reactive/NettyHeadersAdapter.java
  2. 8
      spring-web/src/main/java/org/springframework/http/server/reactive/NettyHeadersAdapter.java
  3. 8
      spring-web/src/test/java/org/springframework/http/server/reactive/HeadersAdaptersTests.java

8
spring-web/src/main/java/org/springframework/http/client/reactive/NettyHeadersAdapter.java

@ -56,7 +56,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> { @@ -56,7 +56,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void add(String key, @Nullable String value) {
this.headers.add(key, value);
if (value != null) {
this.headers.add(key, value);
}
}
@Override
@ -71,7 +73,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> { @@ -71,7 +73,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void set(String key, @Nullable String value) {
this.headers.set(key, value);
if (value != null) {
this.headers.set(key, value);
}
}
@Override

8
spring-web/src/main/java/org/springframework/http/server/reactive/NettyHeadersAdapter.java

@ -56,7 +56,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> { @@ -56,7 +56,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void add(String key, @Nullable String value) {
this.headers.add(key, value);
if (value != null) {
this.headers.add(key, value);
}
}
@Override
@ -71,7 +73,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> { @@ -71,7 +73,9 @@ class NettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void set(String key, @Nullable String value) {
this.headers.set(key, value);
if (value != null) {
this.headers.set(key, value);
}
}
@Override

8
spring-web/src/test/java/org/springframework/http/server/reactive/HeadersAdaptersTests.java

@ -94,6 +94,14 @@ class HeadersAdaptersTests { @@ -94,6 +94,14 @@ class HeadersAdaptersTests {
assertThat(headers.get("TestHeader").size()).isEqualTo(1);
}
@ParameterizedHeadersTest
void nullValuesShouldNotFail(String displayName, MultiValueMap<String, String> headers) {
headers.add("TestHeader", null);
assertThat(headers.getFirst("TestHeader")).isNull();
headers.set("TestHeader", null);
assertThat(headers.getFirst("TestHeader")).isNull();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ParameterizedTest(name = "[{index}] {0}")

Loading…
Cancel
Save