-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Allow disabling Integer to String coercion via CoercionConfig
#3013
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 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. |
Is there an ETA for 2.13? |
@stephenvsp Not really; I would not expect it before maybe June-July 2021, ideally it'd be relatively small increase but it will have to compete with 3.0 development work. |
After it success created. |
I use following walk-around for 2.12: Create custom deserializer public class CoercionLessStringDeserializer extends StringDeserializer {
@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
List<JsonToken> forbiddenTypes =
Arrays.asList(
JsonToken.VALUE_NUMBER_INT,
JsonToken.VALUE_NUMBER_FLOAT,
JsonToken.VALUE_TRUE,
JsonToken.VALUE_FALSE);
if (forbiddenTypes.contains(p.getCurrentToken())) {
String message =
MessageFormat.format("Cannot coerce {0} to String value", p.getCurrentToken());
throw MismatchedInputException.from(p, String.class, message);
}
return super.deserialize(p, ctxt);
}
} And use it with SimpleModule deserializerModule = new SimpleModule();
deserializerModule.addDeserializer(
String.class, new CoercionLessStringDeserializer());
mapper.registerModule(deserializerModule); More advanced approach would be to use |
Thank you @WojciechLuczkow for showing so anyone who needs a work-around now can use it. |
For those who want to use this on a field of a model, you can also use the |
Thank you for sharing @Soggywaters! I also marked this with "pr-welcome" since I probably won't have time to work on this soon, but think it should be quite doable if anyone else wanted to have a go. And I can help getting PR ready and merged even if not having time to implement it myself. |
Hi @cowtowncoder, Thank you for all that you do for this project. About this issue - I would love to help where I can but does it really make sense to add this as a feature? I think the solution above works well for the problem (creating a deserializer extended from the string deserializer and leveraging the @JsonDeserialize annotation). Unless, you're talking about a simple annotation, like |
@Soggywaters it would not require any API addition but application of existing I agree that a custom |
Hi all, I gave it my best shot in this branch: 2.14...Tomasito665:disable-int-to-string-coercion. |
@Tomasito665 There is some code regarding |
If we are specifically talking about Integer-to-String coercion, neither |
Thanks for the feedback, both. @WojciechLuczkow I am not sure whether I can use |
@cowtowncoder Thats right - this issue is regarding Integer to String coercion, but as far I remember deserializer was also doing unexpected coercion for
and
@Tomasito665 You are right, sorry for making a fuss :) |
@WojciechLuczkow Fair enough. Might be most economical to tackle other issues as well, if @Tomasito665 wants to do that. And if so, update issue title here. EDIT: looking at code, I suggest we get this merged in first, then whoever wants can do PR for floating-point number and/or boolean case. |
@cowtowncoder sure! I'll see if I can include your suggestion on my PR to simplify the code tomorrow and file a separate PR for floats and booleans some time later during this weekend. Shall I link those to this issue or would you prefer to create a new one? |
CoercionConfig
@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. |
I am sure it's uninteded, but the questions sound like way too demanding for the questionee, asking for answers and explanations. 🤔 May I ask you to please file another issue @EarthCitizen and maybe link this issue back? There are "TEMPLATES" for requests, not like "I don't think this should be this way or that". All has been developed as intended. |
Please know that I am not speaking on behalf of anybody, but just making an observation here. Thinking maybe the word "demanding" was not the best choice. Question in its nature demands answers. What I saw problem is not how it is asked, but the question itself. You are right that the library is used by many people. Imagine that many people come and ask (I am sure politetely)
No context, no structure, not in the best form that can grow into a something meaningful. Here we have a similiar question on a closed Github issue, again, no context no structure.
It is great that you are contributing by sharing opinions, but would make a difference if it is more structured. With reasons, explanations, background and etc all in issue templates. Following is what I wanted to say :
|
Hi, @cowtowncoder, I have an unusual requirement: to simply log any type mismatches* (like the one OP described), but not fail the deserialization. Can you suggest what would be the easiest way to do that with Jackson? *basically what's covered by CoercionInputShape |
Please refrain from starting a usage question on a closed issue, it hurts maintenance costs @PrimevalCoder |
Aside from what @JooHyukKim suggested (esp. GH discussions), the only thing that comes close would be registering This won't cover all problems, even for |
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
andInteger
.Version information
v2.12.1
To Reproduce
I have class Example (using Lombok annotations):
The ObjectMapper setup:
How I try to deserialize:
Expected behavior
I expect an exception to be thrown saying that
Integer
cannot be converted toString
. But what happens is thatexample
is successfully created withtype
set to"123"
which means there was conversion.Is there anything wrong I do (maybe with the coercion configuration)?
The text was updated successfully, but these errors were encountered: