Skip to content

feat: Allow generating shared models between server and client - #4737

Merged
marcelomendoncasoares merged 9 commits into
serverpod:mainfrom
marcelomendoncasoares:common-package
Feb 19, 2026
Merged

marcelomendoncasoares merged 9 commits into
serverpod:mainfrom
marcelomendoncasoares:common-package

Conversation

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator

This PR introduces the ability to create shared packages that contains only models and the protocol file. This shared package depends exclusively on the serverpod_serialization package and is safe to be imported in both the server and client code.

Closes: #3339
Closes: #993
Closes: #3463

The shared package is tied to the project through the shared_packages field in the config/generator.yaml file, but the models and protocol file are generated in the package's own directory. This means that the code on the shared package is generated as part of the serverpod generate command called for the project.

# config/generator.yaml
shared_packages:
  - ../shared_package
  - ../another_shared_package

After merge, we can work on having this shared package as available in the template to close the original issue: #1256.

Related:

Restrictions

Nearly all features are supported on shared models, with the exception of:

  • Specifying the table property, since the models are not tied to a database.
  • Setting the scope property to serverOnly on model fields, since models are shared.
  • Setting the serverOnly property on the model class, since models are shared.

Notes for reviewers

This PR can be reviewed commit by commit for a better comprehension of the changes. The main changes are:

  1. Addition of the configuration on the generator.yaml file.
  2. Support on the ModelSource and SerializableModelDefinition to identify shared models.
  3. Enhancement of the TypeDefinition to generate correct references to shared models.
  4. Filtering of the shared models on the DartServerCodeGenerator and DartClientCodeGenerator.
  5. Implementation of the DartSharedCodeGenerator as a copy of the DartClientCodeGenerator, but targeting the shared models.

Regarding tests

Since this feature introduces a new DartSharedCodeGenerator, the applying tests for the DartServerCodeGenerator and DartClientCodeGenerator were copied and adapted to the new generator. There are only two test files that cover functionality specific to the shared package generator and are worth reviewing:

  • shared_code_generator_test.dart
  • shared_models_extends_test.dart
  • shared_models_restrictions_test.dart

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

None.

@marcelomendoncasoares marcelomendoncasoares changed the title Allow generating shared models between server and client feat: Allow generating shared models between server and client Feb 17, 2026
@marcelomendoncasoares
marcelomendoncasoares marked this pull request as ready for review February 17, 2026 03:32
@marcelomendoncasoares
marcelomendoncasoares requested a review from a team February 17, 2026 03:32

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Mostly questions..

@FXschwartz

Copy link
Copy Markdown
Collaborator

@marcelomendoncasoares Would this allow us to define a .spy.yaml table definition using a separately defined model in this shared package?

For instance if I defined a Car model in this shared package could I then go over to the server and create a car.spy.yaml and reference this shared Car model but then define the table as well?

Let me know if this is off topic and I can create a feature request to move the discussions to.

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator Author

Would this allow us to define a .spy.yaml table definition using a separately defined model in this shared package?

For instance if I defined a Car model in this shared package could I then go over to the server and create a car.spy.yaml and reference this shared Car model but then define the table as well?

Let me know if this is off topic and I can create a feature request to move the discussions to.

@FXschwartz Precisely! Shared packages won't allow the definition of table models, but you'll be able to declare a base model on your shared package and extend it on the server to add a table key.

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Top dollar..

@marcelomendoncasoares
marcelomendoncasoares merged commit 88fd683 into serverpod:main Feb 19, 2026
72 checks passed
@marcelomendoncasoares
marcelomendoncasoares deleted the common-package branch February 19, 2026 10:09
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

3 participants