Skip to content

Commit 4560dc2

Browse files
committed
Improve empty body check
Fixes spring-projectsgh-22265
1 parent d0033f1 commit 4560dc2

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,8 +79,13 @@ public boolean hasMessageBody() throws IOException {
7979
* @return {@code true} if the response has a zero-length message body, {@code false} otherwise
8080
* @throws IOException in case of I/O errors
8181
*/
82+
@SuppressWarnings("ConstantConditions")
8283
public boolean hasEmptyMessageBody() throws IOException {
8384
InputStream body = this.response.getBody();
85+
// Per contract body shouldn't be null, but check anyway..
86+
if (body == null) {
87+
return true;
88+
}
8489
if (body.markSupported()) {
8590
body.mark(1);
8691
if (body.read() == -1) {

spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void invalidData() {
175175
.verifyError(DecodingException.class));
176176
}
177177

178-
@Test // #22042
178+
@Test // gh-22042
179179
public void decodeWithNullLiteral() {
180180
Flux<Object> result = this.decoder.decode(Flux.concat(stringBuffer("null")),
181181
ResolvableType.forType(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap());

spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,6 +113,20 @@ public void emptyMessageBody() throws IOException {
113113
assertNull(result);
114114
}
115115

116+
@Test // gh-22265
117+
@SuppressWarnings("unchecked")
118+
public void nullMessageBody() throws IOException {
119+
HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
120+
HttpHeaders responseHeaders = new HttpHeaders();
121+
extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter));
122+
given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value());
123+
given(response.getHeaders()).willReturn(responseHeaders);
124+
given(response.getBody()).willReturn(null);
125+
126+
Object result = extractor.extractData(response);
127+
assertNull(result);
128+
}
129+
116130
@Test
117131
@SuppressWarnings("unchecked")
118132
public void normal() throws IOException {

0 commit comments

Comments
 (0)