Throw JsonSyntaxException instead of NullPointerException for a null AtomicLongArray element#3038
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Marcono1234
left a comment
There was a problem hiding this comment.
Thanks a lot!
This looks reasonable; though the direct project members might have additional comments on this.
Are you able to sign the CLA?
(As side note, while the adapter for AtomicLong has a similar code pattern, that one is most likely not affected because the longAdapter is fully controlled by Gson, so only a JSON null could lead to a Java null but the adapter is nullSafe().)
| JsonSyntaxException e = | ||
| assertThrows( | ||
| JsonSyntaxException.class, () -> gson.fromJson("[1,null,3]", AtomicLongArray.class)); | ||
| assertThat(e).hasMessageThat().contains("null"); |
There was a problem hiding this comment.
Given that the message is fully controlled by Gson, maybe it would be better to check the exact message with isEqualTo?
There was a problem hiding this comment.
Good idea — switched to isEqualTo("null is not a valid AtomicLongArray element") to assert the exact message. Thanks!
…lement Deserializing a JSON array with a null element into AtomicLongArray (e.g. [1,null,3]) called longValue() on the null returned by the element adapter, throwing a NullPointerException. Throw a JsonSyntaxException for the null element, consistent with how the Long adapter reports malformed numbers. Adds a regression test.
9993b74 to
a872de0
Compare
|
CLA is signed now. Also agree on |
Deserializing a JSON array that contains a
nullelement into anAtomicLongArraythrows a rawNullPointerException:The array adapter calls
longAdapter.read(in).longValue(), and the element adapter returnsnullfor a JSONnull, solongValue()is invoked onnull.This throws a
JsonSyntaxExceptionfor the null element instead, consistent with how theLongadapter already reports malformed numbers. Includes a regression test.