Skip to content

Commit 6888ed4

Browse files
committed
Add a test to show #2353 works
1 parent edcec24 commit 6888ed4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/creators/TestCreatorsDelegating.java

+40
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.fasterxml.jackson.annotation.JacksonInject;
66
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
78

89
import com.fasterxml.jackson.core.JsonParser;
910
import com.fasterxml.jackson.core.JsonToken;
@@ -82,6 +83,32 @@ public MapBean(Map<String, Long> map) {
8283
}
8384
}
8485

86+
// [databind#2353]: allow delegating and properties-based
87+
static class SuperToken2353 {
88+
public long time;
89+
public String username;
90+
91+
@JsonCreator(mode=JsonCreator.Mode.DELEGATING) // invoked when a string is passed
92+
public static SuperToken2353 from(String username) {
93+
SuperToken2353 token = new SuperToken2353();
94+
token.username = username;
95+
token.time = System.currentTimeMillis();
96+
return token;
97+
}
98+
99+
@JsonCreator(mode=JsonCreator.Mode.PROPERTIES) // invoked when an object is passed, pre-validating property existence
100+
public static SuperToken2353 create(
101+
@JsonProperty("name") String username,
102+
@JsonProperty("time") long time)
103+
{
104+
SuperToken2353 token = new SuperToken2353();
105+
token.username = username;
106+
token.time = time;
107+
108+
return token;
109+
}
110+
}
111+
85112
/*
86113
/**********************************************************
87114
/* Test methods
@@ -179,4 +206,17 @@ public void testIssue465() throws Exception
179206
bean = MAPPER.readValue(EMPTY_JSON, MapBean.class);
180207
assertEquals(0, bean.map.size());
181208
}
209+
210+
// [databind#2353]: allow delegating and properties-based
211+
public void testMultipleCreators2353() throws Exception
212+
{
213+
// first, test delegating
214+
SuperToken2353 result = MAPPER.readValue(quote("Bob"), SuperToken2353.class);
215+
assertEquals("Bob", result.username);
216+
217+
// and then properties-based
218+
result = MAPPER.readValue(aposToQuotes("{'name':'Billy', 'time':123}"), SuperToken2353.class);
219+
assertEquals("Billy", result.username);
220+
assertEquals(123L, result.time);
221+
}
182222
}

0 commit comments

Comments
 (0)