Repository navigation
Description
IMPORTANT EDIT: After more testing, it appears this is a much more generalized problem than the example given here in this first post. You don't actually need this nested structure with Enums for it to produce the error; simply having an empty Map of type Map<int, int> will also throw an error when it's provided as an argument in calling an endpoint method (and possibly in other scenarios too with this empty map, but haven't exhaustively tested). See further discussion in the later posts
Describe the bug
Despite merging PR #1799 and its linked closing of issue #1806 there are still problems when there is an empty Map with your own serverpod-serializable classes, at least in the form that can be recreated as described below. Maps with int keys seem to work fine now but I'm still getting the same errors as before when I have a field in one of the yaml-defined classes as follows:
formattingData: Map<FormattingType, List<FormattingPosition>>
where FormattingType in an enum and FormattingPosition is a serializable class (both defined in the protocol folder yaml files.
I have not done extensive testing to see what this will generalize to but probably it can at least be safely said that a map of type Map<SomeEnum, List<SomeClass>> will cause an error if assigned an empty value and it needs to go through the serialization/deserialization process.
To Reproduce
In your serverpod yaml files, define an enum and define a class (what the fields are within that class probably don't matter). Then use the data type of Map<SomeEnum, List<SomeClass>> as an argument in an endpoint and call that endpoint when that map is empty {}. It will give the error "map is not subtype of List<dynamic> in type cast" on the line in the generated protocol.dart file that is this or similar:
return Map.fromEntries((data as List).map((e) => MapEntry( where the data is being cast to a List.
Additionally, using an empty map like this will cause database problems. It is written to the database in an incorrect format and causes a similar issue when attempting to read it that only happens when it's empty and not when it contains some data.
If instead of an empty Map, you have all possible enum values as keys with empty list values, like:
final workaround = {SomeEnum.valueA: [], SomeEnum.valueB: [], SomeEnum.valueC: []}
the problems go away and it works as expected. This is an OK workaround for now, but I don't think this is or should be the expected behavior.
I currently don't have the time to do enough testing to know how much this issue generalizes, in other words if it applies to all Maps with enum keys regardless of the Map value types, or even further than that.
Expected behavior
An empty map should not cause problems like this. Empty lists seem fine with this various functionality and maps should as well.
Screenshots
If applicable, add screenshots to help explain your problem.
Serverpod versions
Everything set to serverpod 1.2.3
Platform information
flutter web with Flutter 3.16.9
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
SandPod commentedon Dec 19, 2024
@jason-downs - Is this still an issue? We have done some rework on our serialization and deserialization for maps in the framework.
jason-downs commentedon Dec 19, 2024
I haven't checked in a while, let me see if it's still doing this.
jason-downs commentedon Jul 1, 2025
Hi, sorry it took me so long to circle back to this one....but unfortunately it is indeed still a problem. I currently have an endpoint method that takes type
Map<int, int>as a parameter and when it is called from the app with an empty map for this argument, it gives error:type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast. I'm going to look into this a bit further now and try to reproduce with a more generalized example.jason-downs commentedon Jul 1, 2025
The offending code is in the auto-generated protocol.dart (server-side) where:
The "data as List" typecast fails.
jason-downs commentedon Jul 1, 2025
Ok, so I reproduced this with a minimal serverpod example, where an endpoint method has a
Map<int, int>parameter, and it produces an error when this method is called with an empty Map.Important to note that this does NOT produce an error when the Map keys are String instead of int. However, according to the docs, it should work with both of these, and any other serverpod-serializable type in the Map.
On the server side:
In the flutter app, with the standard boilerplate in main.dart that opens a MyHomePage widget as follows:
When you run the app and press the first button that sends a non-empty map as an argument, it prints as expected. When you press the second button it throws the same error as mentioned above,
type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>' in type castwith the same code inprotocol.dartmentioned above being the culprit.As already mentioned, when I change the Map to
Map<String, int>and repeat this process, the empty map is fine and there is no error. I have not tried maps with other types as keys besides String and int.Therefore, although the docs claim that Maps can have any serverpod-compatible type as keys, in practice, unless they are String keys, their usefulness is seriously limited as endpoint method parameters, as it would need to be guaranteed that they hold at least one entry for it to work properly, but this is often not the case for certain kinds of function arguments.
jason-downs commentedon Jul 19, 2025
I'm going to attempt to work on a PR to fix this, as it's a pretty serious annoyance in my current project.
tp commentedon Jul 21, 2025
@jason-downs As discussed on the PR(s), this is indeed a serious issue.
To give some context, this only affects top-level parameters, not
Map<int, X>(and similar) nested in model classes or records. Maybe that's how there was a general assumption that this should work, because all such types inside models are heavily tested.But in this specific case it really only was noticeable in E2E scenarios with such an empty map given to an API endpoint.
#3803 will provide a fix for this immediate issue (to level parameters), though I would also want to follow up to add some tests for further nested entities. For example, I have a hunch that
List<Map<int, X>>will still be broken. We'll see how many of those cases can easily be fixed with the current approach, and which need a bigger refactoring of the serialization layer.As an aside, I could imagine that it would make sense from the framework's side to stop relying on the JSON encoder to "asking us for help" with certain types, and instead always prepare the full object hierarchy as a JSON-encodable structure (which all transforms applied already) and only then call out to have it stringified as JSON.
jason-downs commentedon Jul 22, 2025
@tp Thanks for your work on this and the PR, really appreciate it!