Skip to content

Commit 28db3a2

Browse files
authored
Fix #185 GuavaCollectionDeserializer does not respect JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY (#193)
1 parent 1758c6f commit 28db3a2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

guava/src/main/java/com/fasterxml/jackson/datatype/guava/deser/GuavaCollectionDeserializer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ public T deserialize(JsonParser p, DeserializationContext ctxt)
138138
return _deserializeContents(p, ctxt);
139139
}
140140
// But may support implicit arrays from single values?
141-
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) {
141+
final boolean canWrap = (_unwrapSingle == Boolean.TRUE) ||
142+
((_unwrapSingle == null) && ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
143+
if (canWrap) {
142144
return _deserializeFromSingleValue(p, ctxt);
143145
}
146+
// Otherwise, we have a problem
144147
return (T) ctxt.handleUnexpectedToken(_valueClass, p);
145148
}
146149

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fasterxml.jackson.datatype.guava;
2+
3+
import java.util.List;
4+
5+
import com.google.common.collect.ImmutableList;
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.fasterxml.jackson.annotation.JsonFormat;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
// [datatype-guava#185] : `GuavaCollectionDeserializer` does not respect
14+
// `JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY`
15+
public class ImmutableListAcceptSingle185Test
16+
extends ModuleTestBase
17+
{
18+
static class Line {
19+
public String data;
20+
}
21+
22+
static class GuavaContainer185 {
23+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
24+
public ImmutableList<Line> lines;
25+
}
26+
27+
static class JavaContainer185 {
28+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
29+
public List<Line> lines;
30+
}
31+
32+
private final ObjectMapper MAPPER = mapperWithModule();
33+
34+
// Sanity Check, JDK List works by default
35+
@Test
36+
public void testJDKListWithSingleValue()
37+
throws Exception
38+
{
39+
String json = "{\"lines\":{\"data\":\"something-jdk\"}}";
40+
41+
JavaContainer185 javaContainer = MAPPER.readValue(json, JavaContainer185.class);
42+
assertEquals(1, javaContainer.lines.size());
43+
assertEquals("something-jdk", javaContainer.lines.get(0).data);
44+
}
45+
46+
// Guava's ImmutableList does not work, but should
47+
@Test
48+
public void testGuavaImmutableListWithSingleValue()
49+
throws Exception
50+
{
51+
String json = "{\"lines\":{\"data\":\"something-guava\"}}";
52+
53+
GuavaContainer185 container = MAPPER.readValue(json, GuavaContainer185.class);
54+
assertEquals(1, container.lines.size());
55+
assertEquals("something-guava", container.lines.get(0).data);
56+
}
57+
}

0 commit comments

Comments
 (0)