Skip to content

Deserialization in tests fails if endpoint returns a record #4135

Closed

Description

@TheLastGimbus

Describe the bug

When return type of an endpoint function is a record, deserialization logic iniside generated test utilities breaks, and tries to deserialize-already-deserialized objects:

// greeting_endpoint.dart
Future<(Greeting, String)> hello(Session session, String name) async {
  return (
    Greeting(
      message: 'Hello $name',
      author: 'Serverpod',
      timestamp: DateTime.now(),
    ),
    name
  );
}
withServerpod('Given Greeting endpoint', (sessionBuilder, endpoints) {
  test(
    'when calling `hello` with name then returned greeting includes name',
    () async {
      // Call the endpoint method by using the `endpoints` parameter and
      // pass `sessionBuilder` as a first argument. Refer to the docs on
      // how to use the `sessionBuilder` to set up different test scenarios.
      final greeting = await endpoints.greeting.hello(sessionBuilder, 'Bob');
      expect(greeting.$1.message, 'Hello Bob');
    },
  );
});
Server default listening on port 8080
package:serverpod_records_test_server/src/generated/protocol.dart 37:36  Protocol.deserialize
package:serverpod_records_test_server/src/generated/protocol.dart 44:9   Protocol.deserialize
test/integration/test_tools/serverpod_test_tools.dart 135:32             _GreetingEndpoint.hello.<fn>.<fn>
===== asynchronous gap ===========================
test/integration/test_tools/serverpod_test_tools.dart 129:33             _GreetingEndpoint.hello.<fn>
===== asynchronous gap ===========================
package:serverpod_test/src/function_call_wrappers.dart 12:12             callAwaitableFunctionAndHandleExceptions
===== asynchronous gap ===========================
test/integration/greeting_endpoint_test.dart 19:26                       main.<fn>.<fn>

type '_GreetingImpl' is not a subtype of type 'Map<String, dynamic>'

For some reason, stuff inside to-deserialize map that represents a record, is, already, deserialized

Image

Then, it tries to deserialize them individually:

Image

...but it already is:

Image

...and it's not a Map

To reproduce

I created a sample project with Serverpod Mini - just git pull https://github.com/TheLastGimbus/serverpod_records_test, and run dart test inside server

Expected behavior

It should work -_-

Library version

2.9.1

Platform information

Well, Linux, but i doubt it matters

Additional context

Maybe I have a same problem as #3840

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

Activity

  1. added
    bug
    on Oct 31, 2025
  2. TheLastGimbus commented on Oct 31, 2025

    @TheLastGimbus
    Author

    Okay, I did this

    Map<String, dynamic>? mapRecordToJson(Record? record) {
      if (record == null) {
        return null;
      }
      if (record is (_i4.Greeting, String)) {
        return {
          "p": [
            record.$1.toJson(), // <- ‼️THIS ‼️
            record.$2,
          ],
        };
      }
      throw Exception('Unsupported record type ${record.runtimeType}');
    }

    And it works 🎉

    I'm not brave to say that I understand all of this generator black magic, but I guess someone forgot it and it can just be added

    I found proper place in the generator and "fixed" it - works for me for now 🎉

    Will drop a PR soon

  3. added
    area: endpoints
    area: test framework
    and removed
    area: endpoints
    on Nov 6, 2025
  4. moved this from Inbox 📫 to Contributor In Progress 👩‍💻 in Serverpod Frameworkson Nov 6, 2025
  5. SandPod commented on Nov 6, 2025

    @SandPod
    Contributor

    @TheLastGimbus Thank you for taking on fixing this!

    The PR is currently not passing our CI pipeline. Give me a ping once it is green for review.

  6. moved this from Contributor In Progress 👩‍💻 to Done 🚀 in Serverpod Frameworkson Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: test frameworkbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions