diff --git a/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java b/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java index 5864863e4e..03e364e43f 100644 --- a/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java +++ b/spring-web/src/main/java/org/springframework/web/client/MessageBodyClientHttpResponseWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,8 +79,13 @@ class MessageBodyClientHttpResponseWrapper implements ClientHttpResponse { * @return {@code true} if the response has a zero-length message body, {@code false} otherwise * @throws IOException in case of I/O errors */ + @SuppressWarnings("ConstantConditions") public boolean hasEmptyMessageBody() throws IOException { InputStream body = this.response.getBody(); + // Per contract body shouldn't be null, but check anyway.. + if (body == null) { + return true; + } if (body.markSupported()) { body.mark(1); if (body.read() == -1) { diff --git a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java index f8dea0c25c..0556f2ffc8 100644 --- a/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java +++ b/spring-web/src/test/java/org/springframework/http/codec/json/Jackson2JsonDecoderTests.java @@ -175,7 +175,7 @@ public class Jackson2JsonDecoderTests extends AbstractDecoderTestCase result = this.decoder.decode(Flux.concat(stringBuffer("null")), ResolvableType.forType(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap()); diff --git a/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java b/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java index 1452d45d4a..4bb9b90d51 100644 --- a/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/HttpMessageConverterExtractorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -113,6 +113,20 @@ public class HttpMessageConverterExtractorTests { assertNull(result); } + @Test // gh-22265 + @SuppressWarnings("unchecked") + public void nullMessageBody() throws IOException { + HttpMessageConverter converter = mock(HttpMessageConverter.class); + HttpHeaders responseHeaders = new HttpHeaders(); + extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); + given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); + given(response.getHeaders()).willReturn(responseHeaders); + given(response.getBody()).willReturn(null); + + Object result = extractor.extractData(response); + assertNull(result); + } + @Test @SuppressWarnings("unchecked") public void normal() throws IOException {