-
-
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
Extend DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT to cover scalar type Objects #3941
Comments
Thanks @cowtowncoder, but I am not 100% sure how do I apply this without writing my own deserializer (something I am trying to avoid). In a nutshell, I'd like any conversion from String to "numbers" to be successful if the string is a valid number (that also includes "marginal" cases, where float to integers is also permitted, i.e. "6.5" to integer returns "6"), except if it is an empty string (""), in which case an exception should be thrown and not convert this to a null, which is what DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT does, but not for scalar objects (Integer, Double etc). I don't see how that can be achieved with coercion config. It goes without saying that null values that are specified explicitly (e.g. "numberProperty: null") should also be accepted. |
The above pretty much did it, so no need to do anything special and it worked out of the box to `perfection! |
Should we close this is completed, @GI-2023 ? |
Yes please - the coersion config (something I wasn't aware of existed) works to perfection, so, yes, no need to do anything. Thank you for your support on this. |
Glad it all worked out @GI-2023 !👍🏻 Btw, you can close this issue yourself, since you opened it 👍🏻😄 |
Ah, sure. apologies. Closing... |
Oh no, I didn't mean anything that need apologies 🥹. I would've closed it for you if I had permission 👍🏻 |
Describe the bug
As stated in issue #1588 (further discussions in #1095 and #1106), there is currently no way to prevent mapping of an empty string ("") to Java "numbers" (java.lang.*: e.g. java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Double), - this always gets converted to null.
In other words, there is no difference between specifying (null) and ("") and there is no way to distinguish between the two.
Disabling MapperFeature.ALLOW_COERCION_OF_SCALARS is of no use, as it prevents valid numbers specified in a string to be processed (e.g. ""6"").
Version information
2.15.0
To Reproduce
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
public class Test {
}
Current behavior
java.lang.Integer
value (but might if coercion usingCoercionConfig
was enabled)Expected behavior
The text was updated successfully, but these errors were encountered: