Skip to content

Commit

Permalink
Preserve the original component type in merging to the array (#4121)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurkom authored Sep 20, 2023
1 parent c524e6b commit 7d8a189
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonFormat;
Expand Down Expand Up @@ -258,8 +259,7 @@ public Object[] deserialize(JsonParser p, DeserializationContext ctxt,
return intoValue;
}
final int offset = intoValue.length;
Object[] result = new Object[offset + arr.length];
System.arraycopy(intoValue, 0, result, 0, offset);
Object[] result = Arrays.copyOf(intoValue, offset + arr.length);
System.arraycopy(arr, 0, result, offset, arr.length);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import com.fasterxml.jackson.databind.*;

import java.util.Date;

public class ArrayMergeTest extends BaseMapTest
{
static class MergedX<T>
Expand All @@ -19,7 +21,13 @@ static class MergedX<T>
public MergedX(T v) { value = v; }
protected MergedX() { }
}


static class Merged
{
@JsonMerge(OptBoolean.TRUE)
public Date[] value;
}

/*
/********************************************************
/* Test methods
Expand Down Expand Up @@ -57,6 +65,31 @@ public void testObjectArrayMerging() throws Exception
assertEquals("zap", result.value[2]);
}

public void testComponentTypeArrayMerging() throws Exception
{
Merged input = new Merged();
input.value = new Date[] {new Date(1000L)};
final JavaType type = MAPPER.getTypeFactory().constructType(new TypeReference<Merged>() {});
Merged result = MAPPER.readerFor(type)
.withValueToUpdate(input)
.readValue(a2q("{'value':[2000]}"));
assertSame(input, result);
assertEquals(2, result.value.length);
assertEquals(1000L, result.value[0].getTime());
assertEquals(2000L, result.value[1].getTime());

// and with one trick
result = MAPPER.readerFor(type)
.with(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.withValueToUpdate(input)
.readValue(a2q("{'value':3000}"));
assertSame(input, result);
assertEquals(3, result.value.length);
assertEquals(1000L, result.value[0].getTime());
assertEquals(2000L, result.value[1].getTime());
assertEquals(3000L, result.value[2].getTime());
}

public void testStringArrayMerging() throws Exception
{
MergedX<String[]> input = new MergedX<String[]>(new String[] { "foo" });
Expand Down

0 comments on commit 7d8a189

Please sign in to comment.