Skip to content

Commit e8909ff

Browse files
committed
refactor option deserialization tests
1 parent 6578b47 commit e8909ff

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fasterxml.jackson.module.scala.deser
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
4+
import com.fasterxml.jackson.module.scala.DefaultScalaModule
5+
6+
object OptionWithNumberDeserializerTest {
7+
case class AnnotatedOptionLong(@JsonDeserialize(contentAs = classOf[java.lang.Long]) valueLong: Option[Long])
8+
case class AnnotatedOptionPrimitiveLong(@JsonDeserialize(contentAs = classOf[Long]) valueLong: Option[Long])
9+
case class OptionLong(valueLong: Option[Long])
10+
case class OptionJavaLong(valueLong: Option[java.lang.Long])
11+
case class OptionBigInt(value: Option[BigInt])
12+
}
13+
14+
class OptionWithNumberDeserializerTest extends DeserializerTest {
15+
lazy val module: DefaultScalaModule.type = DefaultScalaModule
16+
import OptionWithNumberDeserializerTest._
17+
18+
private def useOptionLong(v: Option[Long]): Long = v.map(_ * 2).getOrElse(0L)
19+
private def useOptionJavaLong(v: Option[java.lang.Long]): Long = v.map(_ * 2).getOrElse(0L)
20+
private def useOptionBigInt(v: Option[BigInt]): Long = v.map(_ * 2).map(_.toLong).getOrElse(0L)
21+
22+
"JacksonModuleScala" should "support AnnotatedOptionLong" in {
23+
val v1 = deserialize("""{"valueLong":151}""", classOf[AnnotatedOptionLong])
24+
v1 shouldBe AnnotatedOptionLong(Some(151L))
25+
v1.valueLong.get shouldBe 151L
26+
useOptionLong(v1.valueLong) shouldBe 302L
27+
}
28+
29+
it should "support AnnotatedOptionPrimitiveLong" in {
30+
val v1 = deserialize("""{"valueLong":151}""", classOf[AnnotatedOptionPrimitiveLong])
31+
v1 shouldBe AnnotatedOptionPrimitiveLong(Some(151L))
32+
v1.valueLong.get shouldBe 151L
33+
useOptionLong(v1.valueLong) shouldBe 302L
34+
}
35+
36+
it should "support OptionLong" in {
37+
val v1 = deserialize("""{"valueLong":151}""", classOf[OptionLong])
38+
v1 shouldBe OptionLong(Some(151L))
39+
v1.valueLong.get shouldBe 151L
40+
//next assert fails due to unboxing issue -- without the @JsonDeserialize to help, jackson will
41+
//erroneously create an Option[Int] instead of Option[Long] leading to a class cast exception
42+
//useOptionLong(v1.valueLong) shouldBe 302L
43+
}
44+
45+
it should "support OptionJavaLong" in {
46+
val v1 = deserialize("""{"valueLong":151}""", classOf[OptionJavaLong])
47+
v1 shouldBe OptionJavaLong(Some(151L))
48+
v1.valueLong.get shouldBe 151L
49+
useOptionJavaLong(v1.valueLong) shouldBe 302L
50+
}
51+
52+
it should "support OptionBigInt" in {
53+
val v1 = deserialize("""{"value":151}""", classOf[OptionBigInt])
54+
v1 shouldBe OptionBigInt(Some(BigInt(151L)))
55+
v1.value.get shouldBe 151L
56+
useOptionBigInt(v1.value) shouldBe 302L
57+
}
58+
}

src/test/scala/com/fasterxml/jackson/module/scala/deser/ParamWithDashNameDeserializerTest.scala

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize
66
import com.fasterxml.jackson.module.scala.DefaultScalaModule
77

88
object ParamWithDashNameDeserializerTest {
9-
case class AnnotatedOptionLong(@JsonDeserialize(contentAs = classOf[java.lang.Long]) valueLong: Option[Long])
10-
119
case class OptionLongWithDash(`value-long`: Option[Long])
1210

1311
case class AnnotatedOptionLongWithDash(@JsonDeserialize(contentAs = classOf[java.lang.Long]) `value-long`: Option[Long])
@@ -21,19 +19,7 @@ class ParamWithDashNameDeserializerTest extends DeserializerTest {
2119

2220
private def useOptionLong(v: Option[Long]): Long = v.map(_ * 2).getOrElse(0L)
2321

24-
"JacksonModuleScala" should "support standard param names" in {
25-
// check deserialization
26-
val v1 = deserialize("""{"valueLong":151}""", classOf[AnnotatedOptionLong])
27-
v1 shouldBe AnnotatedOptionLong(Some(151L))
28-
v1.valueLong.get shouldBe 151L
29-
30-
val v2 = deserialize(serialize(AnnotatedOptionLong(Some(152))), classOf[AnnotatedOptionLong])
31-
v2 shouldBe AnnotatedOptionLong(Some(152L))
32-
v2.valueLong.get shouldBe 152L
33-
useOptionLong(v2.valueLong) shouldBe 304L
34-
}
35-
36-
it should "support param names with dashes" in {
22+
"JacksonModuleScala" should "support param names with dashes" in {
3723
// check deserialization
3824
val typeRef = new TypeReference[OptionLongWithDash] {}
3925
val v1 = deserialize("""{"value-long":251}""", typeRef)

0 commit comments

Comments
 (0)