Repository navigation
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 |
Gson.fromJson is documented to throw JsonSyntaxException for malformed input, but four groups of built-in adapters let raw NumberFormatException, IllegalArgumentException or NullPointerException escape to the caller. Callers that guard with catch (JsonParseException) do not catch these. This is the same defect class already fixed for AtomicLongArray in google#3038 and currently open for AtomicIntegerArray in google#3047 / google#3095. In each case the neighbouring target type already behaves correctly. - BitSet with a malformed number - null elements in primitive arrays (int[], double[], char[], ...) - double/float with a malformed number - null elements in collections that reject them (TreeSet, ArrayDeque, PriorityQueue, EnumSet)
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.