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

NPE when deserializing JsonAnySetter in Throwable #4316

Closed
1 task done
jpraet opened this issue Jan 13, 2024 · 6 comments
Closed
1 task done

NPE when deserializing JsonAnySetter in Throwable #4316

jpraet opened this issue Jan 13, 2024 · 6 comments
Labels
2.16 Issues planned for 2.16 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Milestone

Comments

@jpraet
Copy link

jpraet commented Jan 13, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When using JsonAnyGetter+JsonAnySetter on an exception class, a NPE is thrown when deserializing.

Version Information

2.16.1

Reproduction

public class Main {

    static class Problem extends Exception {
        @JsonAnySetter
        @JsonAnyGetter
        Map<String, Object> additionalProperties = new HashMap<>();
    }

    public static void main(String[] args) throws JsonProcessingException {
        Problem problem = new Problem();
        problem.additionalProperties.put("additional", "additional");
        String json = new ObjectMapper().writeValueAsString(problem);
        System.out.println(json);
        Problem result = new ObjectMapper().readValue(json, Problem.class); // throws NPE
        System.out.println(result.additionalProperties);
    }
    
}

Expected behavior

No response

Additional context

Stacktrace:

Exception in thread "main" java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:233)
	at java.base/java.lang.invoke.DirectMethodHandle.checkBase(DirectMethodHandle.java:547)
	at java.base/jdk.internal.reflect.MethodHandleObjectFieldAccessorImpl.get(MethodHandleObjectFieldAccessorImpl.java:57)
	at java.base/java.lang.reflect.Field.get(Field.java:442)
	at com.fasterxml.jackson.databind.introspect.AnnotatedField.getValue(AnnotatedField.java:111)
	at com.fasterxml.jackson.databind.deser.SettableAnyProperty$MapFieldAnyProperty._set(SettableAnyProperty.java:347)
	at com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.java:205)
	at com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserializeAndSet(SettableAnyProperty.java:179)
	at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:153)
	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:4899)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3846)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3814)
	at be.fgov.kszbcss.jackson.Main.main(Main.java:25)

_anySetter.deserializeAndSet(p, ctxt, throwable, propName);

Note: It's specifically related to it being a Throwable. When I remove "extends Exception" it works.

@jpraet jpraet added the to-evaluate Issue that has been received but not yet evaluated label Jan 13, 2024
cowtowncoder added a commit that referenced this issue Jan 14, 2024
@cowtowncoder cowtowncoder added has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue and removed to-evaluate Issue that has been received but not yet evaluated labels Jan 14, 2024
@cowtowncoder
Copy link
Member

Thank you for reporting this: looks like a bug in ThrowableDeserializer: I can reproduce (and added a failing test).

@cowtowncoder cowtowncoder changed the title NPE when deserializing JsonAnySetter in Throwable NPE when deserializing JsonAnySetter in Throwable Jan 16, 2024
@cowtowncoder cowtowncoder added this to the 2.16.2 milestone Jan 16, 2024
@cowtowncoder cowtowncoder added the 2.16 Issues planned for 2.16 label Jan 16, 2024
@cowtowncoder
Copy link
Member

Fix is in 2.16 for 2.16.2 release.

@jpraet
Copy link
Author

jpraet commented Jan 17, 2024

For my concrete use case, there is still a problem.

I have @JsonIgnoreProperties({ "cause", "stackTrace", "response", "message", "localizedMessage", "suppressed" }) on my class, and the ThrowableDeserializer seems to depend on the "message" property being present in order to instantiate the object.

if (PROP_NAME_MESSAGE.equalsIgnoreCase(propName)) {
throwable = _instantiate(ctxt, hasStringCreator, p.getValueAsString());

@JooHyukKim
Copy link
Member

For my concrete use case, there is still a problem

Could u elaborate further what the problem is?

@jpraet
Copy link
Author

jpraet commented Jan 17, 2024

For my concrete use case, there is still a problem

Could u elaborate further what the problem is?

Still the same NPE when calling the any setter because the thowable is not initialized.

reproducer:

public class Main {

    @JsonIgnoreProperties({ "cause", "stackTrace", "response", "message", "localizedMessage", "suppressed" })
    static class Problem extends Exception {
        @JsonAnySetter
        @JsonAnyGetter
        Map<String, Object> additionalProperties = new HashMap<>();
    }

    public static void main(String[] args) throws JsonProcessingException {
        Problem problem = new Problem();
        problem.additionalProperties.put("additional", "additional");
        String json = new ObjectMapper().writeValueAsString(problem);
        System.out.println(json);
        Problem result = new ObjectMapper().readValue(json, Problem.class); // throws NPE
        System.out.println(result.additionalProperties);
    }

}

@JooHyukKim
Copy link
Member

JooHyukKim commented Jan 17, 2024

Still the same NPE when calling the any setter because the thowable is not initialized.

Seems like case No.2 that should be covered... this maybe covered by #4326 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.16 Issues planned for 2.16 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Projects
None yet
Development

No branches or pull requests

3 participants