Repository navigation
Closed
Labels
Description
Subtask to: #1873
Is your feature request related to a problem? Please describe.
When using inheritance through serialization, the runtime type must be preserved to ensure that the subclasses can be instantiated correctly after deserialization.
In the following scenarios, the runtime type of the subclass is lost:
- Return type of a non-streaming endpoint method.
- As a parameter to any endpoint method.
- As a field on a model that is passed through a serialization roundtrip (endpoint, database, etc.).
- Contained inside of a container type such as
ListorMappassed through a serialization roundtrip (endpoint, database, etc.).
Describe the solution you'd like
Below are some scenarios that need to be supported:
Inheritance in endpoint
Given a ChildClass that inherits from a ParentClass, the runtime type of the child class would be lost in the following scenarios:
Future<ParentClass> myEndpointMethod(Session session) async {
// The type of the return value would assume the ParentClass in the client.
return ChildClass(....);
}// The type of `value` would assume the parrent class even if ChildClass is passed from the client.
Future<ParentClass> myEndpointMethod(Session session, ParentClass value) async {
assert(value.runtimeType == ChildClass);
....
}Field serialization roundtrip
Given a ContainerClass where a field named contents has the type ParentClass but the instance of that field is a ChildClass that inherits from ParentClass, this should be supported:
ContainerClass container(contents: ChildClass(...));
// Serialization strips the knowledge of the runtime type for the child class
final afterSerializationRoundtrip = serializationRoundtrip(container);
final contents = afterSerializationRoundtrip.contents;
assert(container.contents == contents.runtimeType);Additional context
- Streaming endpoints already handle this by always encoding transported objects with their runtime type through the
encodeWithTypemethod in the Serialization Manager. But note that this does not apply to any fields of the model.
Reacted by Ben Auer and Marcelo Mendonça Soares
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
marcelomendoncasoares commentedon Aug 17, 2025
@SandPod, I decided to explore a bit on this issue, as I've been using inheritance in a project of mine, and may have found one actually simple solution. I published it here for discussion, if you find it a valid approach.