Skip to content

Proposal to split scalar coercion into more granular features #4950

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

Closed
ivamly opened this issue Feb 5, 2025 · 6 comments
Closed

Proposal to split scalar coercion into more granular features #4950

ivamly opened this issue Feb 5, 2025 · 6 comments
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@ivamly
Copy link

ivamly commented Feb 5, 2025

Is your feature request related to a problem? Please describe.

Currently, the ALLOW_COERCION_OF_SCALARS feature in MappingFeature allows coercions from secondary representations for simple scalar types.
However, there are cases where certain coercions can lead to unintended or undesirable behavior. For example:

  • Preventing coercion of int to boolean would help avoid confusing deserialization results.
  • On the other hand, allowing coercion from String to BigDecimal is frequently required in many scenarios.

Describe the solution you'd like

It would be beneficial to either:

  • Split the existing feature into more specific flags or
  • Offer more granular configuration options for scalar coercion, allowing users to precisely control which type conversions are allowed (e.g., permitting coercion from String to BigDecimal but preventing coercion to boolean).

This would provide users with finer control over deserialization behavior and greater flexibility for different use cases.

Usage example

No response

Additional context

If there is already a way to achieve this level of control with the current configuration options, I would appreciate further clarification.

@ivamly ivamly added the to-evaluate Issue that has been received but not yet evaluated label Feb 5, 2025
@pjfanning
Copy link
Member

pjfanning commented Feb 5, 2025

For backward compatibility, we should not change the existing ALLOW_COERCION_OF_SCALARS config. We can add more to support more granular control. Can you provide a detailed proposal or a stub PR?

@cowtowncoder
Copy link
Member

Note that ALLOW_COERCION_OF_SCALARS is sort of legacy setting and more gradular "Coercion Config" settings (which can define type-to-type rules) is the way to do exactly what is asked. It is not Scala module specific.
Not all coercions necessarily check for coercion config settings, as work is incremental, but more cases are covered over time.

For usage, jackson-databind tests are best, but also see:

@ivamly
Copy link
Author

ivamly commented Feb 6, 2025

Hi @pjfanning @cowtowncoder ! Thanks for your quick feedback and suggestions!

I had a case where I needed to deserialize a String as BigDecimal, but I wanted to prevent converting int to boolean. When I found ALLOW_COERCION_OF_SCALARS, I encountered an issue. It fixed the problem with boolean values, but it stopped working for string conversions as well.

To be honest, I didn't know about the possibilities of CoercionConfig. After reviewing the links you shared, I was able to set up exactly what I needed.

Here’s a link with an example of the issue I ran into, along with tests showing how ALLOW_COERCION_OF_SCALARS didn’t work in my case, and how CoercionConfig helped.

@cowtowncoder
Copy link
Member

@ivamly Yes, documentation is still a weak area -- so much functionality exists that it is hard to know what all is available. Glad you were able to figure it out & thank you for sharing the link so other might find it too!

@ivamly
Copy link
Author

ivamly commented Feb 6, 2025

@cowtowncoder Thank you! Should I close this issue, or are there any other actions I should take?

@cowtowncoder
Copy link
Member

I can close. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

3 participants