Skip to content

Commit b106a67

Browse files
committed
Regression test for issue 1711 of jackson-databind (easy repro here, harder over there)
1 parent 854b225 commit b106a67

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/mapper/SmileMapperTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
import org.junit.Assert;
1010

11+
import com.fasterxml.jackson.annotation.JsonCreator;
12+
import com.fasterxml.jackson.annotation.JsonValue;
1113
import com.fasterxml.jackson.core.*;
14+
import com.fasterxml.jackson.core.type.TypeReference;
1215
import com.fasterxml.jackson.databind.JsonNode;
1316
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1418
import com.fasterxml.jackson.databind.node.ArrayNode;
1519
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
1620
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
@@ -24,12 +28,31 @@ public BytesBean() { }
2428
public BytesBean(byte[] b) { bytes = b; }
2529
}
2630

31+
// [dataformats-binary#1711]
32+
static class ByteWrapper1711 {
33+
private final byte[] val;
34+
35+
@JsonCreator // (mode=JsonCreator.Mode.DELEGATING)
36+
public ByteWrapper1711(byte[] val) {
37+
this.val = val;
38+
}
39+
40+
@JsonValue public byte[] getValue() { return val;}
41+
}
42+
43+
static class Wrapper<V> {
44+
public V value;
45+
46+
protected Wrapper() { }
47+
public Wrapper(V v) { value = v; }
48+
}
49+
2750
/*
2851
/**********************************************************
2952
/* Test methods
3053
/**********************************************************
3154
*/
32-
55+
3356
private final ObjectMapper MAPPER = smileMapper();
3457

3558
public void testBinary() throws IOException
@@ -42,6 +65,29 @@ public void testBinary() throws IOException
4265
Assert.assertArrayEquals(input, result.bytes);
4366
}
4467

68+
// [dataformats-binary#1711]
69+
public void testWrappedBinary() throws IOException
70+
{
71+
byte[] bytes = {1, 2, 3, 4, 5};
72+
byte[] smile = MAPPER.writeValueAsBytes(new ByteWrapper1711(bytes));
73+
ByteWrapper1711 read = MAPPER.readValue(smile, ByteWrapper1711.class);
74+
if (!Arrays.equals(bytes, read.val)) {
75+
throw new IllegalStateException("Arrays not equal");
76+
}
77+
78+
// also, verify exception we get if there's no match...
79+
smile = MAPPER.writeValueAsBytes(new Wrapper<>(new ByteWrapper1711(bytes)));
80+
try {
81+
Wrapper<?> ob = MAPPER.readValue(smile, new TypeReference<Wrapper<BytesBean>>() { });
82+
Object val = ob.value;
83+
fail("Should not pass, Wrapper value should be `BytesBean`, got: "+val.getClass().getName());
84+
} catch (MismatchedInputException e) {
85+
verifyException(e, "Cannot deserialize value of type");
86+
verifyException(e, BytesBean.class.getName());
87+
verifyException(e, "incompatible types");
88+
}
89+
}
90+
4591
// UUIDs should be written as binary (starting with 2.3)
4692
public void testUUIDs() throws IOException
4793
{

0 commit comments

Comments
 (0)