Skip to content

fix: Handle dynamic types and JSON containers in SerializationManager.deserialize - #4874

Closed
ThangVuNguyenViet wants to merge 1 commit into
serverpod:mainfrom
ThangVuNguyenViet:fix/dynamic-map-deserialization
Closed

ThangVuNguyenViet wants to merge 1 commit into
serverpod:mainfrom
ThangVuNguyenViet:fix/dynamic-map-deserialization

Conversation

@ThangVuNguyenViet

@ThangVuNguyenViet ThangVuNguyenViet commented Mar 21, 2026 •

Copy link
Copy Markdown

Fixes deserialization of Map<String, dynamic> fields where values are nested List or Map objects.

When Serverpod deserializes a model with a Map<String, dynamic> field, the generated fromJson code calls deserialize<dynamic>(value) for each entry. If a value is itself a List or Map (very common in JSON), the previous code had no matching branch and threw DeserializationTypeNotFoundException.

This PR adds a missing t == dynamic code path to SerializationManager.deserialize:

  • data == null: returns null
  • data is List or data is Map: returns the already-decoded container as-is (only when t == dynamic, so typed deserialization like deserialize<UserInfo> still delegates to Protocol subclasses)
  • Otherwise: re-dispatches on the runtime type (e.g. int, String, double)

Fixes #2722, #1048, #444, #2600

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

None — this adds new handling for previously-unsupported cases that previously threw an exception.

@github-actions

github-actions Bot commented Mar 21, 2026 •

Copy link
Copy Markdown

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@ThangVuNguyenViet
ThangVuNguyenViet marked this pull request as draft March 21, 2026 22:06
@ThangVuNguyenViet
ThangVuNguyenViet force-pushed the fix/dynamic-map-deserialization branch from 3c5994b to 915934f Compare March 21, 2026 22:10
@ThangVuNguyenViet ThangVuNguyenViet changed the title fix: handle dynamic types and JSON containers in SerializationManager.deserialize Handle dynamic types and JSON containers in SerializationManager.deserialize Mar 21, 2026
@ThangVuNguyenViet ThangVuNguyenViet changed the title Handle dynamic types and JSON containers in SerializationManager.deserialize Fix: Handle dynamic types and JSON containers in SerializationManager.deserialize Mar 25, 2026
@ThangVuNguyenViet
ThangVuNguyenViet marked this pull request as ready for review March 25, 2026 12:53
@ThangVuNguyenViet ThangVuNguyenViet changed the title Fix: Handle dynamic types and JSON containers in SerializationManager.deserialize fix: Handle dynamic types and JSON containers in SerializationManager.deserialize Mar 25, 2026
….deserialize

When deserializing a Map<String, dynamic> field, the generated fromJson
code calls deserialize<dynamic>(value) for each entry. If a value is a
nested List or Map, the previous code had no matching branch and threw
DeserializationTypeNotFoundException.

This fix adds three missing code paths:
- When t == dynamic: dispatch on the actual runtime type
- When data is List: return as-is (already decoded)
- When data is Map: return as-is (already decoded)

Fixes reported in discussions serverpod#2722, serverpod#1048, serverpod#444, serverpod#2600
@ThangVuNguyenViet
ThangVuNguyenViet force-pushed the fix/dynamic-map-deserialization branch from 915934f to 1200434 Compare March 25, 2026 12:56
@ThangVuNguyenViet

Copy link
Copy Markdown
Author

recheck

@ThangVuNguyenViet

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@ThangVuNguyenViet

Copy link
Copy Markdown
Author

recheck

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator

Hi @ThangVuNguyenViet! We appreciate your contribution, but I'll ask you to first open an issue so we can properly evaluate the use case and discuss the rightful approach.

This PR cites several discussions, but is not in accordance with the answers from maintainers on them (namely this and this).

I'll close this PR for now, but feel free to review the answers on the discussions and open the issue with your use case.

@github-actions github-actions Bot locked and limited conversation to collaborators Mar 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants