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

JsonUtil.getObject() etc behaves differently between explicit null value and absence of the key #8883

Open
dapengzhang0 opened this issue Feb 1, 2022 · 2 comments
Assignees
Milestone

Comments

@dapengzhang0
Copy link
Member

(Noticed by @sergiitk in #8871 (comment)) The current implementation of JsonUtil.getObject() will throw ClassCastException if the input contains the key with an explicit null value.

  /**
   * Gets an object from an object for the given key.  If the key is not present, this returns null.
   * If the value is not a Map, throws an exception.
   */
  @SuppressWarnings("unchecked")
  @Nullable
  public static Map<String, ?> getObject(Map<String, ?> obj, String key) {
    assert key != null;
    if (!obj.containsKey(key)) {
      return null;
    }
    Object value = obj.get(key);
    if (!(value instanceof Map)) {
      throw new ClassCastException(
          String.format("value '%s' for key '%s' in '%s' is not object", value, key, obj));
    }
    return (Map<String, ?>) value;
  }
@sergiitk
Copy link
Member

sergiitk commented Feb 2, 2022

A unit test that confirms current behavior: #8881

@sergiitk sergiitk self-assigned this Feb 2, 2022
@ejona86
Copy link
Member

ejona86 commented Feb 4, 2022

What is the problem here? Failing seems appropriate. If a key is set it needs to have the right type and null isn't the right type. There seems to be no purpose to allowing the values to be set to null and to treat that as not present.

sergiitk added a commit that referenced this issue Feb 4, 2022
Verifies the behavior of JsonUtil.getObject when the map contains a null value for a given key.

Note: this may be incorrect behavior. Issue to track the investigation: #8883.
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