Skip to content

Commit

Permalink
Fix #67 (or, fix via databind, but add release notes, tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 25, 2018
1 parent ff76427 commit b8ccc50
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

public class DelegatingCreatorTest
public class DelegatingCreatorTest extends ModuleTestBase
{
static class ClassWithDelegatingCreator {
private final String value;
Expand Down Expand Up @@ -55,44 +55,38 @@ public T getValue() {
}
}


@Test
public void shouldNotOverrideJsonCreatorAnnotationWithSpecifiedMode() throws IOException {

// given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

// when
ClassWithDelegatingCreator actual = objectMapper.readValue("{\"value\":\"aValue\"}",
ClassWithDelegatingCreator.class);

// then
Map<String, String> props = new HashMap<>();
props.put("value", "aValue");
ClassWithDelegatingCreator expected = new ClassWithDelegatingCreator(props);
then(actual).isEqualToComparingFieldByField(expected);
}

@Test
public void shouldDeserializeIntWrapper() throws Exception {
ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(new ParameterNamesModule());

IntWrapper actual = mapper.readValue
("{\"value\":13}", IntWrapper.class);
then(actual).isEqualToComparingFieldByField(new IntWrapper(13));
}

@Test
public void shouldDeserializeGenericWrapper() throws Exception {
ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(new ParameterNamesModule());

GenericWrapper<String> actual = mapper.readValue
("{\"value\":\"aValue\"}", new TypeReference<GenericWrapper<String>>() { });
then(actual).isEqualToComparingFieldByField(new GenericWrapper<>("aValue"));
}
@Test
public void shouldNotOverrideJsonCreatorAnnotationWithSpecifiedMode() throws IOException {
// given
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

// when
ClassWithDelegatingCreator actual = objectMapper.readValue("{\"value\":\"aValue\"}",
ClassWithDelegatingCreator.class);

// then
Map<String, String> props = new HashMap<>();
props.put("value", "aValue");
ClassWithDelegatingCreator expected = new ClassWithDelegatingCreator(props);
then(actual).isEqualToComparingFieldByField(expected);
}

@Test
public void shouldDeserializeIntWrapper() throws Exception {
ObjectMapper mapper = newMapper();

IntWrapper actual = mapper.readValue
("{\"value\":13}", IntWrapper.class);
then(actual).isEqualToComparingFieldByField(new IntWrapper(13));
}

@Test
public void shouldDeserializeGenericWrapper() throws Exception {
ObjectMapper mapper = newMapper();

GenericWrapper<String> actual = mapper.readValue
("{\"value\":\"aValue\"}", new TypeReference<GenericWrapper<String>>() { });
then(actual).isEqualToComparingFieldByField(new GenericWrapper<>("aValue"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fasterxml.jackson.module.paramnames;

import java.util.Arrays;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ModuleTestBase
{
protected static ObjectMapper newMapper() {
return new ObjectMapper()
.registerModule(new ParameterNamesModule());
}

protected String quote(String value) {
return "\"" + value + "\"";
}

protected String aposToQuotes(String json) {
return json.replace("'", "\"");
}

protected void verifyException(Throwable e, String... matches)
{
String msg = e.getMessage();
String lmsg = (msg == null) ? "" : msg.toLowerCase();
for (String match : matches) {
String lmatch = match.toLowerCase();
if (lmsg.indexOf(lmatch) >= 0) {
return;
}
}
throw new Error("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fasterxml.jackson.module.paramnames;

import static org.assertj.core.api.BDDAssertions.then;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class NamingStrategy67Test extends ModuleTestBase
{
static class ClassWithOneProperty {
public final String firstProperty;

@JsonCreator
// @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public ClassWithOneProperty(String firstProperty) {
this.firstProperty = "CTOR:"+firstProperty;
}
}
static class ClassWithTwoProperties {
public final int a;
public final int b;

private ClassWithTwoProperties(@JsonProperty("a") int a, @JsonProperty("b") int b) {
this.a = a+1;
this.b = b+1;
}
}

@Test
public void testSnakeCaseNaming() throws Exception
{
ObjectMapper mapper = newMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
;
final String MSG = "1st";
ClassWithOneProperty actual = mapper.readValue(
// "\""+MSG+"\"",
"{\"first_property\":\""+MSG+"\"}",
// "{\"firstProperty\":\""+MSG+"\"}",
ClassWithOneProperty.class);
then(actual).isEqualToComparingFieldByField(new ClassWithOneProperty(MSG));
}

@Test
public void testPrivateConstructorWithPropertyAnnotations() throws Exception
{
ObjectMapper mapper = newMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
ClassWithTwoProperties actual = mapper.readValue("{\"a\":1, \"b\": 2}",
ClassWithTwoProperties.class);

then(actual).isEqualToComparingFieldByField(new ClassWithTwoProperties(1, 2));
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-base</artifactId>
<version>2.9.5</version>
<version>2.9.6-SNAPSHOT</version>
</parent>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-modules-java8</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ Semyon Levin (remal@github)
#65: Use `DeserializationContext.handleWeirdXxxValue()` for datetime deserializers
(2.9.6)

Sonny Gill (sonnygill@github)
#67: `ParameterNamesModule` does not deserialize with a single parameter
constructor when using `SnakeCase` `PropertyNamingStrategy`
(2.9.6)
3 changes: 3 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Modules:

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

2.9.5 (26-Mar-2018)

Expand Down

0 comments on commit b8ccc50

Please sign in to comment.