Skip to content

Duplicated deserialization code for classes referenced in multiple relation fields #4205

Closed

Description

@marcelomendoncasoares

Describe the bug

When a list, set or map of a model class is referenced in multiple relation fields across different models (or even the same model), the code generator creates duplicate deserialization code in the generated protocol.dart file. Although this causes no real problem, it is duplicated and dead code on the SerializationManager.deserialize method.

To reproduce

Create one or more models that have relation fields referencing the same list, set or map type. It can be on the same model or on different models, like what happens in the serverpod_test_server test project with List<Person>? fields.

# tests/serverpod_test_server/lib/src/models/models_with_list_relations/city.spy.yaml
class: City
table: city
fields:
  name: String
  citizens: List<Person>?, relation
  organizations: List<Organization>?, relation(name=city_org)
# tests/serverpod_test_server/lib/src/models/models_with_list_relations/organization.spy.yaml
class: Organization
table: organization
fields:
  name: String
  people: List<Person>?, relation(name=org_person)
  city: City?, relation(name=city_org, optional)

Because the City model references both the List<Person>? and the List<Organization>?, and the Organization model references the List<Person>?, the code generator will create duplicate deserialization code for the List<Person>? type.

if (t == _i1.getType<List<_i110.Person>?>()) {
return (data != null
? (data as List).map((e) => deserialize<_i110.Person>(e)).toList()
: null)
as T;
}
if (t == _i1.getType<List<_i109.Organization>?>()) {
return (data != null
? (data as List)
.map((e) => deserialize<_i109.Organization>(e))
.toList()
: null)
as T;
}
if (t == _i1.getType<List<_i110.Person>?>()) {
return (data != null
? (data as List).map((e) => deserialize<_i110.Person>(e)).toList()
: null)
as T;
}

Expected behavior

Each entry in the SerializationManager.deserialize method should be unique, regardless of how many models reference that type.

Library version

3.0.0-alpha.3

Platform information

Any

Additional context

The issue is caused in the library_generator.dart file:

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,
),

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    area: models
    on Nov 13, 2025
  2. moved this from Inbox 📫 to In Progress 🔥 in Serverpod Frameworkson Nov 15, 2025
  3. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: modelsbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions