Skip to content

Commit

Permalink
add default value tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Aug 1, 2024
1 parent 797c5e9 commit d22bdda
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.fasterxml.jackson.module.scala.deser

import com.fasterxml.jackson.annotation.{JsonSetter, JsonSubTypes, JsonTypeInfo, JsonTypeName, Nulls}
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.{MapperFeature, ObjectMapper}
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.module.scala.DefaultScalaModule
Expand Down Expand Up @@ -41,6 +41,8 @@ object OptionDeserializerTest {

case class OptionSeqLong(values: Option[Seq[Long]])
case class WrappedOptionSeqLong(text: String, wrappedLongs: OptionSeqLong)

case class OptionWithDefault(id: Int, value: Option[String] = Some("123"))
}

class OptionDeserializerTest extends DeserializerTest {
Expand Down Expand Up @@ -111,6 +113,27 @@ class OptionDeserializerTest extends DeserializerTest {
v2 shouldEqual v
}

it should "deserialize case class with an option that has non-empty default" in {
val json: String = """{"id":123}"""
deserialize(json, classOf[OptionWithDefault]) shouldEqual OptionWithDefault(123, Some("123"))
}

it should "deserialize case class with an option that has non-empty default (explicit null value in json)" in {
// see https://github.com/FasterXML/jackson-module-scala/issues/687
val json: String = """{"id":123, "value": null}"""
deserialize(json, classOf[OptionWithDefault]) shouldEqual OptionWithDefault(123, Some("123"))
}

it should "deserialize case class with an option that has non-empty default (disable MapperFeature.APPLY_DEFAULT_VALUES)" in {
val json: String = """{"id":123}"""
val mapper = JsonMapper.builder()
.addModule(DefaultScalaModule)
.disable(MapperFeature.APPLY_DEFAULT_VALUES)
.build()
val o1 = mapper.readValue(json, classOf[OptionWithDefault])
o1 shouldEqual OptionWithDefault(123, None)
}

it should "deserialize case class with an option of seq of strings" in {
val mapper = JsonMapper.builder().addModule(DefaultScalaModule).build()
val w1 = WrappedOptionSeqString("myText", OptionSeqString(Option(Seq("s1", "s2"))))
Expand Down

0 comments on commit d22bdda

Please sign in to comment.