@@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.scala.deser
2
2
3
3
import com .fasterxml .jackson .annotation .{JsonSetter , JsonSubTypes , JsonTypeInfo , JsonTypeName , Nulls }
4
4
import com .fasterxml .jackson .core .`type` .TypeReference
5
- import com .fasterxml .jackson .databind .ObjectMapper
5
+ import com .fasterxml .jackson .databind .{ MapperFeature , ObjectMapper }
6
6
import com .fasterxml .jackson .databind .json .JsonMapper
7
7
import com .fasterxml .jackson .datatype .jdk8 .Jdk8Module
8
8
import com .fasterxml .jackson .module .scala .DefaultScalaModule
@@ -41,6 +41,8 @@ object OptionDeserializerTest {
41
41
42
42
case class OptionSeqLong (values : Option [Seq [Long ]])
43
43
case class WrappedOptionSeqLong (text : String , wrappedLongs : OptionSeqLong )
44
+
45
+ case class OptionWithDefault (id : Int , value : Option [String ] = Some (" 123" ))
44
46
}
45
47
46
48
class OptionDeserializerTest extends DeserializerTest {
@@ -111,6 +113,27 @@ class OptionDeserializerTest extends DeserializerTest {
111
113
v2 shouldEqual v
112
114
}
113
115
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
+
114
137
it should " deserialize case class with an option of seq of strings" in {
115
138
val mapper = JsonMapper .builder().addModule(DefaultScalaModule ).build()
116
139
val w1 = WrappedOptionSeqString (" myText" , OptionSeqString (Option (Seq (" s1" , " s2" ))))
0 commit comments