Repository navigation
fix: Handle serialization of empty Maps with keys other than String - #3803
Conversation
📝 WalkthroughWalkthroughThe changes introduce and update test cases to verify correct handling of empty maps for various key types (including non-string keys) in serialization, deserialization, endpoint parameters, and database round-trips. Generator logic is refactored to consistently serialize empty maps, especially those with non-string keys, by conditionally converting them to empty lists. Parameter and return type handling in code generation is centralized and extended. Changes
Sequence Diagram(s)sequenceDiagram
participant TestCase
participant Client
participant ServerEndpoint
participant SerializationManager
participant Database
TestCase->>Client: Call endpoint with empty map parameter
Client->>SerializationManager: Serialize empty map (as empty list if non-string key)
SerializationManager-->>Client: Serialized parameter
Client->>ServerEndpoint: Send serialized parameter
ServerEndpoint->>SerializationManager: Deserialize parameter (empty map)
ServerEndpoint-->>Client: Return empty map
Client->>SerializationManager: Deserialize response
SerializationManager-->>Client: Empty map
Client-->>TestCase: Assert empty map result
TestCase->>ServerEndpoint: Insert object with empty map to DB
ServerEndpoint->>Database: Store object
Database-->>ServerEndpoint: Confirm insert
ServerEndpoint->>Database: Retrieve object by ID
Database-->>ServerEndpoint: Object with empty map
ServerEndpoint-->>TestCase: Assert retrieved map is empty
Estimated code review effort3 (30–60 minutes) 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
3c22fe2 to
22d6baa
Compare
…e for empty `Map<int, int>`
Map<int, int>Maps with keys other than String
18b2290 to
40c46cf
Compare
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
For Maps with keys other than `String`, we usually rely on the `toEncodable` parameter of the `JsonEncoder`, which calls to custom mapping code when it can not handle serializing the object to JSON internally.
While this works e.g. for a `<int, String>{1: 'hello'}`, it does not for `<int, String>{}`, because the JSON encode looks at the actual data of the map, and not it's type. And thus it can perfectly fine convert it to JSON as `"{}"`.
The issue is then, that we expect all such non-String-keyed Maps to be encoded as lists (`[]`) with objects inside contains `k`/`v` keys.
Thus the later deserialization fails, as the empty map came as `"{}"` instead of `"[]"`.
Interestingly this worked for models containing such maps, because there the model's `toJson` code was always invoked and that converted each non-String-keyed child map to a list.
So the issue was limited to only top-level `Map` parameters on endpoint methods.
Isakdl
left a comment
There was a problem hiding this comment.
Good work! I had some suggestions on a few points!
One thing I would like to be added is additional tests to validate nested Maps making sure we support that too, this is supported inside models already (and there are tests for it!), might be good to validate that the empty map case is covered too!
Also fix detection of "records inside containers insides records", which would not get mapped before
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart (1)
5231-5256: Missing Protocol().deserialize for consistency.Similar to the previous comment, this method should use
_i17.Protocol().deserializefor consistency with other methods.Apply this diff:
- var _localReturnValue = await (_localCallContext.method.call( - _localUniqueSession, - _localCallContext.arguments, - ) as _i3.Future<Map<String, _i18.TestEnum>>); + var _localReturnValue = await _localCallContext.method + .call( + _localUniqueSession, + _localCallContext.arguments, + ) + .then((map) => + _i17.Protocol().deserialize<Map<String, _i18.TestEnum>>(map));
♻️ Duplicate comments (2)
tests/serverpod_test_server/test/serialization_legacy_test.dart (1)
478-514: Test logic is correct but consider file placement.The test comprehensively validates that empty maps of various types are preserved through serialization/deserialization, directly addressing the PR objectives. However, as noted in a previous review, this test should ideally be moved to the non-legacy serialization test file for better organization.
tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart (1)
597-650: Extension correctly handles maps with non-string keys, but architectural concerns remain.The new logic properly extends deserialization support to future-returned maps with non-string keys, following the same pattern as record handling. The implementation is consistent and correct.
However, this addition reinforces the architectural concern mentioned in previous reviews about server test tools not using the full serialization/deserialization flow that normal E2E calls use. This continues the pattern of adding mapping workarounds rather than addressing the root cause.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (15)
tests/serverpod_test_client/lib/src/protocol/client.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_client/lib/src/protocol/protocol.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_client/lib/src/protocol/subfolder/model_in_subfolder.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_client/lib/src/protocol/types_list.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_client/lib/src/protocol/types_map.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_client/lib/src/protocol/types_set.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_module/serverpod_test_module_client/lib/src/protocol/protocol.dartis excluded by!**/lib/src/protocol/**tests/serverpod_test_module/serverpod_test_module_server/lib/src/generated/protocol.dartis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/endpoints.dartis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/protocol.dartis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/protocol.yamlis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/subfolder/model_in_subfolder.dartis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/types_list.dartis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/types_map.dartis excluded by!**/generated/**tests/serverpod_test_server/lib/src/generated/types_set.dartis excluded by!**/generated/**
📒 Files selected for processing (12)
tests/serverpod_test_server/lib/src/endpoints/map_parameters.dart(1 hunks)tests/serverpod_test_server/lib/src/endpoints/record_parameters.dart(2 hunks)tests/serverpod_test_server/test/serialization_legacy_test.dart(2 hunks)tests/serverpod_test_server/test/serialization_test.dart(4 hunks)tests/serverpod_test_server/test_e2e/connection_test.dart(2 hunks)tests/serverpod_test_server/test_e2e/database_roundtrip_type_test.dart(1 hunks)tests/serverpod_test_server/test_e2e/record_types_test.dart(4 hunks)tests/serverpod_test_server/test_integration/test_tools/protocol_test.dart(1 hunks)tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart(55 hunks)tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart(12 hunks)tools/serverpod_cli/lib/src/generator/dart/library_generators/model_library_generator.dart(1 hunks)tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart(5 hunks)
🧠 Learnings (8)
tests/serverpod_test_server/test/serialization_legacy_test.dart (4)
Learnt from: tp
PR: #3625
File: packages/serverpod/lib/src/cloud_storage/public_endpoint.dart:0-0
Timestamp: 2025-07-04T07:30:27.709Z
Learning: In Dart, ByteData.sublistView() is safer than ByteData.view() when working with TypedData views like Uint8List. ByteData.view() creates a view from a raw ByteBuffer without considering the original view's offset/length, potentially exposing the entire underlying buffer. ByteData.sublistView() properly accounts for data.offsetInBytes, data.elementSizeInBytes, and data.lengthInBytes, ensuring the view respects the original bounds. When converting TypedData to ByteData, use ByteData.sublistView(data) instead of ByteData.view(data.buffer).
Learnt from: nielsenko
PR: #3625
File: packages/serverpod/lib/src/cloud_storage/public_endpoint.dart:82-93
Timestamp: 2025-07-03T09:16:08.642Z
Learning: In Serverpod's CloudStoragePublicEndpoint, the _readBinaryBody method must buffer the entire request body in memory because the storage.storeFile interface requires a ByteData object. Implementing true streaming uploads would require reworking Serverpod's serialization handling of binary arguments. The current implementation uses BytesBuilder for efficient byte accumulation instead of list concatenation.
Learnt from: nielsenko
PR: #3625
File: packages/serverpod/lib/src/web_server/routes/static_directory.dart:155-157
Timestamp: 2025-07-03T09:33:09.654Z
Learning: In Dart, Stream.cast() returns Stream where R (the element type) is inferred from context through Dart's downward inference system. For example, when Body.fromDataStream expects Stream, calling file.openRead().cast() will automatically infer R as Uint8List, effectively becoming cast(). The cast method transforms each element of the stream using 'as R'.
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
tests/serverpod_test_server/test_integration/test_tools/protocol_test.dart (2)
Learnt from: tp
PR: #3672
File: tests/serverpod_test_server/test_integration/test_tools/test_session_builder_test.dart:10-30
Timestamp: 2025-06-18T14:49:11.263Z
Learning: In Serverpod test files, tp prefers using nullable late variables for AuthenticationInfo in tests (e.g., late AuthenticationInfo? authenticationInfo;) rather than non-nullable ones to avoid using ! operators and to get clearer error messages when authentication setup fails. This provides better diagnostics when session.authenticated returns null.
Learnt from: tp
PR: #3651
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/test/integration/email_account_endpoint_test.dart:1-1
Timestamp: 2025-06-19T10:25:15.101Z
Learning: Explicit tests for complex endpoint methods provide cleaner error reporting and better debugging experience compared to testing the same functionality only within setUp blocks or helper methods.
tests/serverpod_test_server/test_e2e/connection_test.dart (1)
Learnt from: tp
PR: #3672
File: tests/serverpod_test_server/test_integration/test_tools/test_session_builder_test.dart:10-30
Timestamp: 2025-06-18T14:49:11.263Z
Learning: In Serverpod test files, tp prefers using nullable late variables for AuthenticationInfo in tests (e.g., late AuthenticationInfo? authenticationInfo;) rather than non-nullable ones to avoid using ! operators and to get clearer error messages when authentication setup fails. This provides better diagnostics when session.authenticated returns null.
tests/serverpod_test_server/test_e2e/database_roundtrip_type_test.dart (1)
Learnt from: tp
PR: #3651
File: modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts.dart:35-39
Timestamp: 2025-06-16T12:07:26.018Z
Learning: The withServerpod test helper runs tests within a transaction, which may require adapting database queries to accept transaction parameters for proper snapshot consistency during testing.
tests/serverpod_test_server/test_e2e/record_types_test.dart (1)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart (7)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
Learnt from: tp
PR: #3747
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/pubspec.yaml:37-38
Timestamp: 2025-07-02T16:09:27.702Z
Learning: In Serverpod migration testing, it's appropriate for test servers like serverpod_new_auth_test_server to include dependencies on both legacy packages (e.g., serverpod_auth_server) and new modular packages to enable comprehensive testing of migration paths from old to new authentication systems.
Learnt from: tp
PR: #3672
File: tests/serverpod_test_server/test_integration/test_tools/test_session_builder_test.dart:10-30
Timestamp: 2025-06-18T14:49:11.263Z
Learning: In Serverpod test files, tp prefers using nullable late variables for AuthenticationInfo in tests (e.g., late AuthenticationInfo? authenticationInfo;) rather than non-nullable ones to avoid using ! operators and to get clearer error messages when authentication setup fails. This provides better diagnostics when session.authenticated returns null.
Learnt from: tp
PR: #3651
File: modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts.dart:35-39
Timestamp: 2025-06-16T12:07:26.018Z
Learning: The withServerpod test helper runs tests within a transaction, which may require adapting database queries to accept transaction parameters for proper snapshot consistency during testing.
Learnt from: exaby73
PR: #3741
File: modules/serverpod_auth/serverpod_auth_server/analysis_options.yaml:14-16
Timestamp: 2025-07-01T13:01:27.054Z
Learning: In Serverpod, first-party packages like serverpod_auth_server can appropriately use blanket invalid_use_of_internal_member: ignore directives in their analysis_options.yaml since they use internal Database methods internally without exposing them in their public APIs to end users.
Learnt from: tp
PR: #3721
File: modules/new_serverpod_auth/serverpod_auth_session/serverpod_auth_session_flutter/test/serverpod_auth_session_flutter_test.dart:1-7
Timestamp: 2025-06-27T07:44:09.048Z
Learning: In Serverpod packages, the uuid package is re-exported through a chain: serverpod_auth_session_client re-exports serverpod_client, which re-exports serverpod_serialization, which re-exports uuid. This means Uuid class is available in files that import serverpod_auth_session_client without needing a direct uuid package import.
Learnt from: tp
PR: #3721
File: util/pub_get_all:0-0
Timestamp: 2025-06-27T08:39:36.440Z
Learning: In the Serverpod project, dart pub get commands work fine on Flutter packages both locally and in CI environments, even when the package has flutter: sdk: flutter dependencies in pubspec.yaml. The technical constraint of Flutter SDK dependencies causing dart pub failures may not apply in all environments or recent Dart versions.
tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart (3)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
Learnt from: tp
PR: #3651
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/test/integration/email_account_endpoint_test.dart:1-1
Timestamp: 2025-06-19T10:25:15.101Z
Learning: When reviewing test coverage, consider that endpoint methods that simply forward to underlying service classes don't necessarily need their own tests if the underlying functionality is well-tested, especially when they get indirect testing through helper methods.
Learnt from: tp
PR: #3651
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/test/integration/email_account_endpoint_test.dart:1-1
Timestamp: 2025-06-19T10:25:15.101Z
Learning: Explicit tests for complex endpoint methods provide cleaner error reporting and better debugging experience compared to testing the same functionality only within setUp blocks or helper methods.
tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart (1)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
🧰 Additional context used
🧠 Learnings (8)
tests/serverpod_test_server/test/serialization_legacy_test.dart (4)
Learnt from: tp
PR: #3625
File: packages/serverpod/lib/src/cloud_storage/public_endpoint.dart:0-0
Timestamp: 2025-07-04T07:30:27.709Z
Learning: In Dart, ByteData.sublistView() is safer than ByteData.view() when working with TypedData views like Uint8List. ByteData.view() creates a view from a raw ByteBuffer without considering the original view's offset/length, potentially exposing the entire underlying buffer. ByteData.sublistView() properly accounts for data.offsetInBytes, data.elementSizeInBytes, and data.lengthInBytes, ensuring the view respects the original bounds. When converting TypedData to ByteData, use ByteData.sublistView(data) instead of ByteData.view(data.buffer).
Learnt from: nielsenko
PR: #3625
File: packages/serverpod/lib/src/cloud_storage/public_endpoint.dart:82-93
Timestamp: 2025-07-03T09:16:08.642Z
Learning: In Serverpod's CloudStoragePublicEndpoint, the _readBinaryBody method must buffer the entire request body in memory because the storage.storeFile interface requires a ByteData object. Implementing true streaming uploads would require reworking Serverpod's serialization handling of binary arguments. The current implementation uses BytesBuilder for efficient byte accumulation instead of list concatenation.
Learnt from: nielsenko
PR: #3625
File: packages/serverpod/lib/src/web_server/routes/static_directory.dart:155-157
Timestamp: 2025-07-03T09:33:09.654Z
Learning: In Dart, Stream.cast() returns Stream where R (the element type) is inferred from context through Dart's downward inference system. For example, when Body.fromDataStream expects Stream, calling file.openRead().cast() will automatically infer R as Uint8List, effectively becoming cast(). The cast method transforms each element of the stream using 'as R'.
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
tests/serverpod_test_server/test_integration/test_tools/protocol_test.dart (2)
Learnt from: tp
PR: #3672
File: tests/serverpod_test_server/test_integration/test_tools/test_session_builder_test.dart:10-30
Timestamp: 2025-06-18T14:49:11.263Z
Learning: In Serverpod test files, tp prefers using nullable late variables for AuthenticationInfo in tests (e.g., late AuthenticationInfo? authenticationInfo;) rather than non-nullable ones to avoid using ! operators and to get clearer error messages when authentication setup fails. This provides better diagnostics when session.authenticated returns null.
Learnt from: tp
PR: #3651
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/test/integration/email_account_endpoint_test.dart:1-1
Timestamp: 2025-06-19T10:25:15.101Z
Learning: Explicit tests for complex endpoint methods provide cleaner error reporting and better debugging experience compared to testing the same functionality only within setUp blocks or helper methods.
tests/serverpod_test_server/test_e2e/connection_test.dart (1)
Learnt from: tp
PR: #3672
File: tests/serverpod_test_server/test_integration/test_tools/test_session_builder_test.dart:10-30
Timestamp: 2025-06-18T14:49:11.263Z
Learning: In Serverpod test files, tp prefers using nullable late variables for AuthenticationInfo in tests (e.g., late AuthenticationInfo? authenticationInfo;) rather than non-nullable ones to avoid using ! operators and to get clearer error messages when authentication setup fails. This provides better diagnostics when session.authenticated returns null.
tests/serverpod_test_server/test_e2e/database_roundtrip_type_test.dart (1)
Learnt from: tp
PR: #3651
File: modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts.dart:35-39
Timestamp: 2025-06-16T12:07:26.018Z
Learning: The withServerpod test helper runs tests within a transaction, which may require adapting database queries to accept transaction parameters for proper snapshot consistency during testing.
tests/serverpod_test_server/test_e2e/record_types_test.dart (1)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart (7)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
Learnt from: tp
PR: #3747
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/pubspec.yaml:37-38
Timestamp: 2025-07-02T16:09:27.702Z
Learning: In Serverpod migration testing, it's appropriate for test servers like serverpod_new_auth_test_server to include dependencies on both legacy packages (e.g., serverpod_auth_server) and new modular packages to enable comprehensive testing of migration paths from old to new authentication systems.
Learnt from: tp
PR: #3672
File: tests/serverpod_test_server/test_integration/test_tools/test_session_builder_test.dart:10-30
Timestamp: 2025-06-18T14:49:11.263Z
Learning: In Serverpod test files, tp prefers using nullable late variables for AuthenticationInfo in tests (e.g., late AuthenticationInfo? authenticationInfo;) rather than non-nullable ones to avoid using ! operators and to get clearer error messages when authentication setup fails. This provides better diagnostics when session.authenticated returns null.
Learnt from: tp
PR: #3651
File: modules/new_serverpod_auth/serverpod_auth_email_account/serverpod_auth_email_account_server/lib/src/business/email_accounts.dart:35-39
Timestamp: 2025-06-16T12:07:26.018Z
Learning: The withServerpod test helper runs tests within a transaction, which may require adapting database queries to accept transaction parameters for proper snapshot consistency during testing.
Learnt from: exaby73
PR: #3741
File: modules/serverpod_auth/serverpod_auth_server/analysis_options.yaml:14-16
Timestamp: 2025-07-01T13:01:27.054Z
Learning: In Serverpod, first-party packages like serverpod_auth_server can appropriately use blanket invalid_use_of_internal_member: ignore directives in their analysis_options.yaml since they use internal Database methods internally without exposing them in their public APIs to end users.
Learnt from: tp
PR: #3721
File: modules/new_serverpod_auth/serverpod_auth_session/serverpod_auth_session_flutter/test/serverpod_auth_session_flutter_test.dart:1-7
Timestamp: 2025-06-27T07:44:09.048Z
Learning: In Serverpod packages, the uuid package is re-exported through a chain: serverpod_auth_session_client re-exports serverpod_client, which re-exports serverpod_serialization, which re-exports uuid. This means Uuid class is available in files that import serverpod_auth_session_client without needing a direct uuid package import.
Learnt from: tp
PR: #3721
File: util/pub_get_all:0-0
Timestamp: 2025-06-27T08:39:36.440Z
Learning: In the Serverpod project, dart pub get commands work fine on Flutter packages both locally and in CI environments, even when the package has flutter: sdk: flutter dependencies in pubspec.yaml. The technical constraint of Flutter SDK dependencies causing dart pub failures may not apply in all environments or recent Dart versions.
tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart (3)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
Learnt from: tp
PR: #3651
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/test/integration/email_account_endpoint_test.dart:1-1
Timestamp: 2025-06-19T10:25:15.101Z
Learning: When reviewing test coverage, consider that endpoint methods that simply forward to underlying service classes don't necessarily need their own tests if the underlying functionality is well-tested, especially when they get indirect testing through helper methods.
Learnt from: tp
PR: #3651
File: tests/serverpod_new_auth_test/serverpod_new_auth_test_server/test/integration/email_account_endpoint_test.dart:1-1
Timestamp: 2025-06-19T10:25:15.101Z
Learning: Explicit tests for complex endpoint methods provide cleaner error reporting and better debugging experience compared to testing the same functionality only within setUp blocks or helper methods.
tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart (1)
Learnt from: tp
PR: #3747
File: modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/lib/src/business/migrating_email_endpoint_base.dart:4-4
Timestamp: 2025-07-02T16:07:50.947Z
Learning: In Serverpod, tp prefers importing the full package (e.g., 'package:serverpod_auth_migration_server/serverpod_auth_migration_server.dart') as a baseline rather than using smaller local imports, even within the same package where it might be considered a "self-import".
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Migration e2e tests (3.24.0)
- GitHub Check: Migration e2e tests (3.32.1)
🔇 Additional comments (35)
tests/serverpod_test_server/test_integration/test_tools/protocol_test.dart (1)
163-172: LGTM: Test correctly validates empty non-string-keyed map handling.This test case directly addresses the core issue described in the PR objectives - ensuring empty maps with non-string keys (like
Map<int, int>) are properly serialized and deserialized through endpoint calls.tests/serverpod_test_server/test/serialization_test.dart (1)
313-313: LGTM: Function rename improves clarity and generality.The renaming from
mapRecordContainingContainerToJsontomapContainerToJsonbetter reflects the function's expanded purpose of handling both record-containing containers and non-string-keyed maps.Also applies to: 347-347, 380-380, 413-413
tests/serverpod_test_server/lib/src/endpoints/map_parameters.dart (1)
45-50: LGTM: Method supports testing of nested non-string-keyed maps.The implementation follows the established pattern in this endpoint class and provides necessary infrastructure for testing nested map serialization, which is relevant to the PR's objectives.
tests/serverpod_test_server/test_e2e/database_roundtrip_type_test.dart (1)
264-280: LGTM: Database roundtrip test ensures persistence of empty non-string-keyed maps.This test correctly verifies that empty maps with non-string keys survive database storage and retrieval, which is crucial for the fix described in the PR objectives. The test structure is consistent with the existing pattern in the file.
tests/serverpod_test_server/test_e2e/connection_test.dart (3)
421-429: LGTM! Excellent test coverage for the nested empty map edge case.This test directly addresses the core issue described in the PR - ensuring empty maps with non-string keys are properly serialized and deserialized when nested within other maps.
431-434: LGTM! Essential test case for the primary bug fix.This test verifies that empty maps with integer keys are properly handled, which was the core serialization issue described in the PR objectives.
448-452: LGTM! Comprehensive coverage for enum-keyed empty maps.This test ensures that the fix works for all non-string key types, including enums, providing thorough coverage of the serialization issue.
tools/serverpod_cli/lib/src/generator/dart/library_generators/model_library_generator.dart (1)
1193-1210: Constants verified
mapRecordToJsonFuncNameandmapContainerToJsonFunctionNameare declared as top-level constants inlibrary_generator.dartand correctly imported intomodel_library_generator.dart. No further action required.tests/serverpod_test_server/test_e2e/record_types_test.dart (5)
143-151: LGTM! Test covers empty string-keyed map scenario.This test verifies that empty maps with string keys are correctly serialized and deserialized when contained within records.
163-171: Excellent coverage for the core PR objective.This test specifically addresses the main issue described in the PR: handling empty maps with non-string keys (int in this case). The test verifies that an empty
Map<int, int>within a record is correctly serialized and returned as an empty map rather than causing deserialization failures.
173-181: Good complementary test for non-empty non-string-keyed maps.This test ensures that the fix doesn't break the existing functionality for non-empty maps with non-string keys.
274-296: Comprehensive test coverage for complex nested scenarios.These tests cover:
- Named records with empty non-string-keyed maps (lines 274-283)
- Named records with nested sets containing records (lines 285-296)
This ensures the serialization fix works correctly in more complex nested structures.
249-250: Minor formatting improvements.Good cleanup of extra spaces in test descriptions for consistency.
Also applies to: 261-262
tests/serverpod_test_server/lib/src/endpoints/record_parameters.dart (3)
73-85: Well-structured endpoint methods for map serialization testing.These methods provide the necessary server-side endpoints to test:
- String-keyed maps in records (lines 73-78)
- Non-string-keyed maps in records (lines 80-85)
The simple pass-through implementation is appropriate for testing serialization/deserialization behavior. The non-string-keyed map method directly supports testing the core PR objective.
87-92: Good addition for testing nested record structures.This method handles records containing sets with nested records, which tests more complex serialization scenarios beyond just maps.
133-145: Comprehensive coverage for named record scenarios.These methods extend the testing to named records containing:
- Non-string-keyed maps (lines 133-138)
- Sets with nested records (lines 140-145)
This ensures the serialization fix works correctly across different record structure types.
tests/serverpod_test_server/test_integration/test_tools/serverpod_test_tools.dart (9)
35-36: Import alias refactoring looks good.The swap of import aliases (
_i17for protocol.dart and_i18for test_enum.dart) aligns with the new serialization approach where protocol methods are used for container and record serialization.
5148-5166: Correct implementation for non-string keyed map serialization.The use of
_i17.mapContainerToJson(map)and_i17.Protocol().deserialize<Map<int, int>>(map)properly handles serialization of maps with non-string keys, ensuring empty maps are serialized as[]instead of{}.
5198-5229: Proper handling of enum-keyed maps.The serialization and deserialization of
Map<_i18.TestEnum, int>is correctly implemented using the new pattern.
7738-8175: Consistent record serialization across module endpoints.All module endpoint methods (
ModuleEndpointSubclass,ModuleEndpointAdaptation,ModuleEndpointReduction,ModuleEndpointExtension) correctly use_i17.mapRecordToJsonfor serialization and_i17.Protocol().deserializefor deserialization of record types.
8477-8750: Excellent consistency in record type handling.All record parameter methods correctly handle various record types including:
- Simple records:
(int,),(int, String)- Nullable variations:
(int,)?,(int?,),(int?,)?- Complex records with objects:
(int, _i11.SimpleData)The serialization and deserialization patterns are uniformly applied.
8756-8850: New methods properly handle complex nested structures.The addition of these methods extends support for:
- Records containing string-keyed maps:
(Map<String, int>,)- Records containing non-string-keyed maps:
(Map<int, int>,)- directly addresses PR objectives- Records containing sets with nested records:
(Set<(int,)>,)All follow the established serialization pattern correctly.
8868-9087: Named record methods maintain consistency.All named record methods correctly handle serialization/deserialization, including the new methods:
returnNamedNonStringKeyedMapRecord: Handles({Map<int, int> intIntMap})returnNamedSetWithNestedRecordRecord: Handles({Set<(bool,)> boolSet})These additions complete the support for non-string keyed containers in various record contexts.
9089-9600: Comprehensive support for complex data structures.The implementation correctly handles:
- Record typedefs with proper serialization
- Collections of records (List, Set, Map) using
mapContainerToJson- Nullable collections with appropriate null checks
- Deeply nested structures like
Map<String, List<Set<(int,)>>>All maintain the consistent serialization/deserialization pattern.
9633-12164: Stream methods correctly integrate new serialization.All stream-related methods properly use the new serialization functions within the JSON encoding context:
- Record streams use
_i17.mapRecordToJson- Container streams use
_i17.mapContainerToJson- Nullable initial values are properly handled with conditional checks
The implementation maintains consistency throughout.
tools/serverpod_cli/lib/src/generator/dart/library_generators/server_test_tools_generator.dart (3)
176-176: LGTM! Function names are now configurable.The function name references are now using configurable constants instead of hardcoded strings, improving consistency and maintainability across the codebase.
Also applies to: 180-180
184-200: Excellent refactoring to centralize parameter handling logic.The new
handleParameterhelper function effectively consolidates the parameter serialization logic that was previously scattered. The implementation correctly handles:
- Record types with
mapRecordToJsonRef- Container types with records or non-string keyed maps with proper null safety
- Regular parameters passed through unchanged
This improves code maintainability and reduces duplication.
231-231: Consistent application of the refactoring.The usage of the new
handleParameterfunction and updated function name references maintain consistency with the centralized approach.Also applies to: 556-556, 560-560
tools/serverpod_cli/lib/src/generator/dart/library_generators/library_generator.dart (7)
12-13: Well-structured constant definitions for function names.Defining these constants improves maintainability by centralizing function name references and preventing typos across the codebase.
353-353: Consistent use of the new constant.The usage of
mapContainerToJsonFunctionNameconstant maintains consistency with the refactoring approach.
781-809: Consistent refactoring maintains uniformity across generators.The
handleParameterhelper function in the client code generation follows the same beneficial pattern as the server test tools, ensuring consistent parameter handling across the codebase.Also applies to: 818-818
968-972: Consistent application of function name constants.The
transformReturnTypemethod correctly uses the configurable function name constants, maintaining consistency across the codebase.
1128-1128: Documentation and implementation properly updated.The function name constant usage is consistent, and the documentation correctly reflects the expanded functionality to handle both records and non-string-keyed maps.
Also applies to: 1142-1153, 1169-1170
1213-1221: Well-implemented type checking for non-string-keyed maps.The
containsNonStringKeyedMapgetter correctly implements recursive checking for non-string-keyed maps within container types, following the same pattern asreturnsRecordInContainer. The recursive logic should properly handle nested scenarios as mentioned in past review comments.
1432-1448: Comprehensive extension to handle non-string-keyed maps.The extensions to
transformReturnTypeandrecordToJsonMapCodeproperly integrate support for non-string-keyed maps, maintaining consistency with the existing record handling patterns. The null safety handling is correctly implemented.Also applies to: 1473-1501
… on the empty map value
Isakdl
left a comment
There was a problem hiding this comment.
I have one more concern on the nested checks, see comment.
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
For Maps with keys other than
String, we usually rely on thetoEncodableparameter of theJsonEncoder, which calls to custom mapping code when it can not handle serializing the object to JSON internally.While this works e.g. for a
<int, String>{1: 'hello'}, it does not for<int, String>{}, because the JSON encode looks at the actual data of the map, and not it's type. And thus it can perfectly fine convert it to JSON as"{}".The issue is then, that we expect all such non-String-keyed Maps to be encoded as lists (
[]) with objects inside containsk/vkeys. Thus the later deserialization fails, as the empty map became as"{}"instead of"[]".Interestingly, this worked for models containing such maps, because there the model's
toJsoncode was always invoked and that converted each non-String-keyed child map to a list.So the issue was limited to only top-level
Mapparameters on endpoint methods and the server-side return types.Relates to #3801, #1806
Closes #1908
Summary by CodeRabbit
Summary by CodeRabbit
Tests
New Features
Refactor