Skip to content

fix: handle null Xiaomi token plan responses safely#14

Merged
DimazzzZ merged 1 commit into
mainfrom
fix/json-null-exception
Jul 6, 2026
Merged

fix: handle null Xiaomi token plan responses safely#14
DimazzzZ merged 1 commit into
mainfrom
fix/json-null-exception

Conversation

@DimazzzZ

@DimazzzZ DimazzzZ commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • fix Xiaomi token plan parsing when the API returns null for data, monthUsage, or items
  • extract Xiaomi response parsing into a dedicated XiaomiResponseParser
  • add focused tests for parser and provider behavior

Problem

Xiaomi token plan refresh could throw a ClassCastException when the platform returned JsonNull instead of the expected array/object structure for stopped or expired plans:

java.lang.ClassCastException: class com.google.gson.JsonNull cannot be cast to class com.google.gson.JsonArray
    at com.google.gson.JsonObject.getAsJsonArray(JsonObject.java:211)
    at ...XiaomiProviderClient.fetchTokenPlanUsage$lambda$1(XiaomiProviderClient.kt:139)

Changes

  • replace unsafe getAsJsonArray() / getAsJsonObject() calls with null-safe accessor extensions (getArrayOrNull, getObjectOrNull, getStringOrNull, etc.)
  • move Xiaomi response mapping logic into XiaomiResponseParser
  • preserve expected inactive-plan behavior when usage data is absent (0 tokens, "inactive" status)
  • expand Xiaomi provider tests
  • add dedicated parser tests covering success, null/missing fields, and error envelopes

Validation

  • ./gradlew test --tests "org.zhavoronkov.tokenpulse.provider.xiaomi.*" — all tests pass
  • Xiaomi package line coverage: 95.0% (114/120)

@DimazzzZ DimazzzZ self-assigned this Jul 6, 2026
@DimazzzZ DimazzzZ merged commit 6ea46c1 into main Jul 6, 2026
2 checks passed
@DimazzzZ DimazzzZ deleted the fix/json-null-exception branch July 6, 2026 10:34
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.

1 participant