Skip to content

Commit b8ccc50

Browse files
committed
Fix #67 (or, fix via databind, but add release notes, tests)
1 parent ff76427 commit b8ccc50

File tree

6 files changed

+135
-42
lines changed

6 files changed

+135
-42
lines changed

parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/DelegatingCreatorTest.java

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import static org.assertj.core.api.BDDAssertions.*;
1414

15-
public class DelegatingCreatorTest
15+
public class DelegatingCreatorTest extends ModuleTestBase
1616
{
1717
static class ClassWithDelegatingCreator {
1818
private final String value;
@@ -55,44 +55,38 @@ public T getValue() {
5555
}
5656
}
5757

58-
59-
@Test
60-
public void shouldNotOverrideJsonCreatorAnnotationWithSpecifiedMode() throws IOException {
61-
62-
// given
63-
ObjectMapper objectMapper = new ObjectMapper();
64-
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
65-
66-
// when
67-
ClassWithDelegatingCreator actual = objectMapper.readValue("{\"value\":\"aValue\"}",
68-
ClassWithDelegatingCreator.class);
69-
70-
// then
71-
Map<String, String> props = new HashMap<>();
72-
props.put("value", "aValue");
73-
ClassWithDelegatingCreator expected = new ClassWithDelegatingCreator(props);
74-
then(actual).isEqualToComparingFieldByField(expected);
75-
}
76-
77-
@Test
78-
public void shouldDeserializeIntWrapper() throws Exception {
79-
ObjectMapper mapper = new ObjectMapper();
80-
81-
mapper.registerModule(new ParameterNamesModule());
82-
83-
IntWrapper actual = mapper.readValue
84-
("{\"value\":13}", IntWrapper.class);
85-
then(actual).isEqualToComparingFieldByField(new IntWrapper(13));
86-
}
87-
88-
@Test
89-
public void shouldDeserializeGenericWrapper() throws Exception {
90-
ObjectMapper mapper = new ObjectMapper();
91-
92-
mapper.registerModule(new ParameterNamesModule());
93-
94-
GenericWrapper<String> actual = mapper.readValue
95-
("{\"value\":\"aValue\"}", new TypeReference<GenericWrapper<String>>() { });
96-
then(actual).isEqualToComparingFieldByField(new GenericWrapper<>("aValue"));
97-
}
58+
@Test
59+
public void shouldNotOverrideJsonCreatorAnnotationWithSpecifiedMode() throws IOException {
60+
// given
61+
ObjectMapper objectMapper = new ObjectMapper();
62+
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
63+
64+
// when
65+
ClassWithDelegatingCreator actual = objectMapper.readValue("{\"value\":\"aValue\"}",
66+
ClassWithDelegatingCreator.class);
67+
68+
// then
69+
Map<String, String> props = new HashMap<>();
70+
props.put("value", "aValue");
71+
ClassWithDelegatingCreator expected = new ClassWithDelegatingCreator(props);
72+
then(actual).isEqualToComparingFieldByField(expected);
73+
}
74+
75+
@Test
76+
public void shouldDeserializeIntWrapper() throws Exception {
77+
ObjectMapper mapper = newMapper();
78+
79+
IntWrapper actual = mapper.readValue
80+
("{\"value\":13}", IntWrapper.class);
81+
then(actual).isEqualToComparingFieldByField(new IntWrapper(13));
82+
}
83+
84+
@Test
85+
public void shouldDeserializeGenericWrapper() throws Exception {
86+
ObjectMapper mapper = newMapper();
87+
88+
GenericWrapper<String> actual = mapper.readValue
89+
("{\"value\":\"aValue\"}", new TypeReference<GenericWrapper<String>>() { });
90+
then(actual).isEqualToComparingFieldByField(new GenericWrapper<>("aValue"));
91+
}
9892
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fasterxml.jackson.module.paramnames;
2+
3+
import java.util.Arrays;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public class ModuleTestBase
8+
{
9+
protected static ObjectMapper newMapper() {
10+
return new ObjectMapper()
11+
.registerModule(new ParameterNamesModule());
12+
}
13+
14+
protected String quote(String value) {
15+
return "\"" + value + "\"";
16+
}
17+
18+
protected String aposToQuotes(String json) {
19+
return json.replace("'", "\"");
20+
}
21+
22+
protected void verifyException(Throwable e, String... matches)
23+
{
24+
String msg = e.getMessage();
25+
String lmsg = (msg == null) ? "" : msg.toLowerCase();
26+
for (String match : matches) {
27+
String lmatch = match.toLowerCase();
28+
if (lmsg.indexOf(lmatch) >= 0) {
29+
return;
30+
}
31+
}
32+
throw new Error("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
33+
}
34+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fasterxml.jackson.module.paramnames;
2+
3+
import static org.assertj.core.api.BDDAssertions.then;
4+
5+
import org.junit.Test;
6+
7+
import com.fasterxml.jackson.annotation.JsonCreator;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
11+
12+
public class NamingStrategy67Test extends ModuleTestBase
13+
{
14+
static class ClassWithOneProperty {
15+
public final String firstProperty;
16+
17+
@JsonCreator
18+
// @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
19+
public ClassWithOneProperty(String firstProperty) {
20+
this.firstProperty = "CTOR:"+firstProperty;
21+
}
22+
}
23+
static class ClassWithTwoProperties {
24+
public final int a;
25+
public final int b;
26+
27+
private ClassWithTwoProperties(@JsonProperty("a") int a, @JsonProperty("b") int b) {
28+
this.a = a+1;
29+
this.b = b+1;
30+
}
31+
}
32+
33+
@Test
34+
public void testSnakeCaseNaming() throws Exception
35+
{
36+
ObjectMapper mapper = newMapper()
37+
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
38+
;
39+
final String MSG = "1st";
40+
ClassWithOneProperty actual = mapper.readValue(
41+
// "\""+MSG+"\"",
42+
"{\"first_property\":\""+MSG+"\"}",
43+
// "{\"firstProperty\":\""+MSG+"\"}",
44+
ClassWithOneProperty.class);
45+
then(actual).isEqualToComparingFieldByField(new ClassWithOneProperty(MSG));
46+
}
47+
48+
@Test
49+
public void testPrivateConstructorWithPropertyAnnotations() throws Exception
50+
{
51+
ObjectMapper mapper = newMapper()
52+
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
53+
ClassWithTwoProperties actual = mapper.readValue("{\"a\":1, \"b\": 2}",
54+
ClassWithTwoProperties.class);
55+
56+
then(actual).isEqualToComparingFieldByField(new ClassWithTwoProperties(1, 2));
57+
}
58+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.fasterxml.jackson</groupId>
55
<artifactId>jackson-base</artifactId>
6-
<version>2.9.5</version>
6+
<version>2.9.6-SNAPSHOT</version>
77
</parent>
88
<groupId>com.fasterxml.jackson.module</groupId>
99
<artifactId>jackson-modules-java8</artifactId>

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ Semyon Levin (remal@github)
3232
#65: Use `DeserializationContext.handleWeirdXxxValue()` for datetime deserializers
3333
(2.9.6)
3434

35+
Sonny Gill (sonnygill@github)
36+
#67: `ParameterNamesModule` does not deserialize with a single parameter
37+
constructor when using `SnakeCase` `PropertyNamingStrategy`
38+
(2.9.6)

release-notes/VERSION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Modules:
1212

1313
#65: Use `DeserializationContext.handleWeirdXxxValue()` for datetime deserializers
1414
(contributed by Semyon L)
15+
#67: `ParameterNamesModule` does not deserialize with a single parameter
16+
constructor when using `SnakeCase` `PropertyNamingStrategy`
17+
(reported by Sonny G)
1518

1619
2.9.5 (26-Mar-2018)
1720

0 commit comments

Comments
 (0)