Skip to content

feat: Implement support for polymorphism on endpoints - #4227

Merged
marcelomendoncasoares merged 10 commits into
serverpod:mainfrom
marcelomendoncasoares:polymorphism-on-endpoints
Nov 17, 2025
Merged

marcelomendoncasoares merged 10 commits into
serverpod:mainfrom
marcelomendoncasoares:polymorphism-on-endpoints

Conversation

@marcelomendoncasoares

@marcelomendoncasoares marcelomendoncasoares commented Nov 15, 2025 •

Copy link
Copy Markdown
Collaborator

This PR implements the missing piece for model inheritance on the Serverpod framework: support for polymorphism on the serialization for non-streaming endpoints. The feature implementation consisted in:

  • Add a __className__ field to the JSON serialization of all models carrying the name of the model.
  • Make the Protocol().deserialize check for such field and call deserializeByClassName if present.
  • Centralize all deserialization calls for List, Set and Map on the Protocol().deserialize method instead of directly inside the fromJson method of each class.

The central piece on the polymorphic deserialization is the deserializeByClassName method, which was already implemented for usage in the streaming endpoints with the exact feature of parsing the model from the specified className - that now goes inside the JSON.

Closes #2909.

During development, two bugs were found and fixed in the serialization of models:

Fixes: #4135
Fixes: #4205

Since these fixes were blocking of this feature and would also touch all of the models, it was preferred to solve them here together instead of creating a blocker and conflicts.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

The implementation is fully backwards compatible and introduces no breaking changes.

@coderabbitai

This comment was marked as resolved.

@marcelomendoncasoares
marcelomendoncasoares marked this pull request as ready for review November 15, 2025 02:12
@marcelomendoncasoares
marcelomendoncasoares requested a review from a team November 15, 2025 02:12

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution looks good!

I'm, however, missing some backwards compatibility tests to ensure that the serialization format used in Serverpod 1.x and 2.x is still supported.

Perhaps manually calling an endpoint with the old format can validate that the response is parsed correctly?

And what is our view on porting our streaming methods over to use this format? We could save some space by not using the old serializeByClassName. There is no code modifying that flow at the moment.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BIG! 🙌

cc: @BenAuerDev 🚀

@marcelomendoncasoares
marcelomendoncasoares merged commit e52079c into serverpod:main Nov 17, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 17, 2025
@marcelomendoncasoares
marcelomendoncasoares deleted the polymorphism-on-endpoints branch November 17, 2025 12:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

2 participants