Skip to content

Commit

Permalink
Fix #3091
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 27, 2021
1 parent d94c2de commit 9202d13
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project: jackson-databind
(reported by Asaf R)
#3082: Dont track unknown props in buffer if `ignoreAllUnknown` is true
(contributed by David H)
#3091: Should allow deserialization of java.time types via opaque
`JsonToken.VALUE_EMBEDDED_OBJECT`
#3099: Optimize "AnnotatedConstructor.call()" case by passing explicit null
#3101: Add AnnotationIntrospector.XmlExtensions interface for decoupling javax dependencies
#3117: Use more limiting default visibility settings for JDK types (java.*, javax.*)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
Expand All @@ -14,6 +15,9 @@
* This is used for "known unknown" types: types that we can recognize
* but can not support easily (or support known to be added via extension
* module).
*<p>
* NOTE: starting with 2.13, does allow deserialization from
* {@code JsonToken.VALUE_EMBEDDED_OBJECT} if type matches (or is {@code null}).
*
* @since 2.12
*/
Expand All @@ -32,7 +36,15 @@ public UnsupportedTypeDeserializer(JavaType t, String m) {
}

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
// 26-May-2021, tatu: For [databind#3091], do allow reads from embedded values
if (p.currentToken() == JsonToken.VALUE_EMBEDDED_OBJECT) {
Object value = p.getEmbeddedObject();
if ((value == null) || _type.getRawClass().isAssignableFrom(value.getClass())) {
return value;
}
}
ctxt.reportBadDefinition(_type, _message);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.util.TokenBuffer;

// [databind#2683]: add fallback handling for Java 8 date/time types, to
// prevent accidental serialization as POJOs, as well as give more information
Expand Down Expand Up @@ -42,4 +47,33 @@ public void testBetterDeserializationError() throws Exception
verifyException(e, "add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jsr310\"");
}
}

// But, [databind#3091], allow deser from JsonToken.VALUE_EMBEDDED_OBJECT
public void testAllowAsEmbedded() throws Exception
{
OffsetDateTime time = OffsetDateTime.ofInstant(Instant.now(),
ZoneId.of("Z"));
try (TokenBuffer tb = new TokenBuffer((ObjectCodec) null, false)) {
tb.writeEmbeddedObject(time);

try (JsonParser p = tb.asParser()) {
OffsetDateTime result = MAPPER.readValue(p, OffsetDateTime.class);
assertSame(time, result);
}
}

// but also try deser into an array
try (TokenBuffer tb = new TokenBuffer((ObjectCodec) null, false)) {
tb.writeStartArray();
tb.writeEmbeddedObject(time);
tb.writeEndArray();

try (JsonParser p = tb.asParser()) {
Object[] result = MAPPER.readValue(p, Object[].class);
assertNotNull(result);
assertEquals(1, result.length);
assertSame(time, result[0]);
}
}
}
}

0 comments on commit 9202d13

Please sign in to comment.