Skip to content

Commit 151a6b6

Browse files
committed
More work for #1498: now the original case finally passes
1 parent c356b4d commit 151a6b6

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.concurrent.atomic.AtomicReference;
99

1010
import com.fasterxml.jackson.annotation.*;
11+
import com.fasterxml.jackson.annotation.JacksonInject.Value;
1112
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
1213

1314
import com.fasterxml.jackson.core.JsonParser;
@@ -475,26 +476,37 @@ protected void _addImplicitConstructorCreators(DeserializationContext ctxt,
475476
CreatorCollectionState ccState, List<CreatorCandidate> ctorCandidates)
476477
throws JsonMappingException
477478
{
479+
final DeserializationConfig config = ctxt.getConfig();
478480
final BeanDescription beanDesc = ccState.beanDesc;
479481
final CreatorCollector creators = ccState.creators;
480482
final AnnotationIntrospector intr = ccState.annotationIntrospector();
481483
final VisibilityChecker<?> vchecker = ccState.vchecker;
482484
List<AnnotatedWithParams> implicitCtors = null;
485+
final boolean preferPropsBased = config.getConstructorDetector().singleArgCreatorDefaultsToProperties();
483486

484487
for (CreatorCandidate candidate : ctorCandidates) {
485488
final int argCount = candidate.paramCount();
486489
final AnnotatedWithParams ctor = candidate.creator();
487-
488490
// some single-arg factory methods (String, number) are auto-detected
489491
if (argCount == 1) {
490-
BeanPropertyDefinition propDef = candidate.propertyDef(0);
491-
boolean useProps = _checkIfCreatorPropertyBased(intr, ctor, propDef);
492+
final BeanPropertyDefinition propDef = candidate.propertyDef(0);
493+
final boolean useProps = preferPropsBased || _checkIfCreatorPropertyBased(intr, ctor, propDef);
492494

493495
if (useProps) {
494496
SettableBeanProperty[] properties = new SettableBeanProperty[1];
497+
final JacksonInject.Value injection = candidate.injection(0);
498+
499+
// 18-Sep-2020, tatu: [databind#1498] looks like implicit name not linked
500+
// unless annotation found, so try again from here
495501
PropertyName name = candidate.paramName(0);
502+
if (name == null) {
503+
name = candidate.findImplicitParamName(0);
504+
if ((name == null) && (injection == null)) {
505+
continue;
506+
}
507+
}
496508
properties[0] = constructCreatorProperty(ctxt, beanDesc, name, 0,
497-
candidate.parameter(0), candidate.injection(0));
509+
candidate.parameter(0), injection);
498510
creators.addPropertyCreator(ctor, false, properties);
499511
} else {
500512
/*boolean added = */ _handleSingleArgumentCreator(creators,

src/test/java/com/fasterxml/jackson/databind/deser/creators/ConstructorDetector1498Test.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static class SingleArgNotAnnotated {
3838

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

41-
public SingleArgNotAnnotated(int value) {
41+
public SingleArgNotAnnotated(@ImplicitName("value") @com.fasterxml.jackson.annotation.JsonSetter int value) {
4242
v = value;
4343
}
4444
}
@@ -54,6 +54,15 @@ public SingleArgNoMode(@ImplicitName("value") int value) {
5454
}
5555
}
5656

57+
static class SingleArg1498 {
58+
final int _bar;
59+
60+
// note: annotation only to inject "implicit name" without needing parameter-names module
61+
SingleArg1498(@ImplicitName("bar") int b) {
62+
_bar = b;
63+
}
64+
}
65+
5766
private final ObjectMapper MAPPER_PROPS = mapperFor(ConstructorDetector.USE_PROPERTIES_BASED);
5867
private final ObjectMapper MAPPER_DELEGATING = mapperFor(ConstructorDetector.USE_DELEGATING);
5968
private final ObjectMapper MAPPER_EXPLICIT = mapperFor(ConstructorDetector.EXPLICIT_ONLY);
@@ -64,14 +73,12 @@ public SingleArgNoMode(@ImplicitName("value") int value) {
6473
/**********************************************************************
6574
*/
6675

67-
/*
6876
public void test1ArgDefaultsToPropertiesNonAnnotated() throws Exception
6977
{
7078
SingleArgNotAnnotated value = MAPPER_PROPS.readValue(a2q("{'value' : 137 }"),
7179
SingleArgNotAnnotated.class);
7280
assertEquals(137, value.v);
7381
}
74-
*/
7582

7683
public void test1ArgDefaultsToPropertiesNoMode() throws Exception
7784
{
@@ -81,6 +88,15 @@ public void test1ArgDefaultsToPropertiesNoMode() throws Exception
8188
assertEquals(137, value.v);
8289
}
8390

91+
// And specific test for original [databind#1498]
92+
public void test1ArgDefaultsToPropertiesIssue1498() throws Exception
93+
{
94+
// and similarly for mode-less
95+
SingleArg1498 value = MAPPER_PROPS.readValue(a2q("{'bar' : 404 }"),
96+
SingleArg1498.class);
97+
assertEquals(404, value._bar);
98+
}
99+
84100
/*
85101
/**********************************************************************
86102
/* Test methods, selecting from 1-arg constructors, delegating
@@ -148,12 +164,6 @@ public void test1ArgFailsNoMode() throws Exception
148164
}
149165
}
150166

151-
/*
152-
/**********************************************************************
153-
/* Test methods
154-
/**********************************************************************
155-
*/
156-
157167
/*
158168
/**********************************************************************
159169
/* Helper methods

0 commit comments

Comments
 (0)