Repository navigation
fix: Empty map deserialization bug - #3801
jason-downs wants to merge 8 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
But why would this happen? That seems more of a serialization bug (if the framework would ever do it), than a deserialization one, no? The current implementation seems a bit different than the suggest fix in the description, but the end-result is the same, so it doesn't matter for the following point: (data is List ? data : [])Basically this silently discard any "invalid" data. Personally I would not feel comfortable doing this, but rather prefer an explicit error, because the incoming data to deserializre should not be in the wrong shape in the first place. And if it is, we should be notified about it. Today this might fix some class of "empty map" bugs (however they come about, the root cause for the issue is not yet clear to me), but tomorrow this might hide something else. So I think we need to figure out how the initial (and still current) invalid serialized data comes about, and then make the serialization strict/correct instead of the deserialization more lenient. What do you think, @jason-downs ? |
Yes you're right. I admit to being rushed and having a lot of help here from AI in trying to navigate this codebase for the first time, which was rather daunting, and this is what it came up with for a fix. I agree that the real problem may indeed be the serialization of the empty map. It does happen consistently, for example when an empty I can try to dive into the serialization code which might be easier for me now that I've gotten my feet wet in the codebase. |
|
@jason-downs Thanks for the explanation and tracking this down. The bug indeed is real, but I was only able to trigger it in an E2E test, even though the unit test exhibits the same behavior that an empty I'll dig into this right away to see how we could fix it and at least give an update on the ticket. I'll ping you once I have anything to share :) |
|
#3803 now fixes this issue on the sending side, instead of making the parsing more lenient. @jason-downs I totally get your point. This was much trickier than I thought at first, as it touched quite a bit of the code generation logic (client, server, and test tools). It looks like there is some low-hanging fruit to make that whole (de)serialization a little more concise and get it to exercise the same code paths (so this would've worked on both parameters and field, or be broken on both). But for now, I wanted to get a solid fix in for your immediate issue. As a workaround (until that fix is released), you might be best served by putting the |
fix #1908
(also fixes #1806 which was closed but not fully fixed)
The actual edits are in
tools/serverpod_cli/lib/src/generator/types.dartandtools/serverpod_cli/lib/src/generator/dart/library_generators/util/class_generators_util.dart. The rest of the changes are from changed generated code resulting from the changes in the two files mentioned. Finally, there are new comprehensive unit tests intests/serverpod_test_server/test/empty_map_serialization_test.dart.Further details:
The Bug
Error Message:
Root Cause:
When Maps with non-String keys (like
Map<int, String>) contain empty data, they are supposed to be serialized as empty Lists[]to follow the same pattern where the non-empty map is represented as a list of objects with "k" and "v" names. However, during deserialization, if the data is somehow an empty Map{}instead of a List, the code tries to cast it with(data as List)which throws the type cast error.The Fix
The generated deserialization code should check if
datais a Map before casting as List:Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.If you need help, consider asking for advice on the discussion board.
Breaking changes
This shouldn't have any breaking API changes, just internal/bugfixes.