Repository navigation
Description
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.
serverpod/tests/serverpod_test_server/lib/src/generated/protocol.dart
Lines 8336 to 8355 in cf276aa
| 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:
serverpod/tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart
Lines 184 to 193 in a9f4715
| 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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity