Skip to content

Deserialization of subclasses in inheritance #2909

Closed

Description

@SandPod

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:

  1. Return type of a non-streaming endpoint method.
  2. As a parameter to any endpoint method.
  3. As a field on a model that is passed through a serialization roundtrip (endpoint, database, etc.).
  4. Contained inside of a container type such as List or Map passed 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 encodeWithType method in the Serialization Manager. But note that this does not apply to any fields of the model.

Activity

  1. added
    enhancement
    area: models
    on Oct 28, 2024
  2. marcelomendoncasoares commented on Aug 17, 2025

    @marcelomendoncasoares
    Collaborator

    @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.

  3. moved this from Backlog 🗂️ to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Nov 6, 2025
  4. moved this from Contributor In Progress 👩‍💻 to In Progress 🔥 in Serverpod Frameworkson Nov 6, 2025
  5. 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: modelsenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions