Skip to content

Commit fa318d2

Browse files
committed
Fix a minor bug with Object[] deser; extend test coverage
1 parent 0ee13c6 commit fa318d2

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/main/java/tools/jackson/databind/deser/jdk/UntypedObjectDeserializerNR.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,11 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object into
144144
}
145145
break;
146146
case JsonTokenId.ID_START_ARRAY:
147-
{
148-
JsonToken t = p.nextToken(); // to get to FIELD_NAME or END_OBJECT
147+
if (intoValue instanceof Collection<?>) {
148+
JsonToken t = p.nextToken(); // to get to END_OBJECT
149149
if (t == JsonToken.END_ARRAY) {
150150
return intoValue;
151151
}
152-
}
153-
154-
if (intoValue instanceof Collection<?>) {
155152
Collection<Object> c = (Collection<Object>) intoValue;
156153
// NOTE: merge for arrays/Collections means append, can't merge contents
157154
do {
@@ -161,6 +158,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt, Object into
161158
}
162159
// 21-Apr-2017, tatu: Should we try to support merging of Object[] values too?
163160
// ... maybe future improvement
161+
164162
break;
165163
}
166164
// Easiest handling for the rest, delegate. Only (?) question: how about nulls?

src/test/java/tools/jackson/databind/deser/jdk/UntypedDeserializationTest.java

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void testUntypedMap() throws Exception
216216
}
217217

218218
@Test
219-
public void testSimpleVanillaScalars() throws IOException
219+
public void testSimpleVanillaScalars() throws Exception
220220
{
221221
assertEquals("foo", MAPPER.readValue(q("foo"), Object.class));
222222

@@ -227,14 +227,14 @@ public void testSimpleVanillaScalars() throws IOException
227227
}
228228

229229
@Test
230-
public void testSimpleVanillaStructured() throws IOException
230+
public void testSimpleVanillaStructured() throws Exception
231231
{
232232
List<?> list = (List<?>) MAPPER.readValue("[ 1, 2, 3]", Object.class);
233233
assertEquals(Integer.valueOf(1), list.get(0));
234234
}
235235

236236
@Test
237-
public void testNestedUntypes() throws IOException
237+
public void testNestedUntypes() throws Exception
238238
{
239239
// 05-Apr-2014, tatu: Odd failures if using shared mapper; so work around:
240240
Object root = MAPPER.readValue(a2q("{'a':3,'b':[1,2]}"),
@@ -251,7 +251,7 @@ public void testNestedUntypes() throws IOException
251251
}
252252

253253
@Test
254-
public void testUntypedWithCustomScalarDesers() throws IOException
254+
public void testUntypedWithCustomScalarDesers() throws Exception
255255
{
256256
SimpleModule m = new SimpleModule("test-module");
257257
m.addDeserializer(String.class, new UCStringDeserializer());
@@ -275,31 +275,51 @@ public void testUntypedWithCustomScalarDesers() throws IOException
275275

276276
// Test that exercises non-vanilla variant, with just one simple custom deserializer
277277
@Test
278-
public void testNonVanilla() throws IOException
278+
public void testNonVanilla() throws Exception
279279
{
280280
SimpleModule m = new SimpleModule("test-module");
281281
m.addDeserializer(String.class, new UCStringDeserializer());
282282
final ObjectMapper mapper = jsonMapperBuilder()
283283
.polymorphicTypeValidator(new NoCheckSubTypeValidator())
284284
.addModule(m)
285285
.build();
286+
ObjectReader r = mapper.readerFor(Object.class);
286287
// Also: since this is now non-vanilla variant, try more alternatives
287-
List<?> l = (List<?>) mapper.readValue("[ true, false, 7, 0.5, \"foo\"]", Object.class);
288-
assertEquals(5, l.size());
288+
List<?> l = (List<?>) r.readValue("[ true, false, 7, 0.5, \"foo\", null]");
289+
assertEquals(6, l.size());
289290
assertEquals(Boolean.TRUE, l.get(0));
290291
assertEquals(Boolean.FALSE, l.get(1));
291292
assertEquals(Integer.valueOf(7), l.get(2));
292293
assertEquals(Double.valueOf(0.5), l.get(3));
293294
assertEquals("FOO", l.get(4));
294-
295+
assertNull(l.get(5));
296+
297+
// And Maps
298+
Map<?,?> map = (Map<?,?>) r.readValue(a2q("{'a':0.25,'b':3,'c':true,'d':false}"));
299+
assertEquals(Map.of("a", 0.25, "b", 3, "c", true, "d", false), map);
300+
301+
// And Scalars too; regular and "updating" readers
302+
l = new ArrayList<>();
303+
assertEquals(Integer.valueOf(42), r.readValue("42"));
304+
assertEquals(Integer.valueOf(42), r.withValueToUpdate(l).readValue("42"));
305+
assertEquals(Double.valueOf(2.5), r.readValue("2.5"));
306+
assertEquals(Double.valueOf(2.5), r.withValueToUpdate(l).readValue("2.5"));
307+
assertEquals(true, r.readValue("true"));
308+
assertEquals(true, r.withValueToUpdate(l).readValue("true"));
309+
assertEquals(false, r.readValue("false"));
310+
assertEquals(false, r.withValueToUpdate(l).readValue("false"));
311+
assertNull(r.readValue("null"));
312+
assertSame(l, r.withValueToUpdate(l).readValue("null"));
313+
314+
// and minimal nesting
295315
l = (List<?>) mapper.readValue("[ {}, [] ]", Object.class);
296316
assertEquals(2, l.size());
297-
assertTrue(l.get(0) instanceof Map<?,?>);
298-
assertTrue(l.get(1) instanceof List<?>);
317+
assertEquals(Map.of(), l.get(0));
318+
assertEquals(List.of(), l.get(1));
299319
}
300320

301321
@Test
302-
public void testUntypedWithListDeser() throws IOException
322+
public void testUntypedWithListDeser() throws Exception
303323
{
304324
SimpleModule m = new SimpleModule("test-module");
305325
m.addDeserializer(List.class, new ListDeserializer());
@@ -317,7 +337,7 @@ public void testUntypedWithListDeser() throws IOException
317337
}
318338

319339
@Test
320-
public void testUntypedWithMapDeser() throws IOException
340+
public void testUntypedWithMapDeser() throws Exception
321341
{
322342
SimpleModule m = new SimpleModule("test-module");
323343
m.addDeserializer(Map.class, new YMapDeserializer());
@@ -333,7 +353,7 @@ public void testUntypedWithMapDeser() throws IOException
333353
}
334354

335355
@Test
336-
public void testNestedUntyped989() throws IOException
356+
public void testNestedUntyped989() throws Exception
337357
{
338358
DelegatingUntyped pojo;
339359
ObjectReader r = MAPPER.readerFor(DelegatingUntyped.class);
@@ -360,8 +380,10 @@ public void testUntypedWithJsonArrays() throws Exception
360380
ObjectMapper mapper = jsonMapperBuilder()
361381
.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)
362382
.build();
363-
ob = mapper.readValue("[1]", Object.class);
383+
ob = mapper.readValue("[1, false, true, 0.5, {}]", Object.class);
364384
assertEquals(Object[].class, ob.getClass());
385+
assertEquals(List.of(1, false, true, 0.5, Map.of()),
386+
Arrays.asList((Object[]) ob));
365387
}
366388

367389
@Test
@@ -489,7 +511,7 @@ public void testValueUpdateCustomUntyped() throws Exception
489511

490512
// Allow 'upgrade' of big integers into Long, BigInteger
491513
@Test
492-
public void testObjectSerializeWithLong() throws IOException
514+
public void testObjectSerializeWithLong() throws Exception
493515
{
494516
final ObjectMapper mapper = jsonMapperBuilder()
495517
.activateDefaultTyping(NoCheckSubTypeValidator.instance,
@@ -510,7 +532,7 @@ public void testObjectSerializeWithLong() throws IOException
510532
}
511533

512534
@Test
513-
public void testPolymorphicUntypedVanilla() throws IOException
535+
public void testPolymorphicUntypedVanilla() throws Exception
514536
{
515537
ObjectReader rDefault = jsonMapperBuilder()
516538
.polymorphicTypeValidator(new NoCheckSubTypeValidator())
@@ -554,7 +576,7 @@ public void testPolymorphicUntypedVanilla() throws IOException
554576
}
555577

556578
@Test
557-
public void testPolymorphicUntypedCustom() throws IOException
579+
public void testPolymorphicUntypedCustom() throws Exception
558580
{
559581
// register module just to override one deserializer, to prevent use of Vanilla deser
560582
SimpleModule m = new SimpleModule("test-module")

0 commit comments

Comments
 (0)