Skip to content

Generated protocol has no deserializer for a List field declared on an exception or a sealed parent #5945

Closed

Description

@BiAksoy

Describe the bug

When a container field such as List<FieldProblem> is declared on a serializable exception, or on the sealed parent of a hierarchy, and no other class declares a field of the same type, the generated Protocol.deserialize has no branch for that type. Decoding the object then fails:

No deserialization found for type List<FieldProblem>

On the client this means an exception thrown by an endpoint does not arrive as the typed exception, and a model whose sealed parent declares the list cannot be read at all.

The container types that get a branch are collected from the fields of unsealedModels.whereType<ModelClassDefinition>():

final allFieldsToGenerateSerialization = unsealedModels
.whereType<ModelClassDefinition>()
.expand((m) => m.fields)
.where((f) => f.shouldIncludeField(serverCode))
.distinct();

That leaves out two kinds of declarations:

  • Exceptions, because they are ExceptionClassDefinitions.
  • Sealed parents, models or exceptions, because unsealedModels drops every sealed class, and fields holds only a class's own fields, so the children do not bring the parent's fields with them.

From the history, not run on 2.x: before #4227 this loop iterated over unsealedModels with a classInfo is ClassDefinition check, and unsealedModels dropped only sealed models. So exception fields and the fields of sealed exceptions were collected until 3.0.0, while the fields of sealed models were not:

var unsealedModels = allModels
.where((model) => !(model is ModelClassDefinition && model.isSealed))
.toList();

for (var classInfo in unsealedModels)
// Generate deserialization for fields of models.
if (classInfo is ClassDefinition)
for (var field in classInfo.fields.where(
(field) => field.shouldIncludeField(serverCode),
))
...field.type.generateDeserialization(
serverCode,
config: config,
),

To reproduce

  1. Create a project:

    serverpod create sprepro -t server --no-database --no-redis --no-auth --no-website --ide none
  2. Add these model files under sprepro_server/lib/src/repro/:

    # field_problem.spy.yaml
    class: FieldProblem
    fields:
      field: String?
      message: String
    # some_refused.spy.yaml
    exception: SomeRefused
    fields:
      message: String
      problems: List<FieldProblem>?
    # tag.spy.yaml
    class: Tag
    fields:
      name: String
    # post.spy.yaml
    class: Post
    sealed: true
    fields:
      tags: List<Tag>
    # text_post.spy.yaml
    class: TextPost
    extends: Post
    fields:
      text: String
  3. Run serverpod generate in sprepro_server. Neither protocol.dart (server or client) contains a List<FieldProblem> or a List<Tag> branch.

  4. Add sprepro_client/bin/repro.dart and run it with dart run bin/repro.dart:

    import 'dart:convert';
    import 'package:sprepro_client/sprepro_client.dart';
    
    void main() {
      final values = <Object>[
        SomeRefused(
          message: 'refused',
          problems: [FieldProblem(field: 'name', message: 'required')],
        ),
        TextPost(tags: [Tag(name: 'news')], text: 'hello'),
      ];
      for (final value in values) {
        final wire = jsonDecode(jsonEncode(Protocol().wrapWithClassName(value)));
        try {
          print('decoded: ${Protocol().deserializeByClassName(wire)}');
        } catch (e) {
          print('decode failed: $e');
        }
      }
    }
    decode failed: No deserialization found for type List<FieldProblem>
    decode failed: No deserialization found for type List<Tag>
    

The same happens with a sealed exception that declares the list. If any other class also declares a field of the same container type, the branch is generated and both objects decode.

Expected behavior

Container types used by the fields of exceptions and sealed parents get deserializer branches, as those of other model fields do, and both objects decode.

Library version

Serverpod 4.1.0 (steps above) and main at 0bd8e09. Dart 3.13.7. First seen on 4.0.4.

Platform information

macOS 27.0.1 (arm64).

Additional context

  • A workaround is to declare the list on a model class that is not sealed (for example FieldProblems { items: List<FieldProblem> }) and use that class as the field type.
  • fix: Fix deserialization of collections of serverOnly models #4433 fixed the same symptom for serverOnly models at the same place in the generator.
  • Collecting the fields from allModels.whereType<ClassDefinition>() makes the branches appear and both objects decode. I have a fix with tests and can open a PR.

How experienced are you with this library?

None

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    on Nov 3, 2026
  2. added
    area: models
    on Nov 5, 2026
  3. moved this from Inbox 📫 to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Nov 5, 2026
  4. moved this from Contributor In Progress 👩‍💻 to Done 🚀 in Serverpod Frameworkson Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: modelsbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions