question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot deserialize Throwables with Jackson databind 2.9.4 using @JsonIdentityInfo

See original GitHub issue

I have written a test case that reproduces this issue. Object mapper configuration:

`

private ObjectMapperAccess() {
    this.objectMapper = new ObjectMapper();

    /**
     * Generic configuration
     */
    this.objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    this.objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    this.objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

    /**
     * Configure deserialization features
     */
    this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    /**
     * Register mixins
     */
    this.objectMapper.addMixIn(Throwable.class, ThrowablelMixin.class);
    this.objectMapper.addMixIn(StackTraceElement.class, StackTraceElementMixin.class);
}`

Mixin definitions:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class) public abstract class ThrowablelMixin {}

public abstract class StackTraceElementMixin { // With this property name and getter name will be the same. @JsonProperty("className") private String declaringClass; }

Test case that fails:

`

private void generateException() {
    try {
        Integer.parseInt("invalid_int");
    } catch (NumberFormatException e) {
        e.printStackTrace();
        throw new RmiHttpRuntimeException("Could not parse", e);
    }
}

/**
 * Test mixin for StackTraceElement.
 */
@Test
public void testStackTraceElementAndThrowableMixin() throws IOException {
    try {
        generateException();
        Assert.fail(String.format("%s did not occur.", NumberFormatException.class.getName()));
    } catch (RmiHttpRuntimeException re) {
        re.printStackTrace();
        String json = objectMapper.writeValueAsString(re);
        System.out.println(json);
        RmiHttpRuntimeException pre = objectMapper.readValue(json, RmiHttpRuntimeException.class);
        Assert.assertFalse(pre.getStackTrace()[0].getClassName().isEmpty());
        //TODO figure out a way to de/serialize cause.
        Assert.assertNotNull(pre.getCause().getStackTrace()[0].getClassName());
    }
}`

Exception message and stacktrace of the above test case:

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Throwable]: missing type id property ‘@class’ (for POJO property ‘cause’) at [Source: (String)“{”@class":“com.netapp.oci.platform.http.RmiHttpRuntimeException”,“@id”:1,“detailMessage”:“Could not parse”,“cause”:{“@class”:“java.lang.NumberFormatException”,“@id”:2,“detailMessage”:“For input string: "invalid_int"”,“cause”:2,“stackTrace”:[{“methodName”:“forInputString”,“fileName”:“NumberFormatException.java”,“lineNumber”:65,“className”:“java.lang.NumberFormatException”},{“methodName”:“parseInt”,“fileName”:“Integer.java”,“lineNumber”:580,“className”:“java.lang.Integer”},{“methodName”:“parseIn”[truncated 6895 chars]; line: 1, column: 228]

at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at com.fasterxml.jackson.databind.DeserializationContext.missingTypeIdException(DeserializationContext.java:1638)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingTypeId(DeserializationContext.java:1217)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleMissingTypeId(TypeDeserializerBase.java:300)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedUsingDefaultImpl(AsPropertyTypeDeserializer.java:164)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:88)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1171)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:527)
at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:103)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:194)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:130)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1171)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:527)
at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:103)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:194)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:130)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1171)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
at com.netapp.oci.platform.http.jackson.TestObjectMapper.testStackTraceElementMixin(TestObjectMapper.java:116)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kreigercommented, Apr 10, 2019

cause of a Throwable points to itself. That is wrong and really should be fixed.

It is not wrong. It is the default behavior of Throwable when no cause is given.

From Throwable.java:

/**
 * The throwable that caused this throwable to get thrown, or null if this
 * throwable was not caused by another throwable, or if the causative
 * throwable is unknown.  If this field is equal to this throwable itself,
 * it indicates that the cause of this throwable has not yet been
 * initialized.
 *
 * @serial
 * @since 1.4
 */
private Throwable cause = this;

public synchronized Throwable getCause() {
    return (cause==this ? null : cause);
}
1reaction
cowtowncodercommented, Mar 30, 2018

@sholavanalli I think the main question is that of why @JsonIdentityInfo is being used. Would it be possible to just eliminate it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - MismatchedInputException when using Jackson ...
I am trying to Serialize and Deserialize a large object graph. ... I thought I would use Jackson's @JsonIdentityInfo annotation, which from ......
Read more >
Index (jackson-databind 2.10.4 API) - javadoc.io
Defines interface for resolvers that can resolve abstract types into concrete ones; either by using static mappings, or possibly by materializing ...
Read more >
Using @JsonIdentityInfo to handle circular references - LogicBig
@JsonIdentityInfo is used to handle circular reference of an object by serializing the back-reference's identifier rather than serializing ...
Read more >
MismatchedInputException (jackson-databind 2.9.0 API)
General exception type used as the base class for all JsonMappingException s that are due to input not mapping to target definition; these...
Read more >
More Jackson Annotations - Baeldung
It works in collaboration with @JsonIdentityInfo to force usage of object identities in every serialization, different from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found