Skip to content

Commit

Permalink
fix issue with null handling in option deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Apr 4, 2023
1 parent 48d06fd commit eb55d7a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private class OptionDeserializer(fullType: JavaType,
val deser = valueDeserializer.getOrElse(ctxt.findContextualValueDeserializer(fullType.getContentType, beanProperty.orNull))
val refd: AnyRef = valueTypeDeserializer match {
case Some(vtd) => deser.deserializeWithType(p, ctxt, vtd)
case None => deser.deserialize(p, ctxt)
case None if p.getCurrentToken == JsonToken.VALUE_NULL => null

This comment has been minimized.

Copy link
@cowtowncoder

cowtowncoder Apr 4, 2023

Member

That's the idea, although for optimal handling there'd be call to JsonDeserializer.getNullValue(ctxt); for possible null replacement.

And for actual fail-on-null (or convert to "empty") there's more set up (see jackson-databind CollectionDeserializer or src/main/java/com/fasterxml/jackson/databind/deser/std/ReferenceTypeDeserializer.java) to be done.

case _ => deser.deserialize(p, ctxt)
}
Option(refd)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class TupleDeserializerTest extends DeserializerTest {
result shouldEqual value
}

it should "deserialize an OptionalTupleHolder with nulls" in {
val value = OptionalTupleHolder(None, None)
val json = newMapper.writeValueAsString(value)
val result = deserialize(json, classOf[OptionalTupleHolder])
result shouldEqual value
}

it should "deserialize an OptionalTupleHolder2 with nulls" in {
val value = OptionalTupleHolder2(None, None)
val json = newMapper.writeValueAsString(value)
Expand Down

0 comments on commit eb55d7a

Please sign in to comment.