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

Parsing of null Integer fields changed behavior between version 2.11.4 and 2.12.X #473

Closed
Crim opened this issue May 2, 2021 · 7 comments
Milestone

Comments

@Crim
Copy link

Crim commented May 2, 2021

Running version 2.11.4 using the following configuration:

        // Create new mapper
        final JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(false);

        final XmlMapper mapper = new XmlMapper(module);

        // Configure it
        mapper
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
            .registerModule(new JodaModule())
            .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

Parsing the following xml:

        <campaign>
            <id>123</id>
            <name>Website Tracking</name>
            <cost/>
        </campaign>

into the following bean:

public class Campaign {
    private Long id;
    private String name;
    private Integer cost;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getCost() {
        return cost;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setCost(final Integer cost) {
        this.cost = cost;
    }
}

usage:

  // mapper instance as defined above, campaignXmlStr as defined above.
  mapper.readValue(campaignXmlStr, Campaign.class);

The above JSON gets deserialized into the bean with the cost property being set to null.

With version 2.12.x the cost property becomes integer 0

I reviewed the release notes and didn't notice any breaking change listed in behavior. Is this change expected?

Thanks!

@cowtowncoder
Copy link
Member

This is XML-specific, so will move (jackson-core is for streaming API and JSON parsing).

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-core May 2, 2021
@cowtowncoder
Copy link
Member

I'll have to think about this for a bit -- change itself would not have been planned, but handling of "empty" values with XML is rather tricky as conceptually

<element />

is identical to

<element></element>

and in both cases logical textual content to process is empty String (""). Handling of that did change internally to allow dealing with this case for POJOs (to end up with "empty" Objects).

But I can see how such values should ideally become either null for Number wrappers or, in case of primitives like int, probably result in exception.

So I think that this change is a side effect of legitimate fixes for non-scalar types.

@Crim
Copy link
Author

Crim commented May 2, 2021

Yea, I suspect since the intent is ambiguous from just looking at only the XML (does empty mean null? or 0? or ...?) there's no "right" answer, other than to be opinionated one way or the other (and consistent) with how it's handled.

That said, it might be worth noting the change of behavior in the release notes.

Thanks! :)

@cowtowncoder
Copy link
Member

@Crim fully agree wrt release notes for 2.12 at very least. I have an idea of how this might be relatively simple different wrt new-ish "coercion config" settings for XML module, but if so, how to change things 2.12 -> 2.13 (and if....) is the big question.

cowtowncoder added a commit that referenced this issue May 4, 2021
cowtowncoder added a commit that referenced this issue May 4, 2021
@cowtowncoder cowtowncoder changed the title Parsing of null Integer fields changed behavior between version 2.11.4 and 2.12.X Parsing of null Integer fields changed behavior between version 2.11.4 and 2.12.X May 4, 2021
@cowtowncoder cowtowncoder added this to the 2.12.4 milestone May 4, 2021
@cowtowncoder
Copy link
Member

Actually I think I can fix the reported issue for 2.12.4; also created a related issue #474.

@cowtowncoder
Copy link
Member

Should be fixed for integer/floating-point wrapper types as well as Boolean now, once 2.12.4 gets released (and 2.13.0).

@Crim
Copy link
Author

Crim commented May 4, 2021

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants