Repository navigation
feat: Add abstract copyWith method to sealed classes - #4238
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
- Added abstract `fromJson` method to sealed parent classes - Added abstract `copyWith` method to sealed parent classes - Child classes now implement these methods with @OverRide - Instance `fromJson` method added to children of sealed parents - Updated override logic to handle sealed parent classes correctly Co-authored-by: marcelomendoncasoares <[email protected]>
- Updated _buildCopyWithMethod to add @OverRide when parent is sealed - Made classDefinition parameter optional (null for impl classes) - Updated all sealed class tests to expect new behavior - Added tests for abstract fromJson method on sealed parents - Added tests for instance fromJson with @OverRide on children - All 55 sealed class tests now passing Co-authored-by: marcelomendoncasoares <[email protected]>
- Created test demonstrating polymorphic copyWith and fromJson usage - Verified sealed classes can be used polymorphically via parent type - Both tests pass successfully Co-authored-by: marcelomendoncasoares <[email protected]>
There was a problem hiding this comment.
@copilot Please address my comments. Make sure to run dart format before committing to avoid CI errors.
- Removed abstract fromJson method from sealed parent classes - Removed instance fromJson method from child classes - Kept abstract copyWith method on sealed parents - Removed comments from code - Moved copyWith test to inheritance_polymorphism_test.dart - Removed sealed_polymorphism_test.dart file - Updated sealed_class_test.dart to remove fromJson tests - Ran dart format on all modified files - All tests passing (705 tests) Co-authored-by: marcelomendoncasoares <[email protected]>
Addressed all review comments. Removed abstract Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
marcelomendoncasoares
left a comment
There was a problem hiding this comment.
@copilot Please address my final comments.
- Added expect assertions to verify copyWith is abstract (EmptyFunctionBody) - Removed comment from _shouldOverrideAbstractCopyWithMethod - All 50 sealed class tests passing with proper abstract checks Co-authored-by: marcelomendoncasoares <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
@copilot Add one final unit test on tools/serverpod_cli/test/generator/dart/server_code_generator/sealed_class_test.dart with this case to ensure that sealed classes can be used as types on fields of other models. The model must compile correctly. Then, add one final integration test on the serverpod_test_server with the same purpose. For such, create a new model on the project that contains a sealed class and assert that it is being correctly generated.
- Added unit test in sealed_class_test.dart verifying sealed classes can be used as field types - Created ObjectWithSealedClass model with sealed class fields - Added integration tests in both server and client test files - Tests verify serialization, deserialization, and type safety - All 709 code generator tests passing - All 19 inheritance polymorphism tests passing Co-authored-by: marcelomendoncasoares <[email protected]>
Added comprehensive tests for sealed classes as field types: Unit Test (sealed_class_test.dart):
Integration Tests (inheritance_polymorphism_test.dart):
All tests passing (54 unit tests, 19 integration tests). (commit 1e3dfc8) Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Sealed classes in Serverpod now support polymorphic
copyWithoperations. Previously,copyWithcould only be called on concrete child types, limiting generic code that operates on sealed hierarchies.Note: While the original issue (#3173) requested both
fromJsonandcopyWithabstract methods, onlycopyWithwas implemented as abstract. ThefromJsonmethod is always implemented as a factory constructor and doesn't benefit from being abstract on the parent class.fromJsonandcopyWithabstract methods to sealed classes for child interoperability #4237Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.Breaking changes
None. This is additive - existing code continues to work unchanged. Child classes gain the
@overrideannotation on their existingcopyWithmethods.Changes Made
Code generation updates:
copyWithmethodcopyWithwith@overrideannotationExample usage:
Generated code:
Testing
Unit Tests:
Integration Tests:
ObjectWithSealedClassmodel demonstrating sealed class fields (sealedField, nullableSealedField, sealedList)This implementation enables generic code to work with sealed class hierarchies without knowing the concrete types, using
copyWithpolymorphically and supporting sealed classes as field types in other models with full serialization/deserialization support.Original prompt
fromJsonandcopyWithabstract methods to sealed classes for child interoperability #4237💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.