Skip to content

Commit

Permalink
More work for #1498: now the original case finally passes
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 18, 2020
1 parent c356b4d commit 151a6b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.atomic.AtomicReference;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JacksonInject.Value;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;

import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -475,26 +476,37 @@ protected void _addImplicitConstructorCreators(DeserializationContext ctxt,
CreatorCollectionState ccState, List<CreatorCandidate> ctorCandidates)
throws JsonMappingException
{
final DeserializationConfig config = ctxt.getConfig();
final BeanDescription beanDesc = ccState.beanDesc;
final CreatorCollector creators = ccState.creators;
final AnnotationIntrospector intr = ccState.annotationIntrospector();
final VisibilityChecker<?> vchecker = ccState.vchecker;
List<AnnotatedWithParams> implicitCtors = null;
final boolean preferPropsBased = config.getConstructorDetector().singleArgCreatorDefaultsToProperties();

for (CreatorCandidate candidate : ctorCandidates) {
final int argCount = candidate.paramCount();
final AnnotatedWithParams ctor = candidate.creator();

// some single-arg factory methods (String, number) are auto-detected
if (argCount == 1) {
BeanPropertyDefinition propDef = candidate.propertyDef(0);
boolean useProps = _checkIfCreatorPropertyBased(intr, ctor, propDef);
final BeanPropertyDefinition propDef = candidate.propertyDef(0);
final boolean useProps = preferPropsBased || _checkIfCreatorPropertyBased(intr, ctor, propDef);

if (useProps) {
SettableBeanProperty[] properties = new SettableBeanProperty[1];
final JacksonInject.Value injection = candidate.injection(0);

// 18-Sep-2020, tatu: [databind#1498] looks like implicit name not linked
// unless annotation found, so try again from here
PropertyName name = candidate.paramName(0);
if (name == null) {
name = candidate.findImplicitParamName(0);
if ((name == null) && (injection == null)) {
continue;
}
}
properties[0] = constructCreatorProperty(ctxt, beanDesc, name, 0,
candidate.parameter(0), candidate.injection(0));
candidate.parameter(0), injection);
creators.addPropertyCreator(ctor, false, properties);
} else {
/*boolean added = */ _handleSingleArgumentCreator(creators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static class SingleArgNotAnnotated {

// SingleArgNotAnnotated() { throw new Error("Should not be used"); }

public SingleArgNotAnnotated(int value) {
public SingleArgNotAnnotated(@ImplicitName("value") @com.fasterxml.jackson.annotation.JsonSetter int value) {
v = value;
}
}
Expand All @@ -54,6 +54,15 @@ public SingleArgNoMode(@ImplicitName("value") int value) {
}
}

static class SingleArg1498 {
final int _bar;

// note: annotation only to inject "implicit name" without needing parameter-names module
SingleArg1498(@ImplicitName("bar") int b) {
_bar = b;
}
}

private final ObjectMapper MAPPER_PROPS = mapperFor(ConstructorDetector.USE_PROPERTIES_BASED);
private final ObjectMapper MAPPER_DELEGATING = mapperFor(ConstructorDetector.USE_DELEGATING);
private final ObjectMapper MAPPER_EXPLICIT = mapperFor(ConstructorDetector.EXPLICIT_ONLY);
Expand All @@ -64,14 +73,12 @@ public SingleArgNoMode(@ImplicitName("value") int value) {
/**********************************************************************
*/

/*
public void test1ArgDefaultsToPropertiesNonAnnotated() throws Exception
{
SingleArgNotAnnotated value = MAPPER_PROPS.readValue(a2q("{'value' : 137 }"),
SingleArgNotAnnotated.class);
assertEquals(137, value.v);
}
*/

public void test1ArgDefaultsToPropertiesNoMode() throws Exception
{
Expand All @@ -81,6 +88,15 @@ public void test1ArgDefaultsToPropertiesNoMode() throws Exception
assertEquals(137, value.v);
}

// And specific test for original [databind#1498]
public void test1ArgDefaultsToPropertiesIssue1498() throws Exception
{
// and similarly for mode-less
SingleArg1498 value = MAPPER_PROPS.readValue(a2q("{'bar' : 404 }"),
SingleArg1498.class);
assertEquals(404, value._bar);
}

/*
/**********************************************************************
/* Test methods, selecting from 1-arg constructors, delegating
Expand Down Expand Up @@ -148,12 +164,6 @@ public void test1ArgFailsNoMode() throws Exception
}
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

/*
/**********************************************************************
/* Helper methods
Expand Down

0 comments on commit 151a6b6

Please sign in to comment.