Skip to content

Commit dd57a2d

Browse files
committed
Fix #2051
1 parent 3115356 commit dd57a2d

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Project: jackson-databind
4343
#2038: JDK Serializing and using Deserialized `ObjectMapper` loses linkage
4444
back from `JsonParser.getCodec()`
4545
(reported by Chetan N)
46+
#2051: Implicit constructor property names are not renamed properly with
47+
`PropertyNamingStrategy`
4648

4749
2.9.5 (26-Mar-2018)
4850

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,11 @@ protected void _addExplicitAnyCreator(DeserializationContext ctxt,
728728
if (!useProps && (paramDef != null)) {
729729
// One more thing: if implicit name matches property with a getter
730730
// or field, we'll consider it property-based as well
731-
paramName = candidate.findImplicitParamName(0);
731+
732+
// 25-May-2018, tatu: as per [databind#2051], looks like we have to get
733+
// not implicit name, but name with possible strategy-based-rename
734+
// paramName = candidate.findImplicitParamName(0);
735+
paramName = candidate.paramName(0);
732736
useProps = (paramName != null) && paramDef.couldSerialize();
733737
}
734738
if (useProps) {

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -865,21 +865,20 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
865865
}
866866
}
867867
final String simpleName;
868-
if (rename != null && !fullName.hasSimpleName(rename)) {
868+
if ((rename != null) && !fullName.hasSimpleName(rename)) {
869869
prop = prop.withSimpleName(rename);
870870
simpleName = rename;
871871
} else {
872872
simpleName = fullName.getSimpleName();
873873
}
874-
/* As per [JACKSON-687], need to consider case where there may already be
875-
* something in there...
876-
*/
874+
// Need to consider case where there may already be something in there...
877875
POJOPropertyBuilder old = propMap.get(simpleName);
878876
if (old == null) {
879877
propMap.put(simpleName, prop);
880878
} else {
881879
old.addAll(prop);
882880
}
881+
883882
// replace the creatorProperty too, if there is one
884883
_updateCreatorProperty(prop, _creatorProperties);
885884
}
@@ -1096,9 +1095,11 @@ private PropertyNamingStrategy _findNamingStrategy()
10961095
}
10971096

10981097
protected void _updateCreatorProperty(POJOPropertyBuilder prop, List<POJOPropertyBuilder> creatorProperties) {
1098+
10991099
if (creatorProperties != null) {
1100+
final String intName = prop.getInternalName();
11001101
for (int i = 0, len = creatorProperties.size(); i < len; ++i) {
1101-
if (creatorProperties.get(i).getInternalName().equals(prop.getInternalName())) {
1102+
if (creatorProperties.get(i).getInternalName().equals(intName)) {
11021103
creatorProperties.set(i, prop);
11031104
break;
11041105
}

src/test/java/com/fasterxml/jackson/failing/CreatorWithNamingStrategy2008Test.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/creators/CreatorWithNamingStrategyTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package com.fasterxml.jackson.failing;
1+
package com.fasterxml.jackson.databind.deser.creators;
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.databind.*;
55
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
66
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
77
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
88

9-
public class CreatorWithNamingStrategy2008Test extends BaseMapTest
9+
public class CreatorWithNamingStrategyTest extends BaseMapTest
1010
{
1111
@SuppressWarnings("serial")
1212
static class MyParamIntrospector extends JacksonAnnotationIntrospector
@@ -21,7 +21,7 @@ public String findImplicitPropertyName(AnnotatedMember param) {
2121
}
2222
}
2323

24-
// wrt [https://github.com/FasterXML/jackson-modules-java8/issues/67]
24+
// [databind#2051]
2525
static class OneProperty {
2626
public String paramName0;
2727

@@ -37,18 +37,17 @@ public OneProperty(String bogus) {
3737
/**********************************************************
3838
*/
3939

40-
private final ObjectMapper MAPPER = objectMapper()
40+
private final ObjectMapper MAPPER = newObjectMapper()
4141
.setAnnotationIntrospector(new MyParamIntrospector())
4242
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
4343
;
4444

45-
// Possibly [databind#2008], but originally
46-
// wrt [https://github.com/FasterXML/jackson-modules-java8/issues/67]
45+
// [databind#2051]
4746
public void testSnakeCaseWithOneArg() throws Exception
4847
{
4948
final String MSG = "1st";
5049
OneProperty actual = MAPPER.readValue(
51-
"{\"first_property\":\""+MSG+"\"}",
50+
"{\"param_name0\":\""+MSG+"\"}",
5251
OneProperty.class);
5352
assertEquals("CTOR:"+MSG, actual.paramName0);
5453
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fasterxml.jackson.failing;
22

3-
import com.fasterxml.jackson.annotation.JsonCreator;
43
import com.fasterxml.jackson.databind.*;
54
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
65
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
@@ -38,7 +37,7 @@ public XY(int x, int y) {
3837
/**********************************************************
3938
*/
4039

41-
private final ObjectMapper MAPPER = objectMapper()
40+
private final ObjectMapper MAPPER = newObjectMapper()
4241
.setAnnotationIntrospector(new MyParamIntrospector())
4342
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
4443
;

0 commit comments

Comments
 (0)