How to fix the Sonar issues (returning null Maps) in JsonHelper? #686
sleberknight
started this conversation in
General
Replies: 1 comment
-
This will be addressed in 5.0.0 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The original
JsonHelper
implementation returnsnull
for the following methods:public Map<String, Object> toMap(@Nullable String json)
public <K, V> Map<K, V> toMap(@Nullable String json, TypeReference<Map<K, V>> targetMapType)
public <T> Map<String, T> toFlatMap(@Nullable Object object, Class<T> valueClass)
public <K, V> Map<K, V> convertToMap(Object fromObject, TypeReference<Map<K, V>> targetMapType)
Sonar is unhappy that we're returning
null
for a collection return type. I agree. I did some research intonull
in JSON and, as far as I can determine, the specification permitsnull
values inside a JSON object, but does not appear to permit having anull
JSON object at the "top level". i.e. this is valid:but this doesn't seem to be allowed:
null
as the entire definition of a JSON object.
Per RFC-7159:
and:
and
The way I read this is that you can assign
null
to the things inside a JSON object, but that a JSON object with nothing in it would be represented as:I am
definitely notpretty sure about this. But regardless I actually tried passingnull
to Jackson'sObjectMapper
:and the above code throws an
IllegalArgumentException
:So, it appears Jackson (which is the de-facto standard JSON library in Java) doesn't permit this, so maybe we shouldn't either. In addition, if you pass an empty (or blank) string instead of
null
in the above sample code, you get a JacksonMismatchedInputException
:And last, passing
{}
as the JSON works just fine, and the output is:I'd propose throwing
IllegalArgumentException
if someone passesnull
or an empty string to any of the above four methods.This would necessitate a major version bump in kiwi, though, since while it doesn't change a public API in terms of method signature, it is a major change in behavior.
Refs:
Beta Was this translation helpful? Give feedback.
All reactions