Skip to content

Commit

Permalink
Add a failing test for #178
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 20, 2020
1 parent 05893c5 commit 9b363bf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,37 @@

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;

import org.junit.*;

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

public class JsonCreatorTest
public class JsonCreatorTest extends ModuleTestBase
{
@Test
public void shouldDeserializeClassWithJsonCreatorOnStaticMethod() throws Exception {

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

static class ClassWithJsonCreatorOnStaticMethod {
final String first;
final String second;

ClassWithJsonCreatorOnStaticMethod(String first, String second) {
this.first = first;
this.second = second;
}

@JsonCreator
static ClassWithJsonCreatorOnStaticMethod factory(String first, String second) {
return new ClassWithJsonCreatorOnStaticMethod(first, second);
}
}

private final ObjectMapper MAPPER = newMapper();

@Test
public void testJsonCreatorOnStaticMethod() throws Exception
{
// when
String json = "{\"first\":\"1st\",\"second\":\"2nd\"}";
ClassWithJsonCreatorOnStaticMethod actual = objectMapper.readValue(json, ClassWithJsonCreatorOnStaticMethod.class);
String json = a2q("{'first':'1st','second':'2nd'}");
ClassWithJsonCreatorOnStaticMethod actual = MAPPER.readValue(json, ClassWithJsonCreatorOnStaticMethod.class);

then(actual).isEqualToComparingFieldByField(new ClassWithJsonCreatorOnStaticMethod("1st", "2nd"));
}

static class ClassWithJsonCreatorOnStaticMethod {
final String first;
final String second;

ClassWithJsonCreatorOnStaticMethod(String first, String second) {
this.first = first;
this.second = second;
}

@JsonCreator
static ClassWithJsonCreatorOnStaticMethod factory(String first, String second) {

return new ClassWithJsonCreatorOnStaticMethod(first, second);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ protected static JsonMapper.Builder mapperBuilder() {
.addModule(new ParameterNamesModule());
}

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fasterxml.jackson.module.paramnames.failing;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.module.paramnames.ModuleTestBase;

import org.junit.*;

import static org.junit.Assert.assertNotNull;

public class JsonCreator178Test extends ModuleTestBase
{
// [modules-base#178]
static class Bean178
{
int _a, _b;

public Bean178(@JsonDeserialize() int a, int b) {
_a = a;
_b = b;
}
}

private final ObjectMapper MAPPER = newMapper();

// [modules-base#178]
@Test
public void testJsonCreatorWithOtherAnnotations() throws Exception
{
Bean178 bean = MAPPER.readValue(a2q("{'a':1,'b':2}"), Bean178.class);
assertNotNull(bean);
}
}

0 comments on commit 9b363bf

Please sign in to comment.