Skip to content

feat: Interface Model Support after Refactor - #3292

Open
BenAuerDev wants to merge 29 commits into
serverpod:mainfrom
BenAuerDev:feat/interface-support-after-refactor
Open

BenAuerDev wants to merge 29 commits into
serverpod:mainfrom
BenAuerDev:feat/interface-support-after-refactor

Conversation

@BenAuerDev

@BenAuerDev BenAuerDev commented Mar 9, 2025 •

Copy link
Copy Markdown
Contributor

This PR enables the user to declare interfaces and classes that use the implements keyword to implement them.

It's a newer version of #3248 after #3270 came with a refactor that split ClassDefinition to be a baseclass that is extended by ModelClassDefinition and ExceptionClassDefinition. Therefore I added InterfaceClassDefinition and wrote the generation logic accordingly 🚀

This is done in 4 steps/commits:

  1. Enable interface and implements keywords
  2. Enable generating interfaces and classes implementing them
  3. Exclude interfaces from serialization in protocol
  4. Add Examples to serverpod test project

The changes are closer described in each commit message

This PR tries to close #2842

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

No breaking changes.

@SandPod SandPod mentioned this pull request Mar 10, 2025
8 tasks done

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

Nice work getting this feature into the code 🚀

There is still some massaging left that needs to be done before this is ready for production and test covered to ensure future devs doesn't break it.

@@ -0,0 +1,4 @@
exception: ExceptionWithInterface

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.

Suggestion: Add coverage for all cases listed below:

  • Model that inherits from multiple interfaces
  • Model that inherits from interface that inherits from another interface

For the model that inherits from multiple interfaces it would be good if the interfaces have a name collisions for one of the fields but it is the same type (should be OK).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'll add this last again :)

@SandPod

SandPod commented Mar 25, 2025

Copy link
Copy Markdown
Contributor

Nice progress! I mostly checked the comments that you had resolved and try to answer any open questions. Let me know when it is ready for a full review again :)

@BenAuerDev
BenAuerDev force-pushed the feat/interface-support-after-refactor branch from f4c12d0 to 757f788 Compare March 30, 2025 10:14
@BenAuerDev

Copy link
Copy Markdown
Contributor Author

I rebased the branch. Only the last 3 commits are new

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

Seems like there are comments that have not been submitted (by me) and some comments that have not been addressed.

Please have a look again and remember to expand the comments section when inspecting the PR.

Comment on lines +495 to +498
SourceSpanSeverityException(
'Conflicting types for field "${entry.key}" in implemented interfaces: $type vs ${field.type.className}',
span,
),

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.

Suggestion: Please add a test that validates this.

I couldn't find a test where this is validated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

in implements_test.dart line 418 :)

Comment on lines +42 to +49
ValidateNode(
Keyword.parent,
isDeprecated: true,
isRemoved: true,
alternativeUsageMessage:
'Use the relation keyword instead. E.g. relation(parent=parent_table). Note that the default onDelete action changes from "Cascade" to "NoAction" when using the relation keyword.',
),
ValidateNode(

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.

Yes! This should be a "top" level keyword, that should be required to be set in order for these "table" specific keywords to be used.

The required logic can be added to the validateRelationKey logic.

Comment on lines +42 to +49
ValidateNode(
Keyword.parent,
isDeprecated: true,
isRemoved: true,
alternativeUsageMessage:
'Use the relation keyword instead. E.g. relation(parent=parent_table). Note that the default onDelete action changes from "Cascade" to "NoAction" when using the relation keyword.',
),
ValidateNode(

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.

This does not seem to have been resolved.

@BenAuerDev
BenAuerDev force-pushed the feat/interface-support-after-refactor branch from 757f788 to 7430144 Compare May 1, 2025 08:33
@BenAuerDev

Copy link
Copy Markdown
Contributor Author

Did another rebase, to be up to date with the latest changes on main

@coderabbitai

coderabbitai Bot commented Aug 3, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

definitions:

1. Add `ImplementsDefinition`s (incl. unresolved and resolved) with doc comments

2. Add comments to `InheritanceDefinition`s to make it easier to distinguish from `ImplementsDefinition`s without looking up.

3. Add `InterfaceClassDefinition`

4. Modify `ClassDefinition`:

- add field `isImplementing`

- add getter `implementedInterfaces`

- add getter `implementedFields`

- add getter `uniqueImplementedFields`

- add getter `fieldsIncludingImplemented`

5. Modify `ModelClassDefintion`:

- pass `isImplementing` to super

- add getter `allFields` (inherited, implemented, own)

entity_dependency_resolver: add `_resolveImplements`

model_analyzer: add `InterfaceClass` to analyzer

model_parser:

1. add `serializeInterfaceClassFile`

2. add `parseIsImplementing`

keywords: add `interfaceType` and `isImplementing` to keywords

restrictions:

1. add restrictions to validate `InterfaceNames`

2. add restrictions to dissallow redefining a implementedField without default value

class_yaml_definition and exception_yaml_definition: add `ValidateNode` for `IsImplementing`

interface_yaml_definition: add `InterfaceYamlDefinition` for analyzer

extra_properties_test, inheritance_test and model_types_test: update tests returning a set of white listed node names as `interface` is now a white listed

interface_test: test `analyzer` and `restrictions`
model_library_generator:

1. add logic to generate a InterfaceClass
2. add logic to handle implementing interfaces
3. update logic handling fields
4. add logic to apply the `override` annotation to fields from implemented interfaces

model_class_definition_builder: add `isImplementing` with setter

exception_class_definition_builder: add `isImplementing` and `withSimpleField`

interface_class_definition_builder: create class to test `InterfaceClasses`

compilation_unit_helpers: add `tryFindImplementedClass` for tests

interface_class_test: add test for generating interface classes
similar to the sealed classes, we don't serialize `interfaces` in `library_generator` I renamed

unsealedModels => serializableModels

and added a rule that excludes `InterfaceClassDefinition`s
I forgot to remove that after copying the setup
…ype and add tests validating duplicated fields and implementing interfaces from another module
@BenAuerDev
BenAuerDev force-pushed the feat/interface-support-after-refactor branch from 5127c63 to 37c041f Compare August 3, 2025 10:01
@BenAuerDev

BenAuerDev commented Aug 3, 2025 •

Copy link
Copy Markdown
Contributor Author

I did a rebase but couldn't activate the local cli to regenerate the files with conflicts yet

edit: I managed to fix what I lost in the rebase and generate the protocol.

Now only two tests are failing, I feel this is because the additional validation I added for interface support somehow changes the order in which the classes are validated. 🤔

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Interface" model support.

2 participants