Skip to content

Commit

Permalink
Update CreatorTest.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Jul 10, 2024
1 parent d9da688 commit 8999ac8
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ object CreatorTest
}
}

case class CaseClassAlternativeConstructor(script: String, dummy: Int) {
@JsonCreator
def this(script: String) = {
this(script, 0)
}
}

case class MultipleConstructors(script: String, dummy: Int) {
def this(script: String) = {
this(script, 0)
Expand Down Expand Up @@ -123,6 +130,19 @@ class CreatorTest extends DeserializationFixture {
roundTrip shouldEqual orig
}

it should "use secondary constructor annotated with JsonCreator (Case Class)" in { f =>
val orig = CaseClassAlternativeConstructor("abc", 42)
val bean = f.writeValueAsString(orig)
bean shouldBe """{"script":"abc","dummy":42}"""
val roundTrip = f.readValue(bean, classOf[CaseClassAlternativeConstructor])
roundTrip shouldEqual orig

// this part of test relies on the 2nd constructor being used (with the JsonCreator annotation)
val bean2 = """{"script":"abc"}"""
val cc2 = f.readValue(bean2, classOf[CaseClassAlternativeConstructor])
cc2 shouldEqual CaseClassAlternativeConstructor("abc", 0)
}

it should "use primary constructor if no JsonCreator annotation" in { f =>
val orig = MultipleConstructors("abc", 42)
val bean = f.writeValueAsString(orig)
Expand Down

0 comments on commit 8999ac8

Please sign in to comment.