Skip to content

Fix deserialization of un-annotated Option[Trait] fields #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private class OptionDeserializer(fullType: JavaType,
if (t == JsonToken.VALUE_NULL) {
getNullValue(ctxt)
} else {
typeDeserializer.deserializeTypedFromAny(jp, ctxt).asInstanceOf[Option[AnyRef]]
val value = valueTypeDeserializer.get.deserializeTypedFromAny(jp, ctxt)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't quite seem right. I believe that this deserializeWithType method is supposed to use the passed in typeDeserializer as an override. I will have to look-through/debug your example/test-case more carefully to see if I can figure out how/why it's passing a deserializer that does not work.

Some(value)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo, JsonTypeNam
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.ser.SerializerTest
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

Expand Down Expand Up @@ -32,6 +33,10 @@ object OptionDeserializerTest {

case class Foo(bar: String)
case class Wrapper[T](t: T)

trait TraitWithoutAnnotations
case class TraitImplementation(i: Int) extends TraitWithoutAnnotations
case class TraitOptionHolder(x: Option[TraitWithoutAnnotations])
}

@RunWith(classOf[JUnitRunner])
Expand Down Expand Up @@ -91,4 +96,15 @@ class OptionDeserializerTest extends DeserializerTest {
result = m.readValue[Wrapper[Option[Foo]]](json)
result.t.get.isInstanceOf[Foo] should be(true)
}

it should "deserialize an un-annotated trait object serialized by OptionSerializer" in {
val om = new ObjectMapper with ScalaObjectMapper {
registerModule(module)
enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE, "_t")
}
val expected = TraitOptionHolder(Some(TraitImplementation(1)))
val json = om.writeValueAsString(expected)
val actual = om.readValue[TraitOptionHolder](json)
actual shouldBe expected
}
}