Skip to content

Commit

Permalink
Fix #1476
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 9, 2017
1 parent 002a9a3 commit d44600d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Expand Up @@ -18,6 +18,8 @@ Project: jackson-databind
#1456: `TypeFactory` type resolution broken in 2.7 for generic types
when using `constructType` with context
(reported by Dmitry S)
#1476: Wrong constructor picked up when deserializing object
(reported by laurentgo@github)

2.7.8 (26-Sep-2016)

Expand Down
Expand Up @@ -162,36 +162,39 @@ public void addDelegatingCreator(AnnotatedWithParams creator, boolean explicit,
SettableBeanProperty[] injectables)
{
if (creator.getParameterType(0).isCollectionLikeType()) {
verifyNonDup(creator, C_ARRAY_DELEGATE, explicit);
_arrayDelegateArgs = injectables;
if (verifyNonDup(creator, C_ARRAY_DELEGATE, explicit)) {
_arrayDelegateArgs = injectables;
}
} else {
verifyNonDup(creator, C_DELEGATE, explicit);
_delegateArgs = injectables;
if (verifyNonDup(creator, C_DELEGATE, explicit)) {
_delegateArgs = injectables;
}
}
}

public void addPropertyCreator(AnnotatedWithParams creator, boolean explicit,
SettableBeanProperty[] properties)
{
verifyNonDup(creator, C_PROPS, explicit);
// Better ensure we have no duplicate names either...
if (properties.length > 1) {
HashMap<String,Integer> names = new HashMap<String,Integer>();
for (int i = 0, len = properties.length; i < len; ++i) {
String name = properties[i].getName();
/* [Issue-13]: Need to consider Injectables, which may not have
* a name at all, and need to be skipped
*/
if (name.length() == 0 && properties[i].getInjectableValueId() != null) {
continue;
}
Integer old = names.put(name, Integer.valueOf(i));
if (old != null) {
throw new IllegalArgumentException("Duplicate creator property \""+name+"\" (index "+old+" vs "+i+")");
if (verifyNonDup(creator, C_PROPS, explicit)) {
// Better ensure we have no duplicate names either...
if (properties.length > 1) {
HashMap<String,Integer> names = new HashMap<String,Integer>();
for (int i = 0, len = properties.length; i < len; ++i) {
String name = properties[i].getName();
/* [Issue-13]: Need to consider Injectables, which may not have
* a name at all, and need to be skipped
*/
if (name.length() == 0 && properties[i].getInjectableValueId() != null) {
continue;
}
Integer old = names.put(name, Integer.valueOf(i));
if (old != null) {
throw new IllegalArgumentException("Duplicate creator property \""+name+"\" (index "+old+" vs "+i+")");
}
}
}
_propertyBasedArgs = properties;
}
_propertyBasedArgs = properties;
}

public void addIncompeteParameter(AnnotatedParameter parameter) {
Expand Down Expand Up @@ -293,19 +296,21 @@ private <T extends AnnotatedMember> T _fixAccess(T member)
return member;
}

protected void verifyNonDup(AnnotatedWithParams newOne, int typeIndex, boolean explicit)
/**
* @return True if specified Creator is to be used
*/
protected boolean verifyNonDup(AnnotatedWithParams newOne, int typeIndex, boolean explicit)
{
final int mask = (1 << typeIndex);
_hasNonDefaultCreator = true;
AnnotatedWithParams oldOne = _creators[typeIndex];
// already had an explicitly marked one?
if (oldOne != null) {
boolean verify;

if ((_explicitCreators & mask) != 0) { // already had explicitly annotated, leave as-is
// but skip, if new one not annotated
if (!explicit) {
return;
return false;
}
// both explicit: verify
verify = true;
Expand All @@ -327,7 +332,7 @@ protected void verifyNonDup(AnnotatedWithParams newOne, int typeIndex, boolean e
// otherwise, which one to choose?
if (newType.isAssignableFrom(oldType)) {
// new type more generic, use old
return;
return false;
}
// new type more specific, use it
}
Expand All @@ -336,6 +341,7 @@ protected void verifyNonDup(AnnotatedWithParams newOne, int typeIndex, boolean e
_explicitCreators |= mask;
}
_creators[typeIndex] = _fixAccess(newOne);
return true;
}

/*
Expand Down
@@ -0,0 +1,43 @@
package com.fasterxml.jackson.databind.creators;

import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;

public class Creator1476Test extends BaseMapTest
{
static final class SimplePojo {
private final int intField;
private final String stringField;

public SimplePojo(@JsonProperty("intField") int intField) {
this(intField, "empty");
}

public SimplePojo(@JsonProperty("stringField") String stringField) {
this(-1, stringField);
}

@JsonCreator
public SimplePojo(@JsonProperty("intField") int intField, @JsonProperty("stringField") String stringField) {
this.intField = intField;
this.stringField = stringField;
}

public int getIntField() {
return intField;
}

public String getStringField() {
return stringField;
}
}

public void testConstructorChoice() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimplePojo pojo = mapper.readValue("{ \"intField\": 1, \"stringField\": \"foo\" }", SimplePojo.class);

assertEquals(1, pojo.getIntField());
assertEquals("foo", pojo.getStringField());
}
}

0 comments on commit d44600d

Please sign in to comment.