You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have defined a REST element in our project which contains attributes with primitive int & long . I am using Jersey 2.17 During testing when I tried to pass a "" string, the REST client did not report any validation errors, but in the rest resource, I get the value of the primitive as the default value 0. This behavior is very odd as an empty string should not be accepted in this case. How do I make the request fail with validation errors.
My JSON request:
JSONObject employee = new JSONObject("{name : James}");
employee .put("age", "");
Response :
POST -><http://134.141.206.113:8080/Employee >
data: {"name":"James","age":""}
Status :201 output: {
"name" : "James",
"age" : 0
}
As part of DeserializationFeature, should there be an option to not allow empty string for numeric data types ?
The text was updated successfully, but these errors were encountered:
@MohamedNawaz Most users seem to prefer coercion since there are many non-Java clients that send all kinds of unexpected values: for example, some languages have no boolean type and send numbers; sometimes boolean values are T and F and so on. Similarly missing values may or may not exist and such values are sent as empty Strings.
However, others users agree with you in that they prefer strict match for values; problem mostly being question of exactly how strict (where are coercions allowed etc).
There are a few related existing DeserializationFeatures:
FAIL_ON_NULL_FOR_PRIMITIVES
FAIL_ON_NUMBERS_FOR_ENUMS
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT
none of which is exactly like what you ask.
But there is #1106 which I think is what you are asking? I was hoping to implement this for 2.9.
In the meantime, the one workaround that is possible is to define setter with type Object, in which case mapping for "" would be to String, and from actual numbers to Integer (or Long, Double etc depending on magnitude and int/float). If so you could choose validation yourself.
Actually, now DeserializationFeature. FAIL_ON_NULL_FOR_PRIMITIVES will also cover case of coercion from empty String, so you can enable it to get failure you want.
Fix was for #403 which ensures that primitive arrays follow same rules.
I have defined a REST element in our project which contains attributes with primitive int & long . I am using Jersey 2.17 During testing when I tried to pass a "" string, the REST client did not report any validation errors, but in the rest resource, I get the value of the primitive as the default value 0. This behavior is very odd as an empty string should not be accepted in this case. How do I make the request fail with validation errors.
My JSON request:
JSONObject employee = new JSONObject("{name : James}");
employee .put("age", "");
Response :
POST -><http://134.141.206.113:8080/Employee >
data: {"name":"James","age":""}
Status :201 output: {
"name" : "James",
"age" : 0
}
As part of DeserializationFeature, should there be an option to not allow empty string for numeric data types ?
The text was updated successfully, but these errors were encountered: