feat(a2ui_message): make toJson compatible with fromJson#780
feat(a2ui_message): make toJson compatible with fromJson#780matiasleyba wants to merge 2 commits intoflutter:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the toJson() methods for A2uiMessage subclasses to make them compatible with A2uiMessage.fromJson(), enabling round-trip serialization. The changes are well-implemented and include corresponding updates to tests and new round-trip tests. My feedback focuses on improving maintainability by using constants for repeated string literals like the API version and JSON keys, and a minor cleanup in the new test code.
| /// Converts this message to a JSON map. | ||
| @override | ||
| Map<String, dynamic> toJson() => { | ||
| 'version': 'v0.9', |
There was a problem hiding this comment.
The version string 'v0.9' is repeated across all toJson implementations and is also checked in A2uiMessage.fromJson. To improve maintainability and prevent potential inconsistencies when updating the version, consider defining this as a constant. For example, you could add a private constant at the top of this file: const String _kApiVersion = 'v0.9'; and use it in all relevant places.
| 'createSurface': { | ||
| surfaceIdKey: surfaceId, | ||
| 'catalogId': catalogId, | ||
| 'theme': theme, | ||
| 'sendDataModel': sendDataModel, | ||
| }, |
There was a problem hiding this comment.
There are several string literals used as JSON keys here and in the corresponding fromJson factory (e.g., 'createSurface', 'catalogId', 'theme', 'sendDataModel'). To prevent typos and improve maintainability, it's a good practice to define these as constants, similar to how surfaceIdKey is used. This would make the code safer and easier to refactor.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Description
A2uiMessagesubclass’stoJson()now returns the wrapped envelope (version + one of createSurface / updateComponents / updateDataModel / deleteSurface) instead of a flat payload.A2uiMessage.fromJson()expects, so round-trip works.Why
A2uiMessage.fromJson(saved).fromJson(message.toJson())failed because the root map had no action key.CreateSurface,UpdateComponents, etc.) and build JSON per variant.message.toJson()on anyA2uiMessageand save without type checks.CreateSurface,UpdateComponents,UpdateDataModel, andDeleteSurfacenow put their payload under the corresponding key.Round-trip tests were added
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-devrel channel on Discord.