Skip to content

Commit 7d8a189

Browse files
authored
Preserve the original component type in merging to the array (#4121)
1 parent c524e6b commit 7d8a189

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/std/ObjectArrayDeserializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.lang.reflect.Array;
5+
import java.util.Arrays;
56
import java.util.Objects;
67

78
import com.fasterxml.jackson.annotation.JsonFormat;
@@ -258,8 +259,7 @@ public Object[] deserialize(JsonParser p, DeserializationContext ctxt,
258259
return intoValue;
259260
}
260261
final int offset = intoValue.length;
261-
Object[] result = new Object[offset + arr.length];
262-
System.arraycopy(intoValue, 0, result, 0, offset);
262+
Object[] result = Arrays.copyOf(intoValue, offset + arr.length);
263263
System.arraycopy(arr, 0, result, offset, arr.length);
264264
return result;
265265
}

src/test/java/com/fasterxml/jackson/databind/deser/merge/ArrayMergeTest.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import com.fasterxml.jackson.databind.*;
1111

12+
import java.util.Date;
13+
1214
public class ArrayMergeTest extends BaseMapTest
1315
{
1416
static class MergedX<T>
@@ -19,7 +21,13 @@ static class MergedX<T>
1921
public MergedX(T v) { value = v; }
2022
protected MergedX() { }
2123
}
22-
24+
25+
static class Merged
26+
{
27+
@JsonMerge(OptBoolean.TRUE)
28+
public Date[] value;
29+
}
30+
2331
/*
2432
/********************************************************
2533
/* Test methods
@@ -57,6 +65,31 @@ public void testObjectArrayMerging() throws Exception
5765
assertEquals("zap", result.value[2]);
5866
}
5967

68+
public void testComponentTypeArrayMerging() throws Exception
69+
{
70+
Merged input = new Merged();
71+
input.value = new Date[] {new Date(1000L)};
72+
final JavaType type = MAPPER.getTypeFactory().constructType(new TypeReference<Merged>() {});
73+
Merged result = MAPPER.readerFor(type)
74+
.withValueToUpdate(input)
75+
.readValue(a2q("{'value':[2000]}"));
76+
assertSame(input, result);
77+
assertEquals(2, result.value.length);
78+
assertEquals(1000L, result.value[0].getTime());
79+
assertEquals(2000L, result.value[1].getTime());
80+
81+
// and with one trick
82+
result = MAPPER.readerFor(type)
83+
.with(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
84+
.withValueToUpdate(input)
85+
.readValue(a2q("{'value':3000}"));
86+
assertSame(input, result);
87+
assertEquals(3, result.value.length);
88+
assertEquals(1000L, result.value[0].getTime());
89+
assertEquals(2000L, result.value[1].getTime());
90+
assertEquals(3000L, result.value[2].getTime());
91+
}
92+
6093
public void testStringArrayMerging() throws Exception
6194
{
6295
MergedX<String[]> input = new MergedX<String[]>(new String[] { "foo" });

0 commit comments

Comments
 (0)