|
| 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