Skip to content

Support for inheritance in protocol/*.yaml files #793

Closed

Description

@kieran-myopnotes

Is your feature request related to a problem? Please describe.
The problem with the lack of support for inheritance is that this is the only thing that dart does not provide a workaround for. If you wish to add functions or setters/getters to a generated object then you can simply extend that class which is a built in feature of dart. However, there is currently nothing you can do if you wish to have one of your serverpod classes inherit another one of your serverpod classes to achieve things like polymorphism.

Describe the solution you'd like
Similar to the way you specify the "class: ClassName" and the "table: table_name", there should be a third option for "inherits: InheritedClassName"

Describe alternatives you've considered
alternatively, you could follow the standard dart naming convention and use "extends: InheritedClassName". This would allow for future additions for the "implements: InterfaceClassName" and "with: MixinClass1, MixinClass2, MixinClass3". Although in order for these to work you would need a way in your serverpod object to specify the class type and whether it is a Class, Interface, or Mixin

Additional context
This has been the single largest issue for me and my dev team as we use serverpod in our production application with the only alternative being to manually define classes and use the freezed package however we wish to continue using the simple syntax provided by serverpod.

Activity

  1. wstrange commented on Mar 29, 2023

    @wstrange
    Contributor

    This underscores one of the issues with using yaml to generate Dart, instead of just using native Dart + annotations.

    Older discussion but still relevant: #186

  2. added
    enhancement
    on Apr 3, 2023
  3. tgrushka commented on Jul 27, 2023

    @tgrushka
    Contributor

    I want to use message streaming to notify my clients of updates to models. The following code compiles, and the endpoint sends the message, but the clients never receive it, because the deserialization gets messed up.

    I spent hours trying to figure out why messages weren't being received, then when I removed the entity field, everything worked fine.

    This is all because inheritance is not supported. Why can't we even use SerializableEntity as a generic type so my entity can be dynamic?

    (Also, the serverpod:ModelClass thing is totally undocumented, I discovered it by accident.)

    class: UpdateMessage
    fields:
      user: module:auth:UserInfo
      action: UpdateAction
      entity: serverpod:SerializableEntity
  4. Isakdl commented on Jul 28, 2023

    @Isakdl
    Collaborator

    @tgrushka I think what you are asking for here is different from inheritance on the classes them self's.

    You want to use a base model of a type so that you can add the classes that extends the base model. I think this will more or less be impossible to resolve as we will not know the type we need to serialize / de-serialize into. I presume you actually want the full class not just the base one defined here.

    However I think the solution here is to support Generic types, where you could maybe define something like this:

    class: UpdateMessage<T>
    fields:
      user: module:auth:UserInfo
      action: UpdateAction
      entity: T

    Then we should be able to embed enough information in the message to resolve the datatype, but as dart doesn't support reflections the implementation details may be pretty tough. Related discussion here: #1035.

  5. SandPod commented on Aug 29, 2024

    @SandPod
    Contributor

    Closing this issue and will reference the discussion from the RFC for model inheritance here: #1873

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions