Skip to content

fix: Fix deserialization of collections of serverOnly models - #4433

Merged
marcelomendoncasoares merged 8 commits into
mainfrom
copilot/fix-server-side-serialization
Dec 15, 2025
Merged

marcelomendoncasoares merged 8 commits into
mainfrom
copilot/fix-server-side-serialization

Conversation

Copilot AI commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

Server-side deserialization failed for collections of serverOnly models (e.g., List<Article> where Article is serverOnly: true), throwing DeserializationTypeNotFoundException.

Root cause: allFieldsToGenerateSerialization used shouldSerializeField() to collect field types for deserialization generation. This method excludes serverOnly fields (correct for protocol communication) but prevented their collection types from being generated.

Fix: Use shouldIncludeField() instead, which correctly includes serverOnly fields on server while excluding them on client.

// Before - excluded serverOnly field types from deserialization generation
final allFieldsToGenerateSerialization = unsealedModels
    .whereType<ModelClassDefinition>()
    .expand((m) => m.fields)
    .where((f) => f.shouldSerializeField(serverCode))  // ❌
    .distinct();

// After - includes serverOnly field types on server
final allFieldsToGenerateSerialization = unsealedModels
    .whereType<ModelClassDefinition>()
    .expand((m) => m.fields)
    .where((f) => f.shouldIncludeField(serverCode))  // ✅
    .distinct();

Impact: One-line change in library_generator.dart. No changes to serialization behavior (toJson/toJsonForProtocol still exclude serverOnly fields).

Testing improvements: Protocol generation tests simplified to check for model name presence/absence instead of regex patterns, making them more resilient to implementation changes. Runtime tests reorganized into separate Given groups for better clarity.

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

Breaking changes

None.

Original prompt

This section details on the original issue you should resolve

<issue_title>Broken server-side serialization on serverOnly models</issue_title>
<issue_description>### Describe the bug

I use a wrapper model to serialize the list of another class, since upgrading to serverpod v3 I get (only when serverOnly=true):


ERROR: No deserialization found for type List<Article>
serverpod/serverpod#0      SerializationManager.deserialize (package:serverpod_serialization/src/serialization.dart:108:5)
serverpod/serverpod#1      Protocol.deserialize (package:app_server/src/generated/protocol.dart:1383:18)
serverpod/serverpod#2      new ArticleList.fromJson (package:app_server/src/generated/article/article_list.dart:32:31)
serverpod/serverpod#3      Service.fetchArticlesForCategory (package:app_server/src/services/service.dart:49:32)

To reproduce

article.spy.yml

class: Article
serverOnly: true
fields:
  name: String
  price: double

article_list.spy.yml

class: ArticleList
serverOnly: true
fields:
    results: List<Article>

service.dart

 import 'package:app_server/src/generated/protocol.dart';
      
 final protocol = Protocol(); 
 final articles = protocol.decode<ArticleList>(json);

Expected behavior

I expected serverOnly to prevent the client from serializing the model, while server-side serialization should always work, regardless of whether serverOnly is true or false.

Library version

  • Serverpod version: 3.0.1
  • Dart SDK version: 3.10.1 (stable)
  • Flutter 3.38.3 (stable)

Platform information

  • OS: Arch Linux
  • Kernel: 6.17.9-arch1-1
  • Architecture: x86_64

Additional context

No response

How experienced are you with this library?

Intermediate - Familiar with the basics or have used it in a few projects

Are you interested in working on a PR for this?

  • I want to work on this</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 12, 2025 13:46
Fixed bug where List<ServerOnlyModel> deserialization was not generated on server side.
Root cause: shouldSerializeField() returned false for serverOnly fields even on server,
preventing field types from being included in deserialization generation.

Added test case reproducing the issue with Article and ArticleList serverOnly models.

Co-authored-by: marcelomendoncasoares <[email protected]>
Fixed bug where List<ServerOnlyModel> deserialization was not generated on server side.
Root cause: allFieldsToGenerateSerialization used shouldSerializeField() which filtered
out serverOnly fields. Changed to use shouldIncludeField() instead, which correctly
includes serverOnly fields on the server while excluding them on the client.

shouldSerializeField() is specifically for protocol communication (toJson/toJsonForProtocol),
while shouldIncludeField() is for determining which fields exist in generated code.

Added test case with Article and ArticleList serverOnly models reproducing the issue.

Co-authored-by: marcelomendoncasoares <[email protected]>
Copilot AI changed the title [WIP] Fix broken server-side serialization on serverOnly models Fix serverOnly model deserialization in collections Dec 12, 2025

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

Fix is correct. Now also add tests on tools/serverpod_cli/test/generator/dart for both client and server to attest that the deserialization entry exist on the protocol file. After this, run the generate_all_parallel script again and await until all have finished to fix a broken package after the last generate. Do this in separate commits.

Copilot AI and others added 2 commits December 12, 2025 19:07
Added tests in tools/serverpod_cli/test/generator/dart to verify:
- Server protocol includes deserialization for List<ServerOnlyModel>
- Client protocol excludes deserialization for serverOnly models

Tests confirm the fix works correctly for both client and server code generation.

Co-authored-by: marcelomendoncasoares <[email protected]>
Regenerated all protocol files after the fix. Changes include:
- serverpod_auth_bridge: Added Set<String> deserialization
- serverpod_auth_idp: Added Map<String, String> deserialization
- serverpod_auth_migration: Fixed empty protocol file with complete definitions

All packages now have proper deserialization entries for their field types.

Co-authored-by: marcelomendoncasoares <[email protected]>

Copilot AI commented Dec 12, 2025

Copy link
Copy Markdown
Contributor Author

Fix is correct. Now also add tests on tools/serverpod_cli/test/generator/dart for both client and server to attest that the deserialization entry exist on the protocol file. After this, run the `gen...

Done. Added protocol generation tests for both client and server in commits ac59628 and regenerated all packages with generate_all_parallel in commit 15b68e9. The broken serverpod_auth_migration package protocol file has been fixed.

@marcelomendoncasoares marcelomendoncasoares changed the title Fix serverOnly model deserialization in collections fix: Fix deserialization of collections of serverOnly models Dec 12, 2025
@marcelomendoncasoares
marcelomendoncasoares marked this pull request as ready for review December 13, 2025 03:16
@marcelomendoncasoares
marcelomendoncasoares requested a review from a team December 13, 2025 03:16

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

LGTM!

Two minor suggestions to improve tests.

@marcelomendoncasoares

Copy link
Copy Markdown
Collaborator

@copilot Please address the last review comments on the tests.

- Simplified protocol tests to check for 'Article' string presence/absence instead of regex patterns
- Made tests more resilient to implementation changes (e.g., if->switch refactoring)
- Reorganized server_only_model_list_deserialization_test into separate Given groups for better clarity

Co-authored-by: marcelomendoncasoares <[email protected]>

Copilot AI commented Dec 15, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Please address the last review comments on the tests.

Addressed all review comments in commit 35db199.

@marcelomendoncasoares
marcelomendoncasoares merged commit 6f1ecf2 into main Dec 15, 2025
@marcelomendoncasoares
marcelomendoncasoares deleted the copilot/fix-server-side-serialization branch December 15, 2025 15:48
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broken server-side serialization on serverOnly models

3 participants