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 String fields changed behavior between version 2.11.4 and 2.12.x and newer #507

Closed
Crim opened this issue Dec 28, 2021 · 2 comments

Comments

@Crim
Copy link

Crim commented Dec 28, 2021

Almost the same as #473 , apologies for not catching the changed behavior of strings at the same time as posting that issue.

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/>
        </campaign>

into the following bean:

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

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

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

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 name property being set to null.

With version 2.12.x and newer the name property becomes "" (empty string)

Is this considered a bug? Or expected behavior for newer versions? Is there a way to revert to the previous behavior via config?

Thanks!

@cowtowncoder
Copy link
Member

There is setting

FromXmlParser.EMPTY_ELEMENT_AS_NULL

which may be relevant here. But other than that the new behavior is considered correct for String values: there really is no good way to indicate null in XML except by using xsi:nil="true" marker.
That marker is processed if FromXmlParser.PROCESS_XSI_NIL is true (which is the default setting).

@Crim
Copy link
Author

Crim commented Jan 4, 2022

Awesome, that worked perfectly. Thanks for sorting me out!

@Crim Crim closed this as completed Jan 4, 2022
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