Skip to content

Commit 199c4ef

Browse files
authored
Additional test coverage for ACCEPT_SINGLE_VALUE_AS_ARRAY (#2921)
Note that `testSingleElementWithStringFactoryRead` currently fails on 2.12, but passes on 2.11.
1 parent d8bc0a1 commit 199c4ef

File tree

1 file changed

+110
-1
lines changed

1 file changed

+110
-1
lines changed

src/test/java/com/fasterxml/jackson/databind/struct/FormatFeatureAcceptSingleTest.java

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.fasterxml.jackson.databind.struct;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
35
import java.util.EnumSet;
46
import java.util.List;
57

8+
import com.fasterxml.jackson.annotation.JsonCreator;
69
import com.fasterxml.jackson.annotation.JsonFormat;
10+
import com.fasterxml.jackson.annotation.JsonProperty;
711
import com.fasterxml.jackson.databind.BaseMapTest;
812
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
914

1015
public class FormatFeatureAcceptSingleTest extends BaseMapTest
1116
{
@@ -51,6 +56,33 @@ static class StringListWrapper {
5156
public List<String> values;
5257
}
5358

59+
@JsonDeserialize(builder = StringListWrapperWithBuilder.Builder.class)
60+
static class StringListWrapperWithBuilder {
61+
public final List<String> values;
62+
63+
private StringListWrapperWithBuilder(List<String> values) {
64+
this.values = values;
65+
}
66+
67+
static class Builder {
68+
private List<String> values = Collections.emptyList();
69+
70+
@JsonProperty
71+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
72+
public Builder values(Iterable<? extends String> elements) {
73+
values = new ArrayList<>();
74+
for (String value : elements) {
75+
values.add(value);
76+
}
77+
return this;
78+
}
79+
80+
public StringListWrapperWithBuilder build() {
81+
return new StringListWrapperWithBuilder(values);
82+
}
83+
}
84+
}
85+
5486
static class EnumSetWrapper {
5587
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
5688
public EnumSet<ABC> values;
@@ -66,11 +98,60 @@ static class RolesInList {
6698
public List<Role> roles;
6799
}
68100

101+
@JsonDeserialize(builder = RolesInListWithBuilder.Builder.class)
102+
static class RolesInListWithBuilder {
103+
public final List<Role> roles;
104+
105+
private RolesInListWithBuilder(List<Role> roles) {
106+
this.roles = roles;
107+
}
108+
109+
static class Builder {
110+
private List<Role> values = Collections.emptyList();
111+
112+
@JsonProperty
113+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
114+
public Builder roles(Iterable<? extends Role> elements) {
115+
values = new ArrayList<>();
116+
for (Role value : elements) {
117+
values.add(value);
118+
}
119+
return this;
120+
}
121+
122+
public RolesInListWithBuilder build() {
123+
return new RolesInListWithBuilder(values);
124+
}
125+
}
126+
}
127+
128+
static class WrapperWithStringFactoryInList {
129+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
130+
public List<WrapperWithStringFactory> values;
131+
}
132+
69133
static class Role {
70134
public String ID;
71135
public String Name;
72136
}
73137

138+
@JsonDeserialize
139+
static class WrapperWithStringFactory {
140+
private final Role role;
141+
142+
private WrapperWithStringFactory(Role role) {
143+
this.role = role;
144+
}
145+
146+
@JsonCreator
147+
static WrapperWithStringFactory from(String value) {
148+
Role role = new Role();
149+
role.ID = "1";
150+
role.Name = value;
151+
return new WrapperWithStringFactory(role);
152+
}
153+
}
154+
74155
private final ObjectMapper MAPPER = new ObjectMapper();
75156

76157
/*
@@ -150,7 +231,7 @@ public void testSingleElementArrayRead() throws Exception {
150231
assertEquals(1, response.roles.length);
151232
assertEquals("333", response.roles[0].ID);
152233
}
153-
234+
154235
public void testSingleStringListRead() throws Exception {
155236
String json = aposToQuotes(
156237
"{ 'values': 'first' }");
@@ -160,6 +241,16 @@ public void testSingleStringListRead() throws Exception {
160241
assertEquals("first", result.values.get(0));
161242
}
162243

244+
public void testSingleStringListReadWithBuilder() throws Exception {
245+
String json = aposToQuotes(
246+
"{ 'values': 'first' }");
247+
StringListWrapperWithBuilder result =
248+
MAPPER.readValue(json, StringListWrapperWithBuilder.class);
249+
assertNotNull(result.values);
250+
assertEquals(1, result.values.size());
251+
assertEquals("first", result.values.get(0));
252+
}
253+
163254
public void testSingleElementListRead() throws Exception {
164255
String json = aposToQuotes(
165256
"{ 'roles': { 'Name': 'User', 'ID': '333' } }");
@@ -169,6 +260,24 @@ public void testSingleElementListRead() throws Exception {
169260
assertEquals("333", response.roles.get(0).ID);
170261
}
171262

263+
public void testSingleElementListReadWithBuilder() throws Exception {
264+
String json = aposToQuotes(
265+
"{ 'roles': { 'Name': 'User', 'ID': '333' } }");
266+
RolesInListWithBuilder response = MAPPER.readValue(json, RolesInListWithBuilder.class);
267+
assertNotNull(response.roles);
268+
assertEquals(1, response.roles.size());
269+
assertEquals("333", response.roles.get(0).ID);
270+
}
271+
272+
public void testSingleElementWithStringFactoryRead() throws Exception {
273+
String json = aposToQuotes(
274+
"{ 'values': '333' }");
275+
WrapperWithStringFactoryInList response = MAPPER.readValue(json, WrapperWithStringFactoryInList.class);
276+
assertNotNull(response.values);
277+
assertEquals(1, response.values.size());
278+
assertEquals("333", response.values.get(0).role.Name);
279+
}
280+
172281
public void testSingleEnumSetRead() throws Exception {
173282
EnumSetWrapper result = MAPPER.readValue(aposToQuotes("{ 'values': 'B' }"),
174283
EnumSetWrapper.class);

0 commit comments

Comments
 (0)