Skip to content

Commit 7fe2d4f

Browse files
committed
Merge branch '2.7' into 2.8
2 parents 75b7049 + d44600d commit 7fe2d4f

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Project: jackson-databind
147147
#1456: `TypeFactory` type resolution broken in 2.7 for generic types
148148
when using `constructType` with context
149149
(reported by Dmitry S)
150+
#1476: Wrong constructor picked up when deserializing object
151+
(reported by laurentgo@github)
150152

151153
2.7.8 (26-Sep-2016)
152154

src/main/java/com/fasterxml/jackson/databind/deser/impl/CreatorCollector.java

+38-32
Original file line numberDiff line numberDiff line change
@@ -157,37 +157,40 @@ public void addBooleanCreator(AnnotatedWithParams creator,
157157
public void addDelegatingCreator(AnnotatedWithParams creator,
158158
boolean explicit, SettableBeanProperty[] injectables) {
159159
if (creator.getParameterType(0).isCollectionLikeType()) {
160-
verifyNonDup(creator, C_ARRAY_DELEGATE, explicit);
161-
_arrayDelegateArgs = injectables;
160+
if (verifyNonDup(creator, C_ARRAY_DELEGATE, explicit)) {
161+
_arrayDelegateArgs = injectables;
162+
}
162163
} else {
163-
verifyNonDup(creator, C_DELEGATE, explicit);
164-
_delegateArgs = injectables;
164+
if (verifyNonDup(creator, C_DELEGATE, explicit)) {
165+
_delegateArgs = injectables;
166+
}
165167
}
166168
}
167169

168170
public void addPropertyCreator(AnnotatedWithParams creator,
169171
boolean explicit, SettableBeanProperty[] properties) {
170-
verifyNonDup(creator, C_PROPS, explicit);
171-
// Better ensure we have no duplicate names either...
172-
if (properties.length > 1) {
173-
HashMap<String, Integer> names = new HashMap<String, Integer>();
174-
for (int i = 0, len = properties.length; i < len; ++i) {
175-
String name = properties[i].getName();
176-
// Need to consider Injectables, which may not have
177-
// a name at all, and need to be skipped
178-
if (name.length() == 0
179-
&& properties[i].getInjectableValueId() != null) {
180-
continue;
181-
}
182-
Integer old = names.put(name, Integer.valueOf(i));
183-
if (old != null) {
184-
throw new IllegalArgumentException(String.format(
185-
"Duplicate creator property \"%s\" (index %s vs %d)",
186-
name, old, i));
172+
if (verifyNonDup(creator, C_PROPS, explicit)) {
173+
// Better ensure we have no duplicate names either...
174+
if (properties.length > 1) {
175+
HashMap<String, Integer> names = new HashMap<String, Integer>();
176+
for (int i = 0, len = properties.length; i < len; ++i) {
177+
String name = properties[i].getName();
178+
// Need to consider Injectables, which may not have
179+
// a name at all, and need to be skipped
180+
if (name.length() == 0
181+
&& properties[i].getInjectableValueId() != null) {
182+
continue;
183+
}
184+
Integer old = names.put(name, Integer.valueOf(i));
185+
if (old != null) {
186+
throw new IllegalArgumentException(String.format(
187+
"Duplicate creator property \"%s\" (index %s vs %d)",
188+
name, old, i));
189+
}
187190
}
188191
}
192+
_propertyBasedArgs = properties;
189193
}
190-
_propertyBasedArgs = properties;
191194
}
192195

193196
public void addIncompeteParameter(AnnotatedParameter parameter) {
@@ -292,20 +295,21 @@ private <T extends AnnotatedMember> T _fixAccess(T member) {
292295
return member;
293296
}
294297

295-
protected void verifyNonDup(AnnotatedWithParams newOne, int typeIndex,
296-
boolean explicit) {
298+
/**
299+
* @return True if specified Creator is to be used
300+
*/
301+
protected boolean verifyNonDup(AnnotatedWithParams newOne, int typeIndex, boolean explicit)
302+
{
297303
final int mask = (1 << typeIndex);
298304
_hasNonDefaultCreator = true;
299305
AnnotatedWithParams oldOne = _creators[typeIndex];
300306
// already had an explicitly marked one?
301307
if (oldOne != null) {
302308
boolean verify;
303-
304-
if ((_explicitCreators & mask) != 0) { // already had explicitly
305-
// annotated, leave as-is
309+
if ((_explicitCreators & mask) != 0) { // already had explicitly annotated, leave as-is
306310
// but skip, if new one not annotated
307311
if (!explicit) {
308-
return;
312+
return false;
309313
}
310314
// both explicit: verify
311315
verify = true;
@@ -327,7 +331,7 @@ protected void verifyNonDup(AnnotatedWithParams newOne, int typeIndex,
327331
// other implicit
328332
// creator (`fromString()`)
329333
if (_isEnumValueOf(newOne)) {
330-
return; // ignore
334+
return false; // ignore
331335
}
332336
if (_isEnumValueOf(oldOne)) {
333337
;
@@ -343,7 +347,7 @@ protected void verifyNonDup(AnnotatedWithParams newOne, int typeIndex,
343347
// otherwise, which one to choose?
344348
else if (newType.isAssignableFrom(oldType)) {
345349
// new type more generic, use old
346-
return;
350+
return false;
347351
}
348352
// new type more specific, use it
349353
}
@@ -352,6 +356,7 @@ else if (newType.isAssignableFrom(oldType)) {
352356
_explicitCreators |= mask;
353357
}
354358
_creators[typeIndex] = _fixAccess(newOne);
359+
return true;
355360
}
356361

357362
/**
@@ -365,8 +370,9 @@ protected boolean _isEnumValueOf(AnnotatedWithParams creator) {
365370
}
366371

367372
/*
368-
* /********************************************************** /* Helper
369-
* class(es) /**********************************************************
373+
/**********************************************************
374+
/* Helper class(es)
375+
/**********************************************************
370376
*/
371377

372378
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.databind.creators;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
public class Creator1476Test extends BaseMapTest
8+
{
9+
static final class SimplePojo {
10+
private final int intField;
11+
private final String stringField;
12+
13+
public SimplePojo(@JsonProperty("intField") int intField) {
14+
this(intField, "empty");
15+
}
16+
17+
public SimplePojo(@JsonProperty("stringField") String stringField) {
18+
this(-1, stringField);
19+
}
20+
21+
@JsonCreator
22+
public SimplePojo(@JsonProperty("intField") int intField, @JsonProperty("stringField") String stringField) {
23+
this.intField = intField;
24+
this.stringField = stringField;
25+
}
26+
27+
public int getIntField() {
28+
return intField;
29+
}
30+
31+
public String getStringField() {
32+
return stringField;
33+
}
34+
}
35+
36+
public void testConstructorChoice() throws Exception {
37+
ObjectMapper mapper = new ObjectMapper();
38+
SimplePojo pojo = mapper.readValue("{ \"intField\": 1, \"stringField\": \"foo\" }", SimplePojo.class);
39+
40+
assertEquals(1, pojo.getIntField());
41+
assertEquals("foo", pojo.getStringField());
42+
}
43+
}

0 commit comments

Comments
 (0)