Repository navigation
Description
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>():
serverpod/tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart
Lines 279 to 283 in 0bd8e09
| 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
unsealedModelsdrops every sealed class, andfieldsholds 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:
serverpod/tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart
Lines 54 to 56 in 5e5bca3
| var unsealedModels = allModels | |
| .where((model) => !(model is ModelClassDefinition && model.isSealed)) | |
| .toList(); |
serverpod/tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart
Lines 184 to 193 in 5e5bca3
| 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
-
Create a project:
serverpod create sprepro -t server --no-database --no-redis --no-auth --no-website --ide none
-
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
-
Run
serverpod generateinsprepro_server. Neitherprotocol.dart(server or client) contains aList<FieldProblem>or aList<Tag>branch. -
Add
sprepro_client/bin/repro.dartand run it withdart 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
serverOnlymodels #4433 fixed the same symptom forserverOnlymodels 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity