Skip to content

Commit 9cbc830

Browse files
authored
Handle raw Map encoding and decoding in JacksonJrDecoder (#3726)
1 parent 2886eb0 commit 9cbc830

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎jackson-jr/src/main/java/feign/jackson/jr/JacksonJrDecoder.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ protected Transformer findTransformer(Response response, Type type) {
111111
}
112112
if (type instanceof Class) {
113113
Class<?> clazz = (Class<?>) type;
114+
// jackson-jr 2.23.0 throws exception when calling `beanFrom` with raw Map
115+
// See https://github.com/FasterXML/jackson-jr/issues/229
116+
if (clazz.equals(Map.class)) {
117+
return JSON::mapFrom;
118+
}
114119
return (mapper, reader) -> mapper.beanFrom(clazz, reader);
115120
}
116121
throw new DecodeException(500, "Cannot decode type: " + type.getTypeName(), response.request());
@@ -132,7 +137,13 @@ public Object convert(Object object, Type type) throws IOException {
132137
type = rawType;
133138
}
134139
if (type instanceof Class) {
135-
return mapper.beanFrom((Class<?>) type, json);
140+
Class<?> clazz = (Class<?>) type;
141+
// jackson-jr 2.23.0 throws exception when calling `beanFrom` with raw Map
142+
// See https://github.com/FasterXML/jackson-jr/issues/229
143+
if (clazz.equals(Map.class)) {
144+
return mapper.mapFrom(json);
145+
}
146+
return mapper.beanFrom(clazz, json);
136147
}
137148
throw new IOException("Cannot convert to type: " + type.getTypeName());
138149
}

‎jackson-jr/src/test/java/feign/jackson/jr/JacksonCodecTest.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,43 @@ void decodesToMap() throws Exception {
243243
assertThat(map).containsEntry("name", "jim");
244244
}
245245

246+
@Test
247+
void decodesToRawMap() throws Exception {
248+
String json = "{\"name\":\"jim\",\"id\":12}";
249+
250+
Response response =
251+
Response.builder()
252+
.status(200)
253+
.reason("OK")
254+
.request(
255+
Request.create(
256+
HttpMethod.GET, "/api", Collections.emptyMap(), Request.Body.empty(), null))
257+
.headers(Collections.emptyMap())
258+
.body(json, UTF_8)
259+
.build();
260+
261+
@SuppressWarnings("unchecked")
262+
Map<String, Object> map =
263+
(Map<String, Object>) new JacksonJrDecoder().decode(response, Map.class);
264+
265+
assertThat(map).containsEntry("id", 12);
266+
assertThat(map).containsEntry("name", "jim");
267+
}
268+
269+
@Test
270+
void convertsToRawMap() throws Exception {
271+
Map<String, Object> source = new LinkedHashMap<>();
272+
source.put("name", "jim");
273+
source.put("id", 12);
274+
275+
@SuppressWarnings("unchecked")
276+
Map<String, Object> map =
277+
(Map<String, Object>) new JacksonJrDecoder().convert(source, Map.class);
278+
279+
assertThat(map).containsEntry("id", 12);
280+
assertThat(map).containsEntry("name", "jim");
281+
}
282+
246283
public static class Zone {
247284

248285
private String name;

0 commit comments

Comments
 (0)