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

Prevent coercion of int from empty String to null if DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES is true #1095

Closed
yzmyyff opened this issue Jan 26, 2016 · 12 comments
Milestone

Comments

@yzmyyff
Copy link

yzmyyff commented Jan 26, 2016

I got 0 from the code below.

int i = mapper.readValue("\"\"", int.class);
System.out.println(i);

It seems that Json Number type cannot start with ".
Could I make the code throw some Exceptions?

@cowtowncoder
Copy link
Member

So you would not want empty String to be coerced into 'missing' value of '0'?

Perhaps a DeserializationFeature could be added to prevent this -- there may already exist an issue for this.

@yzmyyff
Copy link
Author

yzmyyff commented Jan 27, 2016

I tried to add DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT to my mapper, but it still print 0.

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
int i = mapper.readValue("\"\"", int.class);
System.out.println(i);

The right order in my mind is empty Json String -> Json Null -> Json Number -> primitive.
it seems that these 2 features don't take effect at the same time.

@cowtowncoder
Copy link
Member

@yzmyyff Correct, the features in question are handled separately. Or, I should say, coercion from empty String into scalars does NOT consider ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, as scalars are not (Java) Objects. It is confusing. I will think about this to see if adding checks as you suggest is the best way to go, or adding a new DeserializationFeature. I have some concerns about backwards compatibility, if changing existing behavior, but perhaps those are not valid.

@cowtowncoder
Copy link
Member

I think this would make sense. I elaborated bit more on #1106.

@yzmyyff
Copy link
Author

yzmyyff commented Jan 29, 2016

Thanks a lot.

@cowtowncoder cowtowncoder changed the title deserialize empty string to Number 0 Prevent coercion of int from empty String to null if DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES is true Jan 30, 2016
@cowtowncoder cowtowncoder added this to the 2.7.1 milestone Jan 30, 2016
@cowtowncoder
Copy link
Member

Implemented for 2.7.1; affects primitives, but not equivalent wrappers, as per naming of features.

@gkozyryatskyy
Copy link

gkozyryatskyy commented Mar 30, 2017

What should I do if i want this

Integer i = mapper.readValue("\"\"", Integer.class);
System.out.println(i);

to throw Exception?

@cowtowncoder
Copy link
Member

Was about to say that it works, but then realized you are talking about wrapper object.

DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT is the thing to disable if you do not want such coercion. Handling has been rewritten for 2.9 (not yet released, but 2.9.0.pr2 is available) and this will work; earlier versions had more limited support for some types. I do not know offhand if Integer handling was working or not.

@gkozyryatskyy
Copy link

gkozyryatskyy commented Mar 31, 2017

@cowtowncoder
Im using 2.8.6 and I try with 2.9.0.pr2
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, false);
And there is no exception... Empty string is successfully parsed as null Integer or Long.

@cowtowncoder
Copy link
Member

@gkozyryatskyy Ok I'll have a look; since this issue is closed, I'll file a new one.

@gkozyryatskyy
Copy link

@cowtowncoder
thx a lot!

@cowtowncoder
Copy link
Member

Ok actually I misspoke here. Due to backwards compatibility requirements, DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT can not be made to apply to scalar values: feature defaults to false, yet behavior of coercion is enabled by default.

So: #1106 will actually be needed here; feature to be added is MapperFeature.ALLOW_COERCION_OF_SCALARS and will be included in 2.9,0.pr3.

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

3 participants