Skip to content
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

Subclassed Throwable deserialization fails since v2.18.0 - no creator index for property 'cause' #4827

Open
1 task done
nilswieber opened this issue Dec 3, 2024 · 2 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@nilswieber
Copy link

nilswieber commented Dec 3, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

The deserialization of Throwables fails if they are subclassed without a no-arg constructor.

Version Information

Seems to affect versions 2.18.0 - 2.18.2

Reproduction

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SubclassedExceptionJava extends Exception {
    @JsonCreator
    SubclassedExceptionJava(@JsonProperty("message") String message, @JsonProperty("cause") Throwable cause){
        super(message, cause);
    }


    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper()
                .configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        SubclassedExceptionJava exceptionToSerialize = new SubclassedExceptionJava("Test Message", null);
        String serialized = mapper.writeValueAsString(exceptionToSerialize);

        System.out.println(serialized);
        
        mapper.readValue(serialized, SubclassedExceptionJava.class);
        //Exception in thread "main" java.lang.IllegalStateException: Internal error: no creator index for property 'cause' (of type com.fasterxml.jackson.databind.deser.impl.MethodProperty)
    }
}

The following exception is thrown:

Exception in thread "main" java.lang.IllegalStateException: Internal error: no creator index for property 'cause' (of type com.fasterxml.jackson.databind.deser.impl.MethodProperty)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.getCreatorIndex(SettableBeanProperty.java:450)
	at com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer.assignParameter(PropertyValueBuffer.java:363)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:449)
	at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:85)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4917)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3828)
	at SubclassedExceptionJava.main(SubclassedExceptionJava.java:21)

Expected behavior

In versions before 2.18.0 the described code worked flawlessly and the Exception is parsed.

Additional context

If a no arg constructor is added, the example works (even for versions 2.18.0 - 2.18.2)

@nilswieber nilswieber added the to-evaluate Issue that has been received but not yet evaluated label Dec 3, 2024
@cowtowncoder
Copy link
Member

cowtowncoder commented Dec 4, 2024

First of all, thank you for reporting this issue. It definitely looks like a regression.

Quick note: what happens if you remove

.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

?

I am asking since this is the very first thing that should always be removed from reproduction, as it tends to hide problems.

@nilswieber
Copy link
Author

If you remove this configuration:

  • For versions > 2.18.0 the exception is the same.
  • But versions <= 2.17.3 will throw a com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "suppressed" (class util.SubclassedExceptionJava), not marked as ignorable (3 known properties: "cause", "stackTrace", "message"]) because there are only setters / constructors in Throwable for cause, stackTrace and message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants