Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the original component type in merging to an array #4121

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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