Skip to content

NULL_BOOLEAN is not compatible with Value Objects #2185

Description

@cushon

google-http-client uses sentinel objects like NULL_BOOLEAN to distinguish between fields being absent and explicitly set to null:

https://github.com/googleapis/google-http-java-client/blame/95fac312dd743a94fb28532ef8a50d374e6481ad/google-http-client/src/main/java/com/google/api/client/util/Data.java#L48-L49

Those sentinel fields are using deprecated constructors like new Boolean(true) (the deprecation warnings were suppressed in #1215).

JDK 28 introduces a preview of the value classes feature (https://openjdk.org/jeps/401). With JEP 401 boxed primitive classes like Boolean and Integer become value classes that don't have identity, so instance are compared by value:

  • new Boolean(true) == Boolean.TRUE will evaluate to true
  • new Integer(0) == Integer.valueOf(0) will evaluate to true

This causes google-http-client's Data.isNull(object) to return true for legitimate values.

With JEP 401, assertions like these will start to fail:

    assertThat(Data.isNull(true)).isFalse(); 
    assertThat(Data.isNull(0)).isFalse();
    assertThat(Data.isNull(0L)).isFalse();

Possible fixes:

  • Introduce a new API like JsonNullable<Boolean> to replace the use of NULL_BOOLEAN
  • Maintain a set of explicitly set fields, that could be queried instead of relying on sentinel values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions