Skip to content

Commit 20328f7

Browse files
committed
Update CreatorTest.scala
1 parent 6ce3b21 commit 20328f7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/test/scala/tools/jackson/module/scala/deser/CreatorTest.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ object CreatorTest
5555
}
5656
}
5757

58+
case class CaseClassAlternativeConstructor(script: String, dummy: Int) {
59+
@JsonCreator
60+
def this(script: String) = {
61+
this(script, 0)
62+
}
63+
}
64+
5865
case class MultipleConstructors(script: String, dummy: Int) {
5966
def this(script: String) = {
6067
this(script, 0)
@@ -124,6 +131,19 @@ class CreatorTest extends DeserializationFixture {
124131
roundTrip shouldEqual orig
125132
}
126133

134+
it should "use secondary constructor annotated with JsonCreator (Case Class)" in { f =>
135+
val orig = CaseClassAlternativeConstructor("abc", 42)
136+
val bean = f.writeValueAsString(orig)
137+
bean shouldBe """{"script":"abc","dummy":42}"""
138+
val roundTrip = f.readValue(bean, classOf[CaseClassAlternativeConstructor])
139+
roundTrip shouldEqual orig
140+
141+
// this part of test relies on the 2nd constructor being used (with the JsonCreator annotation)
142+
val bean2 = """{"script":"abc"}"""
143+
val cc2 = f.readValue(bean2, classOf[CaseClassAlternativeConstructor])
144+
cc2 shouldEqual CaseClassAlternativeConstructor("abc", 0)
145+
}
146+
127147
it should "use primary constructor if no JsonCreator annotation" in { f =>
128148
val orig = MultipleConstructors("abc", 42)
129149
val bean = f.writeValueAsString(orig)

0 commit comments

Comments
 (0)