problem icon indicating copy to clipboard operation
problem copied to clipboard

Deserialization of AbstractThrowableProblem implementation fails with InvalidTypeIdException

Open FRosner opened this issue 1 year ago • 2 comments

Description

I defined a custom problem ResourceNotFoundProblem and I was checking it in my unit test. However, the object mapper failed to deserialize it with the following exception:

com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'https://your-company.com/problems/ResourceNotFoundProblem.html as a subtype of `ResourceNotFoundProblem`: known type ids = [ResourceNotFoundProblem]
 at [Source: (String)"{"id":"cd892175-ea84-4329-a3a0-aa7c40155bda","type":"https://your-company.com/problems/ResourceNotFoundProblem.html","title":"Resource not found","status":404,"detail":"Task cd892175-ea84-4329-a3a0-aa7c40155bda not found. Did you submit it recently? It may have expired from the result cache, or you might have submitted it to a different process."}"; line: 1, column: 53]

I don't know why, because I did not provide any JsonTypeInfo property. Here's my class definition:

public final class ResourceNotFoundProblem extends AbstractThrowableProblem
{

    static final URI TYPE = URI.create(String.format("https://your-company.com/problems/%s.html", ResourceNotFoundProblem.class.getSimpleName()));

    public final String id;

    public ResourceNotFoundProblem(final String id, final String detail) {
        super(TYPE, "Resource not found", Status.NOT_FOUND, detail);
        this.id = id;
    }

}

And here's a sample to reproduce:

String ser = OBJECT_MAPPER.writeValueAsString(new ResourceNotFoundProblem("id", "detail"));
ResourceNotFoundProblem deser = OBJECT_MAPPER.readValue(ser, ResourceNotFoundProblem.class);

Here's my object mapper.

public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS, true)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .registerModule(new JavaTimeModule())
            .registerModule(new Jdk8Module())
            .registerModule(new ProblemModule());

Expected Behavior

I expect the deserialization to work.

Actual Behavior

The deserialization does not work.

Possible Fix

Not sure what the root cause is. Is this expected to work?

Steps to Reproduce

String ser = OBJECT_MAPPER.writeValueAsString(new ResourceNotFoundProblem("id", "detail"));
ResourceNotFoundProblem deser = OBJECT_MAPPER.readValue(ser, ResourceNotFoundProblem.class);

Context

I can't test my code.

Your Environment

  • Problem 0.27.1
  • Jackson databind 2.14.2

FRosner avatar Jan 22 '24 12:01 FRosner

Bump. I am experiencing the same issue trying to deserialize a Zalando problem to extract the error details.

A3Ackerman avatar Apr 17 '24 21:04 A3Ackerman

I believe you have to register your type (using ObjectMapper.registerSubtypes(...)) as a subtype so that Jackson knows about when when deserializing.

whiskeysierra avatar Apr 18 '24 07:04 whiskeysierra