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

Empty String deserialized as null instead of empty string #130

Closed
iulianrosca opened this issue Apr 25, 2019 · 4 comments
Closed

Empty String deserialized as null instead of empty string #130

iulianrosca opened this issue Apr 25, 2019 · 4 comments
Labels
yaml Issue related to YAML format backend
Milestone

Comments

@iulianrosca
Copy link

iulianrosca commented Apr 25, 2019

Hello,

I have the following issue/question for which I was hoping to find a easy solution.

I have inherited a large codebase where I am trying to migrate from jackson 2.6.4 to 2.9.8. The project contains multiple YAML files which are deserialized using com.fasterxml.jackson.dataformat.yaml.YAMLMapper. Most of these files contain empty fields e.g.

emptyStringField:

With version 2.6.4 these fields were being decoded to empty strings, while after migrating to 2.9.8 the field evaluated to null. I have managed to trace the diverging behavior in com.fasterxml.jackson.dataformat.yaml.YAMLParser#_decodeScalar.
The 2.6 version was evaluating the field to JsonToken.VALUE_STRING while the 2.9 version is using SnakeYaml implicit resolvers and evaluates to JsonToken.VALUE_NULL.

What is the desirable way to keep the behavior from 2.6, deserialize empty fields to empty string instead of null, after migrating to 2.9?

@cowtowncoder
Copy link
Member

Hmmh. I do not have a good answer here, but "Null replacement/error/skipping" introduced in 2.9:

https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff

would at least allow coercing null into "" in general. It can be enabled for String valued fields, or specific fields.

@iulianrosca iulianrosca changed the title (yaml) Empty field deserialized to empty string instead of null (yaml) Empty field deserialized to null instead of empty string Apr 26, 2019
@stefanleh
Copy link

Please also check ... #116

@cowtowncoder
Copy link
Member

Finally got around to have another look: fix seems easy, but timing is bit challenging as we are past last RC for 2.12.0. I think, however, that I can add a YAMLParser.Feature, defaulting to false (for 2.12) to allow restoring behavior.

cowtowncoder added a commit that referenced this issue Nov 24, 2020
@cowtowncoder cowtowncoder changed the title (yaml) Empty field deserialized to null instead of empty string Empty String deserialized as null instead of empty string Nov 24, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Nov 24, 2020
@cowtowncoder
Copy link
Member

Fixed this by adding a new feature, YAMLParser.Feature.EMPTY_STRING_AS_NULL, which defaults to true for backwards-compatibility with Jackson 2.11.
It can be disabled to prevent coercion from empty String into null in 2.12; we may consider changing default behavior in a later 2.x version as well as 3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

3 participants