Skip to content

Commit d22bdda

Browse files
committed
add default value tests
1 parent 797c5e9 commit d22bdda

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.scala.deser
22

33
import com.fasterxml.jackson.annotation.{JsonSetter, JsonSubTypes, JsonTypeInfo, JsonTypeName, Nulls}
44
import com.fasterxml.jackson.core.`type`.TypeReference
5-
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.databind.{MapperFeature, ObjectMapper}
66
import com.fasterxml.jackson.databind.json.JsonMapper
77
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
88
import com.fasterxml.jackson.module.scala.DefaultScalaModule
@@ -41,6 +41,8 @@ object OptionDeserializerTest {
4141

4242
case class OptionSeqLong(values: Option[Seq[Long]])
4343
case class WrappedOptionSeqLong(text: String, wrappedLongs: OptionSeqLong)
44+
45+
case class OptionWithDefault(id: Int, value: Option[String] = Some("123"))
4446
}
4547

4648
class OptionDeserializerTest extends DeserializerTest {
@@ -111,6 +113,27 @@ class OptionDeserializerTest extends DeserializerTest {
111113
v2 shouldEqual v
112114
}
113115

116+
it should "deserialize case class with an option that has non-empty default" in {
117+
val json: String = """{"id":123}"""
118+
deserialize(json, classOf[OptionWithDefault]) shouldEqual OptionWithDefault(123, Some("123"))
119+
}
120+
121+
it should "deserialize case class with an option that has non-empty default (explicit null value in json)" in {
122+
// see https://github.com/FasterXML/jackson-module-scala/issues/687
123+
val json: String = """{"id":123, "value": null}"""
124+
deserialize(json, classOf[OptionWithDefault]) shouldEqual OptionWithDefault(123, Some("123"))
125+
}
126+
127+
it should "deserialize case class with an option that has non-empty default (disable MapperFeature.APPLY_DEFAULT_VALUES)" in {
128+
val json: String = """{"id":123}"""
129+
val mapper = JsonMapper.builder()
130+
.addModule(DefaultScalaModule)
131+
.disable(MapperFeature.APPLY_DEFAULT_VALUES)
132+
.build()
133+
val o1 = mapper.readValue(json, classOf[OptionWithDefault])
134+
o1 shouldEqual OptionWithDefault(123, None)
135+
}
136+
114137
it should "deserialize case class with an option of seq of strings" in {
115138
val mapper = JsonMapper.builder().addModule(DefaultScalaModule).build()
116139
val w1 = WrappedOptionSeqString("myText", OptionSeqString(Option(Seq("s1", "s2"))))

0 commit comments

Comments
 (0)