From d22bddafd0d94cbba9579946ac95b289d3a01bfc Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 1 Aug 2024 17:54:31 +0100 Subject: [PATCH] add default value tests --- .../scala/deser/OptionDeserializerTest.scala | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/test/scala/com/fasterxml/jackson/module/scala/deser/OptionDeserializerTest.scala b/src/test/scala/com/fasterxml/jackson/module/scala/deser/OptionDeserializerTest.scala index 43eb119c..c08f483e 100644 --- a/src/test/scala/com/fasterxml/jackson/module/scala/deser/OptionDeserializerTest.scala +++ b/src/test/scala/com/fasterxml/jackson/module/scala/deser/OptionDeserializerTest.scala @@ -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 @@ -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 { @@ -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"))))