Skip to content

Commit 7dd671d

Browse files
committed
Fix #1122, #1197
1 parent 131a405 commit 7dd671d

File tree

4 files changed

+92
-66
lines changed

4 files changed

+92
-66
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Project: jackson-databind
66

77
2.7.4 (not yet released)
88

9+
#1122: Jackson 2.7 and Lombok: 'Conflicting/ambiguous property name definitions'
910
#1178: `@JsonSerialize(contentAs=superType)` behavior disallowed in 2.7
1011
#1186: SimpleAbstractTypeResolver breaks generic parameters
1112
(reported by tobiash@github)
@@ -15,6 +16,7 @@ Project: jackson-databind
1516
#1194: Incorrect signature for generic type via `JavaType.getGenericSignature
1617
#1195: `JsonMappingException` not Serializable due to 2.7 reference to source (parser)
1718
(reported by mjustin@github)
19+
#1197: `SNAKE_CASE` doesn't work when using Lombok's `@AllArgsConstructor`
1820
#1198: Problem with `@JsonTypeInfo.As.EXTERNAL_PROPERTY`, `defaultImpl`, missing type id, NPE
1921
#1203: `@JsonTypeInfo` does not work correctly for ReferenceTypes like `AtomicReference`
2022
- Improve handling of custom content (de)serializers for `AtomicReference`

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,29 @@ public class JacksonAnnotationIntrospector
8080
* @since 2.7
8181
*/
8282
protected transient LRUMap<Class<?>,Boolean> _annotationsInside = new LRUMap<Class<?>,Boolean>(48, 48);
83-
83+
84+
/*
85+
/**********************************************************
86+
/* Local configuration settings
87+
/**********************************************************
88+
*/
89+
90+
/**
91+
* See {@link #setConstructorPropertiesImpliesCreator(boolean)} for
92+
* explanation.
93+
*<p>
94+
* Defaults to true.
95+
*
96+
* @since 2.7.4
97+
*/
98+
protected boolean _cfgConstructorPropertiesImpliesCreator = true;
99+
100+
/*
101+
/**********************************************************
102+
/* Life-cycle
103+
/**********************************************************
104+
*/
105+
84106
public JacksonAnnotationIntrospector() { }
85107

86108
@Override
@@ -95,6 +117,28 @@ protected Object readResolve() {
95117
return this;
96118
}
97119

120+
/*
121+
/**********************************************************
122+
/* Configuration
123+
/**********************************************************
124+
*/
125+
126+
/**
127+
* Method for changing behavior of {@link java.beans.ConstructorProperties}:
128+
* if set to `true`, existence DOES indicate that the given constructor should
129+
* be considered a creator; `false` that it should NOT be considered a creator
130+
* without explicit use of <code>JsonCreator</code> annotation.
131+
*<p>
132+
* Default setting is `true`
133+
*
134+
* @since 2.7.4
135+
*/
136+
public JacksonAnnotationIntrospector setConstructorPropertiesImpliesCreator(boolean b)
137+
{
138+
_cfgConstructorPropertiesImpliesCreator = b;
139+
return this;
140+
}
141+
98142
/*
99143
/**********************************************************
100144
/* General annotation properties
@@ -294,10 +338,9 @@ public VisibilityChecker<?> findAutoDetectVisibility(AnnotatedClass ac,
294338
*/
295339

296340
@Override
297-
public String findImplicitPropertyName(AnnotatedMember param) {
298-
// not known by default (until JDK8) for creators; default
299-
//
300-
return null;
341+
public String findImplicitPropertyName(AnnotatedMember m) {
342+
PropertyName n = _findConstructorName(m);
343+
return (n == null) ? null : n.getSimpleName();
301344
}
302345

303346
@Override
@@ -866,10 +909,6 @@ public PropertyName findNameForSerialization(Annotated a)
866909
if (pann != null) {
867910
return PropertyName.construct(pann.value());
868911
}
869-
PropertyName ctorName = _findConstructorName(a);
870-
if (ctorName != null) {
871-
return ctorName;
872-
}
873912
if (_hasOneOf(a, ANNOTATIONS_TO_INFER_SER)) {
874913
return PropertyName.USE_DEFAULT;
875914
}
@@ -1019,10 +1058,6 @@ public PropertyName findNameForDeserialization(Annotated a)
10191058
if (pann != null) {
10201059
return PropertyName.construct(pann.value());
10211060
}
1022-
PropertyName ctorName = _findConstructorName(a);
1023-
if (ctorName != null) {
1024-
return ctorName;
1025-
}
10261061
if (_hasOneOf(a, ANNOTATIONS_TO_INFER_DESER)) {
10271062
return PropertyName.USE_DEFAULT;
10281063
}
@@ -1059,11 +1094,15 @@ public boolean hasCreatorAnnotation(Annotated a)
10591094
if (ann != null) {
10601095
return (ann.mode() != JsonCreator.Mode.DISABLED);
10611096
}
1062-
if (a instanceof AnnotatedConstructor) {
1063-
if (_jdk7Helper != null) {
1064-
Boolean b = _jdk7Helper.hasCreatorAnnotation(a);
1065-
if (b != null) {
1066-
return b.booleanValue();
1097+
// 19-Apr-2016, tatu: As per [databind#1197], [databind#1122] (and some related),
1098+
// may or may not consider it a creator
1099+
if (_cfgConstructorPropertiesImpliesCreator ) {
1100+
if (a instanceof AnnotatedConstructor) {
1101+
if (_jdk7Helper != null) {
1102+
Boolean b = _jdk7Helper.hasCreatorAnnotation(a);
1103+
if (b != null) {
1104+
return b.booleanValue();
1105+
}
10671106
}
10681107
}
10691108
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.beans.ConstructorProperties;
44

5+
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.fasterxml.jackson.databind.*;
67

78
public class CreatorPropertiesTest extends BaseMapTest
@@ -22,6 +23,29 @@ public Issue905Bean(int a, int b) {
2223
}
2324
}
2425

26+
// for [databind#1122]
27+
static class Ambiguity {
28+
29+
@JsonProperty("bar")
30+
private int foo;
31+
32+
protected Ambiguity() {}
33+
34+
@ConstructorProperties({ "foo" })
35+
public Ambiguity(int foo) {
36+
this.foo = foo;
37+
}
38+
39+
public int getFoo() {
40+
return foo;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "Ambiguity [foo=" + foo + "]";
46+
}
47+
}
48+
2549
/*
2650
/**********************************************************
2751
/* Test methods
@@ -38,4 +62,13 @@ public void testCreatorPropertiesAnnotation() throws Exception
3862
assertEquals(2, b._x);
3963
assertEquals(3, b._y);
4064
}
65+
66+
// [databind#1122]
67+
public void testPossibleNamingConflict() throws Exception
68+
{
69+
String json = "{\"bar\":3}";
70+
Ambiguity amb = MAPPER.readValue(json, Ambiguity.class);
71+
assertNotNull(amb);
72+
assertEquals(3, amb.getFoo());
73+
}
4174
}

src/test/java/com/fasterxml/jackson/failing/CreatorProperties1122Test.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)