Skip to content

Commit 2c9fa0e

Browse files
committed
Minor tweak for #761 fix
1 parent 5a73da8 commit 2c9fa0e

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ public JsonDeserializer<?> buildBuilderBased(JavaType valueType,
374374
}
375375
// also: type of the method must be compatible
376376
Class<?> rawBuildType = _buildMethod.getRawReturnType();
377-
if (!rawBuildType.isAssignableFrom(valueType.getRawClass())) {
377+
Class<?> rawValueType = valueType.getRawClass();
378+
if ((rawBuildType != rawValueType)
379+
&& !rawBuildType.isAssignableFrom(rawValueType)
380+
&& !rawValueType.isAssignableFrom(rawBuildType)) {
378381
throw new IllegalArgumentException("Build method '"+_buildMethod.getFullName()
379382
+" has bad return type ("+rawBuildType.getName()
380383
+"), not compatible with POJO type ("+valueType.getRawClass().getName()+")");

src/test/java/com/fasterxml/jackson/databind/creators/TestBuilderSimple.java

+67-18
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,18 @@ public CreatorValue build() {
159159
}
160160
}
161161

162+
// for [databind#761]
163+
162164
@JsonDeserialize(builder=ValueInterfaceBuilder.class)
163165
interface ValueInterface {
164166
int getX();
165167
}
166168

169+
@JsonDeserialize(builder=ValueInterface2Builder.class)
170+
interface ValueInterface2 {
171+
int getX();
172+
}
173+
167174
static class ValueInterfaceImpl implements ValueInterface
168175
{
169176
final int _x;
@@ -177,6 +184,21 @@ public int getX() {
177184
return _x;
178185
}
179186
}
187+
188+
static class ValueInterface2Impl implements ValueInterface2
189+
{
190+
final int _x;
191+
192+
protected ValueInterface2Impl(int x) {
193+
_x = x+1;
194+
}
195+
196+
@Override
197+
public int getX() {
198+
return _x;
199+
}
200+
}
201+
180202
static class ValueInterfaceBuilder
181203
{
182204
public int x;
@@ -190,10 +212,27 @@ public ValueInterface build() {
190212
return new ValueInterfaceImpl(x);
191213
}
192214
}
215+
216+
static class ValueInterface2Builder
217+
{
218+
public int x;
219+
220+
public ValueInterface2Builder withX(int x0) {
221+
this.x = x0;
222+
return this;
223+
}
224+
225+
// should also be ok: more specific type
226+
public ValueInterface2Impl build() {
227+
return new ValueInterface2Impl(x);
228+
}
229+
}
230+
231+
// for [databind#761]
193232
@JsonDeserialize(builder = ValueBuilderWrongBuildType.class)
194233
static class ValueClassWrongBuildType {
195-
196234
}
235+
197236
static class ValueBuilderWrongBuildType
198237
{
199238
public int x;
@@ -207,7 +246,8 @@ public ValueClassXY build() {
207246
return null;
208247
}
209248
}
210-
/*
249+
250+
/*
211251
/**********************************************************
212252
/* Unit tests
213253
/**********************************************************
@@ -217,25 +257,25 @@ public ValueClassXY build() {
217257

218258
public void testSimple() throws Exception
219259
{
220-
String json = "{\"x\":1,\"y\":2}";
221-
Object o = mapper.readValue(json, ValueClassXY.class);
222-
assertNotNull(o);
223-
assertSame(ValueClassXY.class, o.getClass());
224-
ValueClassXY value = (ValueClassXY) o;
225-
// note: ctor adds one to both values
226-
assertEquals(value._x, 2);
227-
assertEquals(value._y, 3);
260+
String json = "{\"x\":1,\"y\":2}";
261+
Object o = mapper.readValue(json, ValueClassXY.class);
262+
assertNotNull(o);
263+
assertSame(ValueClassXY.class, o.getClass());
264+
ValueClassXY value = (ValueClassXY) o;
265+
// note: ctor adds one to both values
266+
assertEquals(value._x, 2);
267+
assertEquals(value._y, 3);
228268
}
229269

230270
public void testMultiAccess() throws Exception
231271
{
232-
String json = "{\"c\":3,\"a\":2,\"b\":-9}";
233-
ValueClassABC value = mapper.readValue(json, ValueClassABC.class);
234-
assertNotNull(value);
235-
// note: ctor adds one to both values
236-
assertEquals(value.a, 2);
237-
assertEquals(value.b, -9);
238-
assertEquals(value.c, 3);
272+
String json = "{\"c\":3,\"a\":2,\"b\":-9}";
273+
ValueClassABC value = mapper.readValue(json, ValueClassABC.class);
274+
assertNotNull(value);
275+
// note: ctor adds one to both values
276+
assertEquals(value.a, 2);
277+
assertEquals(value.b, -9);
278+
assertEquals(value.c, 3);
239279
}
240280

241281
// test for Immutable builder, to ensure return value is used
@@ -264,13 +304,22 @@ public void testWithCreator() throws Exception
264304
assertEquals(3, value.c);
265305
}
266306

267-
public void testBuilderMethodReturnInterface() throws Exception
307+
// for [databind#761]
308+
309+
public void testBuilderMethodReturnMoreGeneral() throws Exception
268310
{
269311
final String json = "{\"x\":1}";
270312
ValueInterface value = mapper.readValue(json, ValueInterface.class);
271313
assertEquals(2, value.getX());
272314
}
273315

316+
public void testBuilderMethodReturnMoreSpecific() throws Exception
317+
{
318+
final String json = "{\"x\":1}";
319+
ValueInterface2 value = mapper.readValue(json, ValueInterface2.class);
320+
assertEquals(2, value.getX());
321+
}
322+
274323
public void testBuilderMethodReturnInvalidType() throws Exception
275324
{
276325
final String json = "{\"x\":1}";

0 commit comments

Comments
 (0)