Skip to content
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

jackson json parser accepting empty single quotes for int primitives #1526

Closed
MohamedNawaz opened this issue Feb 13, 2017 · 2 comments
Closed

Comments

@MohamedNawaz
Copy link

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 ?

@cowtowncoder
Copy link
Member

@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.

@cowtowncoder
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants