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

Deserialization of unknown value for enums does not yield default enum value #4403

Closed
1 task done
dominik-henning opened this issue Feb 29, 2024 · 6 comments
Closed
1 task done
Labels
has-failing-test Indicates that there exists a test case (under `failing/`) to reproduce the issue
Milestone

Comments

@dominik-henning
Copy link

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

i have a class containing a field of an enum type. The data is fetched from an api that can add further possible enum values at any point. I want to guard my implementation by using the JsonEnumDefaultValue.

During the deserialization of an unknown value, instead of the annotated default, a different instance of this enum is returned.

Version Information

2.16

Reproduction

https://github.com/dominik-henning/jackson-databind-enum-deserialization-reproducer

i created a projected containing a test class which shows my usecase and setup.

Expected behavior

When an value is encountered which does not match to a annotated JsonProperty, i would expect that the JsonEnumDefaultValue is returned.

Additional context

The real world example is different, but the project boils the problem down to the bare minimum.

@dominik-henning dominik-henning added the to-evaluate Issue that has been received but not yet evaluated label Feb 29, 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 Mar 3, 2024
@cowtowncoder
Copy link
Member

Ok, yes, I can reproduce: thank you for providing the sample project.

The root cause: this happens because "001" is treated as index 1, before considering it "unknown" value.

I'll have to see what we can do here.

@cowtowncoder
Copy link
Member

@dominik-henning In the meantime you may want to enable

DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS

to prevent use of index (number) values for Enum deserialization.
That will make things work as expected here.

@dominik-henning
Copy link
Author

Thank you @cowtowncoder for the hint. I enabled the feature and now it works like a charm.

@cowtowncoder
Copy link
Member

Good! I hope I can find other minor improvements but at least you can use configuration to avoid the problem.

@cowtowncoder cowtowncoder changed the title Deserialization of unknown value for enums does not yield default enum value. Deserialization of unknown value for enums does not yield default enum value Mar 8, 2024
@cowtowncoder cowtowncoder added this to the 2.17.0 milestone Mar 8, 2024
@cowtowncoder
Copy link
Member

Ok, was able to add a minor improvement: will NOT consider Strings with leading 0 as legitimate number: so "001" will no longer be coerced into number.

Not quite as good as preventing (automatic) use of "Stringified" numbers, but thinking it helps. Will be in 2.17.0.

@dominik-henning
Copy link
Author

Thanks @cowtowncoder , appreciate it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

2 participants