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

constructorDetector seems to invalidate defaultSetterInfo for nullability #3241

Closed
joca-bt opened this issue Aug 12, 2021 · 7 comments
Closed
Labels
2.18 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue property-discovery Problem with property discovery (introspection)
Milestone

Comments

@joca-bt
Copy link

joca-bt commented Aug 12, 2021

Was trying out the new constructor detector feature (https://cowtowncoder.medium.com/jackson-2-12-most-wanted-3-5-246624e2d3d0/) and stumbled upon the following issue.

I had this code which forbids nulls on deserialization:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

public class Input {
    private final Boolean field;

    @JsonCreator
    public Input(Boolean field) {
        this.field = field;
    }
}

ObjectMapper objectMapper = JsonMapper.builder()
    .addModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES))
    .defaultSetterInfo(JsonSetter.Value.construct(Nulls.FAIL, Nulls.FAIL))
    .build();

objectMapper.readValue("{ \"field\": null }", Input.class); // throws InvalidNullException, as expected

and was trying to get rid of @JsonCreator with the following

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

public class Input {
    private final Boolean field;

    // @JsonCreator gone!
    public Input(Boolean field) {
        this.field = field;
    }

    public Boolean field() {
      return field;
    }
}

ObjectMapper objectMapper = JsonMapper.builder()
    .addModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES))
    .constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED) // new!
    .defaultSetterInfo(JsonSetter.Value.construct(Nulls.FAIL, Nulls.FAIL))
    .build();

objectMapper.readValue("{ \"field\": null }", Input.class); // no error, field is assigned to null

Although I no longer need to annotate my class with @JsonCreator, my settings for nullability are ignored, which means I can't really use the new feature.

(Might be related with #3227?)

@joca-bt joca-bt added the to-evaluate Issue that has been received but not yet evaluated label Aug 12, 2021
@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Aug 25, 2021
@cowtowncoder
Copy link
Member

cowtowncoder commented Aug 25, 2021

Yes, I can reproduce this. Strange... It is possible this is due to implicit detection of constructors missing some information that would be discovered by explicitly marker ones.

cowtowncoder added a commit that referenced this issue Aug 25, 2021
@cowtowncoder cowtowncoder added 2.13 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue labels Aug 25, 2021
@joca-bt
Copy link
Author

joca-bt commented Nov 29, 2021

This is particularly annoying when using records as we need to do something like:

public record Input(Boolean field) {
    @JsonCreator
    public Input {}
}

Do you think this fix can be included in 2.14?

@cowtowncoder
Copy link
Member

As soon as someone provides a fix, yes, it could probably be included in a 2.13.x patch as well. I haven't had and probably won't have time to work in this in near future.

@cowtowncoder cowtowncoder added the property-discovery Problem with property discovery (introspection) label Jun 13, 2022
@cowtowncoder
Copy link
Member

Sigh. Yes, I think this is because of the disprepancy between merging of annotations between explicitly annotated and implicitly located Creators: this is a long-standing general problem that manifests in many different ways.

On plus side: if and when property introspection gets rewritten (it is #1 top Big Thing for me to fix) this problem will be resolved.
On downside: I have not been able to find enough time to start the rewrite.

@cowtowncoder cowtowncoder added 2.15 and removed 2.13 labels Dec 3, 2022
@yihtserns
Copy link
Contributor

This is particularly annoying when using records as we need to do something like:

public record Input(Boolean field) {
   @JsonCreator
   public Input {}
}

@cowtowncoder cowtowncoder removed the 2.15 label May 5, 2024
@cowtowncoder
Copy link
Member

Work on #4515 likely makes it possible to solve this for 2.18.

@cowtowncoder cowtowncoder changed the title constructorDetector seems to invalidate defaultSetterInfo for nullability constructorDetector seems to invalidate defaultSetterInfo for nullability May 30, 2024
@cowtowncoder cowtowncoder added this to the 2.18.0 milestone May 30, 2024
@cowtowncoder
Copy link
Member

Fixed via #4515, to be included in 2.18.0.

cowtowncoder added a commit that referenced this issue May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.18 has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue property-discovery Problem with property discovery (introspection)
Projects
None yet
Development

No branches or pull requests

3 participants