Skip to content

#1168: handle JSON null in User.Smart#name() and #hasName()#1851

Merged
yegor256 merged 2 commits intojcabi:masterfrom
bibonix:fix-1168-user-name-null
May 6, 2026
Merged

#1168: handle JSON null in User.Smart#name() and #hasName()#1851
yegor256 merged 2 commits intojcabi:masterfrom
bibonix:fix-1168-user-name-null

Conversation

@bibonix
Copy link
Copy Markdown
Contributor

@bibonix bibonix commented May 6, 2026

@yegor256, this PR closes #1168.

The bug

When a GitHub user account has no name set, the API returns "name": null in the user JSON instead of omitting the key. In that case, User.Smart#hasName() returned true (because the key is present) and User.Smart#name() then failed with java.lang.ClassCastException from JsonObject#getString while trying to cast JsonValue.NULL to JsonString. Calling hasName() first did not protect callers from the CCE.

The fix

Both name() and hasName() now treat a JSON null value the same as an absent key:

  • hasName() returns false when the value is JSON null.
  • name() throws the same IllegalStateException it already threw when the key was absent.

The behavioral contract if (smart.hasName()) smart.name(); is once again safe.

Tests

RtUserTest gets two regression tests (hasNameReturnsFalseWhenNameIsNull, nameThrowsIllegalStateWhenNameIsNull) that fail on master with the original ClassCastException and pass after the fix. They are committed before the fix so the regression is reproducible from the test commit alone.

Commits

  1. #1168: regression tests for User.Smart with null name JSON value
  2. #1168: skip JSON null when checking User.Smart#name() and #hasName()

CI note

The mvn workflow currently fails on master due to ~837 pre-existing qulice (PMD/Checkstyle) violations that this PR does not introduce. With my changes the violation count is unchanged at 837. All other 8 CI jobs pass.

@yegor256 yegor256 merged commit 7ce9302 into jcabi:master May 6, 2026
8 of 14 checks passed
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

Successfully merging this pull request may close these issues.

User will fail with class cast exception if name is null

2 participants