Skip to content

fix(hydrated_bloc): serialize non-string map keys#4758

Merged
felangel merged 14 commits intofelangel:masterfrom
ersanKolay:fix/hydrated-bloc-non-string-map-keys
Mar 14, 2026
Merged

fix(hydrated_bloc): serialize non-string map keys#4758
felangel merged 14 commits intofelangel:masterfrom
ersanKolay:fix/hydrated-bloc-non-string-map-keys

Conversation

@ersanKolay
Copy link
Contributor

@ersanKolay ersanKolay commented Mar 11, 2026

Summary

  • Fixes _traverseComplexJson silently dropping map entries with non-String keys (e.g. int keys from @JsonValue(int) enums used as map keys in json_serializable).
  • The root cause was _cast<String>(key) returning null for non-String keys, causing entries to be skipped entirely. Replaced with key is String ? key : key?.toString(), consistent with how _traverseRead already handles keys.
  • Added unit tests and E2E tests covering non-string map key serialization and restoration.

Closes #3983

Test plan

  • Existing 122 tests pass
  • New unit test: verifies int map keys are converted to string keys when persisting
  • New unit test: verifies state restoration from cache with stringified int keys
  • New E2E test: full write-read-restore cycle with @JsonValue(int) enum-like map keys
  • dart analyze clean (no errors or warnings)

…of dropping them

When using json_serializable with @jsonvalue(int) enums as map keys,
_traverseComplexJson silently dropped entries because _cast<String>(key)
returned null for non-String keys. This converts them via toString()
instead, consistent with _traverseRead behavior.

Closes felangel#3983
@ersanKolay ersanKolay requested a review from felangel as a code owner March 11, 2026 13:23
@felangel felangel added bug Something isn't working pkg:hydrated_bloc This issue is related to the hydrated_bloc package labels Mar 11, 2026
@felangel felangel changed the title fix(hydrated_bloc): preserve non-string map keys during serialization fix(hydrated_bloc): serialize non-string map keys Mar 14, 2026
felangel
felangel previously approved these changes Mar 14, 2026
Copy link
Owner

@felangel felangel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks so much for the contribution! 💙

@felangel felangel merged commit 81701f3 into felangel:master Mar 14, 2026
11 checks passed
@codecov
Copy link

codecov bot commented Mar 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working pkg:hydrated_bloc This issue is related to the hydrated_bloc package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Enum with int type cannot be stored

2 participants