Skip to content

Commit

Permalink
Fix #2051
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 25, 2018
1 parent 3115356 commit dd57a2d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project: jackson-databind
#2038: JDK Serializing and using Deserialized `ObjectMapper` loses linkage
back from `JsonParser.getCodec()`
(reported by Chetan N)
#2051: Implicit constructor property names are not renamed properly with
`PropertyNamingStrategy`

2.9.5 (26-Mar-2018)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,11 @@ protected void _addExplicitAnyCreator(DeserializationContext ctxt,
if (!useProps && (paramDef != null)) {
// One more thing: if implicit name matches property with a getter
// or field, we'll consider it property-based as well
paramName = candidate.findImplicitParamName(0);

// 25-May-2018, tatu: as per [databind#2051], looks like we have to get
// not implicit name, but name with possible strategy-based-rename
// paramName = candidate.findImplicitParamName(0);
paramName = candidate.paramName(0);
useProps = (paramName != null) && paramDef.couldSerialize();
}
if (useProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,21 +865,20 @@ protected void _renameUsing(Map<String, POJOPropertyBuilder> propMap,
}
}
final String simpleName;
if (rename != null && !fullName.hasSimpleName(rename)) {
if ((rename != null) && !fullName.hasSimpleName(rename)) {
prop = prop.withSimpleName(rename);
simpleName = rename;
} else {
simpleName = fullName.getSimpleName();
}
/* As per [JACKSON-687], need to consider case where there may already be
* something in there...
*/
// Need to consider case where there may already be something in there...
POJOPropertyBuilder old = propMap.get(simpleName);
if (old == null) {
propMap.put(simpleName, prop);
} else {
old.addAll(prop);
}

// replace the creatorProperty too, if there is one
_updateCreatorProperty(prop, _creatorProperties);
}
Expand Down Expand Up @@ -1096,9 +1095,11 @@ private PropertyNamingStrategy _findNamingStrategy()
}

protected void _updateCreatorProperty(POJOPropertyBuilder prop, List<POJOPropertyBuilder> creatorProperties) {

if (creatorProperties != null) {
final String intName = prop.getInternalName();
for (int i = 0, len = creatorProperties.size(); i < len; ++i) {
if (creatorProperties.get(i).getInternalName().equals(prop.getInternalName())) {
if (creatorProperties.get(i).getInternalName().equals(intName)) {
creatorProperties.set(i, prop);
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.deser.creators;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

public class CreatorWithNamingStrategy2008Test extends BaseMapTest
public class CreatorWithNamingStrategyTest extends BaseMapTest
{
@SuppressWarnings("serial")
static class MyParamIntrospector extends JacksonAnnotationIntrospector
Expand All @@ -21,7 +21,7 @@ public String findImplicitPropertyName(AnnotatedMember param) {
}
}

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

Expand All @@ -37,18 +37,17 @@ public OneProperty(String bogus) {
/**********************************************************
*/

private final ObjectMapper MAPPER = objectMapper()
private final ObjectMapper MAPPER = newObjectMapper()
.setAnnotationIntrospector(new MyParamIntrospector())
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
;

// Possibly [databind#2008], but originally
// wrt [https://github.com/FasterXML/jackson-modules-java8/issues/67]
// [databind#2051]
public void testSnakeCaseWithOneArg() throws Exception
{
final String MSG = "1st";
OneProperty actual = MAPPER.readValue(
"{\"first_property\":\""+MSG+"\"}",
"{\"param_name0\":\""+MSG+"\"}",
OneProperty.class);
assertEquals("CTOR:"+MSG, actual.paramName0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fasterxml.jackson.failing;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
Expand Down Expand Up @@ -38,7 +37,7 @@ public XY(int x, int y) {
/**********************************************************
*/

private final ObjectMapper MAPPER = objectMapper()
private final ObjectMapper MAPPER = newObjectMapper()
.setAnnotationIntrospector(new MyParamIntrospector())
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
;
Expand Down

0 comments on commit dd57a2d

Please sign in to comment.