Skip to content

Asymmetric handling of Map<String, dynamic>: serialization works, deserialization throws #4996

Closed

Description

@thangmoxielabs

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 dynamic at SerializationManager.deserialize when a value is a nested List/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:

  1. Lets Map<String, dynamic> pass CLI codegen without error.
  2. Generates fromJson code that calls deserialize<dynamic>(...).
  3. Throws at runtime when that code path is exercised on nested containers.

That's three inconsistent layers. Pick one:

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 dynamic branch → works, but the consuming Flutter library is published to pub.dev, and dependency_overrides aren't transitive — consumers would silently break.
  • String? text column with manual jsonEncode/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 null
  • data is List or data is Map → return as-is (already decoded by jsonDecode)
  • 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

Activity

  1. marcelomendoncasoares commented on Apr 30, 2026

    @marcelomendoncasoares
    Collaborator

    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.

  2. moved this from Inbox 📫 to In Progress 🔥 in Serverpod Frameworkson Apr 30, 2026
  3. added
    area: models
    on Apr 30, 2026
  4. thangmoxielabs commented on May 1, 2026

    @thangmoxielabs
    Author

    @marcelomendoncasoares thanks for addressing this issue. Really looking forward to this feature being merged

  5. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: models

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions