Repository navigation
Description
Summary
Serverpod's stance is "dynamic is not supported as a type" (per #2722, #2600). That rule is enforced inconsistently: Map<String, dynamic> round-trips partially through the serialization layer, then throws on the deserialize side. Either the rule should be enforced uniformly at codegen time, or the runtime should handle the case it already half-supports.
Reproduction
Endpoint signature:
Future<Map<String, dynamic>> foo(Session session) async => {
'nested': {'a': 1}, // any List or Map value
};- Serialization (server → wire): succeeds. JSON-encodes fine.
- Deserialization (wire → server, e.g. as a parameter, or client-side decoding the response): throws
DeserializationTypeNotFoundException: No deserialization found for type dynamicatSerializationManager.deserializewhen a value is a nestedList/Map.
The stack trace originates in generated Protocol.deserialize, called from MapBase.map over each entry of the Map<String, dynamic>, dispatching deserialize<dynamic>(value) — which has no matching branch.
Why this is a bug, not a feature request
The cited maintainer guidance (#2722, #2600) is sound for the wire-format / type-preservation argument. But the current implementation:
- Lets
Map<String, dynamic>pass CLI codegen without error. - Generates
fromJsoncode that callsdeserialize<dynamic>(...). - Throws at runtime when that code path is exercised on nested containers.
That's three inconsistent layers. Pick one:
- Strict: reject
dynamicat the CLI / spy.yaml validator. Fail fast with a clear error pointing users to typed models. (Aligns with the stated design.) - Permissive: handle
t == dynamicinSerializationManager.deserializeby returning already-decodedList/Mapcontainers as-is. (What fix: Handle dynamic types and JSON containers in SerializationManager.deserialize #4874 did.)
The current state — accept it everywhere except at the one runtime point where it matters — is the worst of both.
Use cases that the typed-model workaround doesn't cover
The standard advice ("define a typed class") works when you own the schema. It doesn't cover:
- User-defined / schema-on-read content (CMS document bodies, headless content APIs). The shape is defined by end users at runtime, not by the Serverpod developer.
- Webhook payloads from third parties whose schemas evolve outside your release cycle.
- Passthrough / gateway endpoints that re-emit external JSON without needing to model it.
For these, the only escape hatches are (a) String + manual jsonEncode/jsonDecode at every callsite, or (b) hand-rolled custom serializers. Both defeat the point of having a typed RPC framework.
Real-world context
We hit this building dart_desk_be, an open-source Serverpod-based headless CMS backend. The Document.data field is user-defined JSON (schema controlled by end users, not us). We tried every supported route:
Map<String, dynamic>?→ blocked by this issue (CLI accepts; deserialize throws).- Forking Serverpod to add the
dynamicbranch → works, but the consuming Flutter library is published to pub.dev, anddependency_overridesaren't transitive — consumers would silently break. String?text column with manualjsonEncode/jsonDecode→ works but loses any chance of typed jsonb queries.
We ended up adding a Postgres-side generated jsonb column (data_jsonb) derived from the text column, with hand-written unsafeQuery for containment lookups (commits 4c75373, 03a4a71). That works, but it's a workaround for a framework-level inconsistency. We documented the tradeoffs in the repo's CLAUDE.md for other contributors.
Relationship to PR #4960 (jsonb as first-class storage)
#4960 makes jsonb storage available for List<T>, Map<String, T> (concrete T), and named classes — all cases where T is statically known. It does not relax the "must know the type" rule, nor should it. This issue is orthogonal: it's about the deserializer's behavior when the user has explicitly opted out of type info via dynamic.
Proposal
Option A (preferred — keeps existing intent, fixes the inconsistency): handle t == dynamic in SerializationManager.deserialize:
data == null→ return nulldata is Listordata is Map→ return as-is (already decoded byjsonDecode)- Otherwise → re-dispatch on runtime type
This is a non-breaking change (previously-throwing cases now succeed). PR #4874 implemented exactly this with tests; happy to revive it if this issue lands favorably.
Option B (stricter): reject dynamic at CLI codegen time. Breaking change for anyone currently relying on the partially-working serialization side, but internally consistent.
Either is better than the status quo.
Environment
- Serverpod: 3.4.x (also reproducible on 2.x per linked discussions)
- Affects: any endpoint or model with
Map<String, dynamic>fields where values are nested containers
Related
- Discussions: No deserialization found for type dynamic #2722, Storing JSON (Map<String,dynamic>) #2600, No deserialization found for type dynamic #1048, FormatException: No deserialization found for type dynamic #444
- Closed PR with implementation + tests: fix: Handle dynamic types and JSON containers in SerializationManager.deserialize #4874
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
marcelomendoncasoares commentedon Apr 30, 2026
Thanks for reporting this asymmetry, @thangmoxielabs! We already had an idea to solve
dynamic, so it will be solved in the best way possible: supporting the type officially on models and endpoints.thangmoxielabs commentedon May 1, 2026
@marcelomendoncasoares thanks for addressing this issue. Really looking forward to this feature being merged