Skip to content

Commit 85ca9f4

Browse files
committed
Support reading unresolvable types in AbstractJacksonHttpMessageConverter
Closes gh-35889
1 parent e228702 commit 85ca9f4

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

spring-web/src/main/java/org/springframework/http/converter/AbstractJacksonHttpMessageConverter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,7 @@ public boolean canRead(ResolvableType type, @Nullable MediaType mediaType) {
264264
if (!canRead(mediaType)) {
265265
return false;
266266
}
267-
Class<?> clazz = type.resolve();
268-
if (clazz == null) {
269-
return false;
270-
}
271-
return this.mapperRegistrations == null || selectMapper(clazz, mediaType) != null;
267+
return this.mapperRegistrations == null || selectMapper(type.toClass(), mediaType) != null;
272268
}
273269

274270
@Override

spring-web/src/test/java/org/springframework/http/converter/json/JacksonJsonHttpMessageConverterTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.core.ParameterizedTypeReference;
4949
import org.springframework.core.ResolvableType;
5050
import org.springframework.http.MediaType;
51+
import org.springframework.http.ResponseEntity;
5152
import org.springframework.http.converter.HttpMessageNotReadableException;
5253
import org.springframework.web.testfixture.http.MockHttpInputMessage;
5354
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
@@ -67,6 +68,8 @@ class JacksonJsonHttpMessageConverterTests {
6768

6869
private JacksonJsonHttpMessageConverter converter = new JacksonJsonHttpMessageConverter();
6970

71+
private final ResolvableType unresolvableType = ResolvableType.forClass(ResponseEntity.class).getNested(2);
72+
7073

7174
@Test
7275
void canRead() {
@@ -75,6 +78,7 @@ void canRead() {
7578
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.UTF_8))).isTrue();
7679
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.US_ASCII))).isTrue();
7780
assertThat(this.converter.canRead(MyBean.class, new MediaType("application", "json", StandardCharsets.ISO_8859_1))).isTrue();
81+
assertThat(this.converter.canRead(this.unresolvableType, MediaType.APPLICATION_JSON)).isTrue();
7882
}
7983

8084
@Test
@@ -173,6 +177,29 @@ void readUntyped() throws IOException {
173177
assertThat(result).containsEntry("bool", Boolean.TRUE);
174178
assertThat(result).containsEntry("bytes", "AQI=");
175179
}
180+
@Test
181+
@SuppressWarnings("unchecked")
182+
void readUnresolable() throws IOException {
183+
String body = "{" +
184+
"\"bytes\":\"AQI=\"," +
185+
"\"array\":[\"Foo\",\"Bar\"]," +
186+
"\"number\":42," +
187+
"\"string\":\"Foo\"," +
188+
"\"bool\":true," +
189+
"\"fraction\":42.0}";
190+
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
191+
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
192+
HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(this.unresolvableType, inputMessage, null);
193+
assertThat(result).containsEntry("string", "Foo");
194+
assertThat(result).containsEntry("number", 42);
195+
assertThat((Double) result.get("fraction")).isCloseTo(42D, within(0D));
196+
List<String> array = new ArrayList<>();
197+
array.add("Foo");
198+
array.add("Bar");
199+
assertThat(result).containsEntry("array", array);
200+
assertThat(result).containsEntry("bool", Boolean.TRUE);
201+
assertThat(result).containsEntry("bytes", "AQI=");
202+
}
176203

177204
@Test
178205
void write() throws IOException {

0 commit comments

Comments
 (0)