question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow disabling Integer to String coercion via `CoercionConfig`

See original GitHub issue

Describe the bug

I am experiencing issues with the new coercion settings that were released in v2.12.0. I would like to disable coercion between String and Integer.

Version information

v2.12.1

To Reproduce

I have class Example (using Lombok annotations):

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Example {
    private String type;
}

The ObjectMapper setup:

ObjectMapper mapper = new ObjectMapper();
mapper.coercionConfigFor(LogicalType.Textual)
        .setCoercion(CoercionInputShape.Integer, CoercionAction.Fail);

How I try to deserialize:

Example example = mapper.readValue("{\"type\": 123}", Example.class);

Expected behavior

I expect an exception to be thrown saying that Integer cannot be converted to String. But what happens is that example is successfully created with type set to "123" which means there was conversion.

Is there anything wrong I do (maybe with the coercion configuration)?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
cowtowncodercommented, Jan 16, 2021

@e1-emilkostadinov Thank you for reporting this! Your usage of the feature looks correct so that is not the problem.

Up until now coercions from other scalar types to String have been automatically allowed but with the new coercion-config settings it should be possible to prevent this. This requires some work for StringDeserializer (and perhaps couple of other related deserializers) to add applicable checks.

So this is just a case of missing checks and should be implemented: I mark it for 2.13 as the work would most likely be down when starting to consider features to add in 2.13, but it could probably be backported in 2.12 as well if change seems safe enough.

1reaction
cowtowncodercommented, Oct 3, 2022

@Tomasito665 On issues, please create new one so PRs link nicely one-to-one. You may combine multiple new types (boolean and float) that’s fine, but since I merged and closed this issue let’s not link more changes to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson 2.12 Most Wanted (4/5):. CoercionConfig system
ALLOW_COERCION_OF_SCALARS controls whether “number as JSON String” coercion is allowed for ... Solution: configurable “CoercionConfig”s.
Read more >
DeserializationConfig (jackson-databind 2.12.0 API)
General-purpose accessor for finding what to do when specified coercion from shape that is now always allowed to be coerced from is requested....
Read more >
ObjectMapper (jackson-databind 2.14.1 API) - javadoc.io
Method for removing all registered DeserializationProblemHandler s instances from this mapper. MutableCoercionConfig · coercionConfigDefaults(). Accessor for ...
Read more >
Security update for jackson-databind, jackson-dataformats ...
writeString (Reader, int)' + Allow "optional-padding" for ... "Enum from integer" coercion using new 'CoercionConfig' system + '@JsonValue' ...
Read more >
com.fasterxml.jackson.databind.deser.std.StdDeserializer ...
InputCoercionException ; import com.fasterxml.jackson.core.io. ... exception (if no coercion allowed), or returns appropriate * result value using coercion ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found