Allow disabling Integer to String coercion via `CoercionConfig`
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:7
- Comments:18 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 forStringDeserializer
(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.
@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.