Skip to content

Commit 24274b2

Browse files
committed
Merge branch '2.11' into 2.12
2 parents b30e8fd + f4c8427 commit 24274b2

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

+18
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,25 @@ protected BeanDeserializerBase(BeanDeserializerBuilder builder,
212212
_injectables = (injectables == null || injectables.isEmpty()) ? null
213213
: injectables.toArray(new ValueInjector[injectables.size()]);
214214
_objectIdReader = builder.getObjectIdReader();
215+
216+
// If you check for canCreateFromInt() below which appears to be the equivalent
217+
// of the existing canCreateUsingArrayDelegate() it also causes the included
218+
// test testPOJOWithPrimitiveCreatorFromObjectRepresentation() to break in the
219+
// same way as the test testPOJOWithArrayCreatorFromObjectRepresentation() which
220+
// I assert is failing because of the call to canCreateUsingArrayDelegate() below.
221+
//
222+
// Removing the call to canCreateUsingArrayDelegate() below allows the test
223+
// testPOJOWithArrayCreatorFromObjectRepresentation() to pass and interestingly
224+
// all existing tests continue to pass. I somehow doubt that it's that simple.
225+
//
226+
// Specifically, it seems to me that the vanilla-ness of the deserialization can
227+
// only be determined once the structure of the incoming JSON can be compared to
228+
// what creator methods exist. For example, if there is an array delegate and we
229+
// are asked to deserialize from an array then it's not vanilla. However, if we
230+
// are asked to deserialize from an object then it's vanilla (subject to no other
231+
// non-vanilla-ness being present).
215232
_nonStandardCreation = (_unwrappedPropertyHandler != null)
233+
//|| _valueInstantiator.canCreateFromInt()
216234
|| _valueInstantiator.canCreateUsingDelegate()
217235
|| _valueInstantiator.canCreateUsingArrayDelegate() // new in 2.7
218236
|| _valueInstantiator.canCreateFromObjectWith()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.databind.BaseMapTest;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
7+
8+
import java.util.List;
9+
10+
public class BuilderDeserializationTest2486
11+
extends BaseMapTest
12+
{
13+
@JsonDeserialize(builder = MyPOJOWithArrayCreator.Builder.class)
14+
public static class MyPOJOWithArrayCreator {
15+
private final int index;
16+
17+
private MyPOJOWithArrayCreator(int i) {
18+
index = i;
19+
}
20+
21+
public int getIndex() {
22+
return index;
23+
}
24+
25+
public static class Builder {
26+
private int index;
27+
28+
public Builder() {
29+
// Default constructor
30+
}
31+
32+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
33+
public Builder(final List<Object> jsonArray) {
34+
withIndex((int) jsonArray.get(0));
35+
}
36+
37+
public Builder withIndex(int i) {
38+
index = i;
39+
return this;
40+
}
41+
42+
public MyPOJOWithArrayCreator build() {
43+
return new MyPOJOWithArrayCreator(index);
44+
}
45+
}
46+
}
47+
48+
@JsonDeserialize(builder = MyPOJOWithPrimitiveCreator.Builder.class)
49+
public static class MyPOJOWithPrimitiveCreator {
50+
private final int index;
51+
52+
private MyPOJOWithPrimitiveCreator(int i) {
53+
index = i;
54+
}
55+
56+
public int getIndex() {
57+
return index;
58+
}
59+
60+
public static class Builder {
61+
private int index;
62+
63+
public Builder() {
64+
// Default constructor
65+
}
66+
67+
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
68+
public Builder(final int i) {
69+
withIndex(i);
70+
}
71+
72+
public Builder withIndex(int i) {
73+
index = i;
74+
return this;
75+
}
76+
77+
public MyPOJOWithPrimitiveCreator build() {
78+
return new MyPOJOWithPrimitiveCreator(index);
79+
}
80+
}
81+
}
82+
83+
private final ObjectMapper MAPPER = newJsonMapper();
84+
85+
// This test passes when the array based @JsonCreator is removed from the
86+
// MyPOJOWithArrayCreator.Builder implementation. The presence of the creator
87+
// in the case of arrays breaks deserialize from an object.
88+
//
89+
// Compare that to the analogous tests for MyPOJOWithPrimitiveCreator which
90+
// pass in both cases.
91+
//
92+
// I left some notes in BeanDeserializerBase as to behavior.
93+
public void testPOJOWithArrayCreatorFromObjectRepresentation() throws Exception {
94+
final String json = aposToQuotes("{ 'index': 123 }");
95+
final MyPOJOWithArrayCreator deserialized = MAPPER.readValue(json, MyPOJOWithArrayCreator.class);
96+
assertEquals(123, deserialized.getIndex());
97+
}
98+
99+
public void testPOJOWithArrayCreatorFromArrayRepresentation() throws Exception {
100+
final String json = "[123]";
101+
final MyPOJOWithArrayCreator deserialized = MAPPER.readValue(json, MyPOJOWithArrayCreator.class);
102+
assertEquals(123, deserialized.getIndex());
103+
}
104+
105+
public void testPOJOWithPrimitiveCreatorFromObjectRepresentation() throws Exception {
106+
final String json = aposToQuotes("{ 'index': 123 }");
107+
final MyPOJOWithPrimitiveCreator deserialized = MAPPER.readValue(json, MyPOJOWithPrimitiveCreator.class);
108+
assertEquals(123, deserialized.getIndex());
109+
}
110+
111+
public void testPOJOWithPrimitiveCreatorFromPrimitiveRepresentation() throws Exception {
112+
final String json ="123";
113+
final MyPOJOWithPrimitiveCreator deserialized = MAPPER.readValue(json, MyPOJOWithPrimitiveCreator.class);
114+
assertEquals(123, deserialized.getIndex());
115+
}
116+
}

0 commit comments

Comments
 (0)