Skip to content

Conversation

@KATT
Copy link
Member

@KATT KATT commented Aug 4, 2025

Closes #6863

🎯 Changes

✅ Checklist

  • Try to repro
  • Repro
  • Fix

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of serialization and deserialization in batch streaming and subscription links, ensuring correct invocation with custom transformers.
  • Refactor

    • Replaced local utility for object type checking with a standardized shared version.
    • Updated method signatures in transformer interfaces for consistency.
  • New Features

    • Introduced a utility function to reliably detect plain JavaScript objects.
  • Tests

    • Added tests to verify custom serialization and deserialization with multiple HTTP link types, addressing a previous regression.
@railway-app
Copy link

railway-app bot commented Aug 4, 2025

🚅 Deployed to the trpc-pr-6878 environment in trpc-sse-and-websockets

Service Status Web Updated (UTC)
next-sse-chat ◻️ Removed (View Logs) Web Aug 4, 2025 at 4:29 pm
next-prisma-websockets-starter ◻️ Removed (View Logs) Aug 4, 2025 at 4:29 pm
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 4, 2025

Walkthrough

This change standardizes transformer function signatures, replaces a local isPlainObject implementation with a shared version, and updates how (de)serialization functions are passed to streaming utilities. It introduces a new utility module, modifies transformer interface syntax, and adds targeted tests to verify custom class serialization/deserialization with various HTTP client links.

Changes

Cohort / File(s) Change Summary
Client/Server Batch Stream (De)Serialization
packages/client/src/links/httpBatchStreamLink.ts, packages/server/src/unstable-core-do-not-import/http/resolveResponse.ts, packages/client/src/links/httpSubscriptionLink.ts, packages/react-query/src/server/ssgProxy.ts
Wraps transformer (de)serialize functions in arrow functions before passing to stream utilities or serializers.
Transformer Interface Syntax
packages/server/src/unstable-core-do-not-import/transformer.ts
Updates interface method signatures from arrow function types to standard method declarations.
Plain Object Utility Refactor
packages/server/src/unstable-core-do-not-import/stream/jsonl.ts, packages/server/src/vendor/is-plain-object.ts
Replaces local isPlainObject with a new shared utility module.
Custom Class Serialization Tests
packages/tests/server/transformer.test.ts
Adds tests for custom class serialization/deserialization using various HTTP links, including regression coverage.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant httpBatchStreamLink
    participant Server
    participant jsonlStreamProducer/Consumer
    participant Transformer

    Client->>httpBatchStreamLink: Send request (custom class)
    httpBatchStreamLink->>Server: Forward request
    Server->>Transformer: serialize(customClassInstance)
    Transformer-->>Server: Serialized data
    Server->>jsonlStreamProducer: Pass data with (data) => serialize(data)
    jsonlStreamProducer-->>Server: Streamed JSONL response
    Server-->>Client: Streamed response
    Client->>jsonlStreamConsumer: Pass data with (data) => deserialize(data)
    jsonlStreamConsumer->>Transformer: deserialize(data)
    Transformer-->>jsonlStreamConsumer: Custom class instance
    jsonlStreamConsumer-->>Client: Custom class instance
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix custom class serialization/deserialization in httpBatchStreamLink (root value, not nested) [#6863]
Use a smarter isPlainObject check to avoid false positives with custom classes [#6863]
Add regression tests for custom class handling with httpBatchStreamLink [#6863]

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
None

Poem

A rabbit hopped through code today,
Ensuring custom classes work the streaming way.
With new tests and a shared plain check,
Serialization bugs are kept in check!
Now custom things hop back with glee—
In every batch, as plain as can be!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a1c8b52 and 4c4f893.

📒 Files selected for processing (1)
  • packages/react-query/src/server/ssgProxy.ts (1 hunks)
⏰ 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). (20)
  • GitHub Check: E2E-tests (next-prisma-todomvc)
  • GitHub Check: E2E-tests (soa)
  • GitHub Check: E2E-tests (next-prisma-websockets-starter)
  • GitHub Check: E2E-tests (next-formdata)
  • GitHub Check: E2E-tests (minimal-react)
  • GitHub Check: E2E-tests (.test/diagnostics-big-router)
  • GitHub Check: E2E-tests (express-minimal)
  • GitHub Check: E2E-tests (.experimental/next-app-dir)
  • GitHub Check: E2E-tests (.test/ssg)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
  • GitHub Check: Release using pkg.pr.new
  • GitHub Check: Test a monorepo using built declaration files
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 20.x)
  • GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
  • GitHub Check: E2E-tests (Deno) (deno-deploy)
  • GitHub Check: typecheck
  • GitHub Check: test
  • GitHub Check: Lint and auto-fix
🔇 Additional comments (1)
packages/react-query/src/server/ssgProxy.ts (1)

97-97: LGTM! Consistent function wrapping pattern.

This change standardizes the serialize function assignment to match the pattern used in the else branch (line 118), ensuring consistent invocation style across both code paths. The wrapper function approach aligns with the broader codebase changes mentioned in the AI summary and should help with the custom class serialization issues described in the PR objectives.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch issues/6863

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

Support

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

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.
@vercel
Copy link

vercel bot commented Aug 4, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-prisma-starter ✅ Ready (Inspect) Visit Preview Aug 4, 2025 4:26pm
og-image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 4, 2025 4:26pm
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 4, 2025 4:26pm
@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 4, 2025

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6878

@trpc/next

npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6878

@trpc/react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6878

@trpc/server

npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6878

@trpc/tanstack-react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/tanstack-react-query@6878

@trpc/upgrade

npm i https://pkg.pr.new/trpc/trpc/@trpc/upgrade@6878

commit: 4c4f893

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
packages/server/src/unstable-core-do-not-import/transformer.ts (1)

33-37: Same duplication comment as above applies to OutputDataTransformer

🧹 Nitpick comments (1)
packages/server/src/unstable-core-do-not-import/transformer.ts (1)

22-26: Redundant method redeclaration – let docs extend the base interface

InputDataTransformer already extends DataTransformer; repeating identical method signatures adds maintenance overhead and risks drift.

Consider:

-interface InputDataTransformer extends DataTransformer {
-  /** Runs **on the client** before sending the data to the server. */
-  serialize(object: any): any;
-  /** Runs **on the server** to transform the data before it is passed to the resolver */
-  deserialize(object: any): any;
-}
+/**
+ * Transformer directions from client ➜ server.
+ * Inherits the call signatures from `DataTransformer`.
+ */
+interface InputDataTransformer extends DataTransformer {}

You keep the descriptive comment while avoiding duplicate type definitions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b7b8d3a and 1886a04.

📒 Files selected for processing (6)
  • packages/client/src/links/httpBatchStreamLink.ts (1 hunks)
  • packages/server/src/unstable-core-do-not-import/http/resolveResponse.ts (1 hunks)
  • packages/server/src/unstable-core-do-not-import/stream/jsonl.ts (1 hunks)
  • packages/server/src/unstable-core-do-not-import/transformer.ts (1 hunks)
  • packages/server/src/vendor/is-plain-object.ts (1 hunks)
  • packages/tests/server/transformer.test.ts (2 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: the cli options `trpcfile` and `trpcimportname` in `packages/upgrade/src/bin/cli.ts` are temporary a...
Learnt from: barelyhuman
PR: trpc/trpc#6530
File: packages/upgrade/src/bin/cli.ts:218-222
Timestamp: 2025-02-19T20:31:52.088Z
Learning: The CLI options `trpcFile` and `trpcImportName` in `packages/upgrade/src/bin/cli.ts` are temporary and planned to be removed in favor of using automatically discovered references from the TypeScript program.

Applied to files:

  • packages/server/src/unstable-core-do-not-import/stream/jsonl.ts
📚 Learning: in the trpc monorepo, packages/tests and packages/upgrade don't need main/module/types fields in the...
Learnt from: juliusmarminge
PR: trpc/trpc#6789
File: packages/tanstack-react-query/tsdown.config.ts:1-24
Timestamp: 2025-06-09T14:01:20.033Z
Learning: In the tRPC monorepo, packages/tests and packages/upgrade don't need main/module/types fields in their package.json because tests is not distributed and upgrade just exposes a binary.

Applied to files:

  • packages/server/src/unstable-core-do-not-import/stream/jsonl.ts
  • packages/tests/server/transformer.test.ts
📚 Learning: in the minimal-react-typesafe-errors example, the database mock in db.ts should throw generic error ...
Learnt from: heitorlisboa
PR: trpc/trpc#6849
File: examples/minimal-react-typesafe-errors/server/db.ts:18-20
Timestamp: 2025-06-29T21:25:46.435Z
Learning: In the minimal-react-typesafe-errors example, the database mock in db.ts should throw generic Error objects rather than typed errors. The typed errors are handled at the tRPC procedure layer, which catches database errors and transforms them into API-specific typed errors. This maintains proper architectural separation between the database layer and API layer.

Applied to files:

  • packages/tests/server/transformer.test.ts
🧬 Code Graph Analysis (1)
packages/server/src/vendor/is-plain-object.ts (1)
packages/server/src/unstable-core-do-not-import/utils.ts (1)
  • isObject (31-33)
⏰ 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). (20)
  • GitHub Check: E2E-tests (vercel-edge-runtime)
  • GitHub Check: E2E-tests (soa)
  • GitHub Check: E2E-tests (standalone-server)
  • GitHub Check: E2E-tests (nuxt)
  • GitHub Check: E2E-tests (express-minimal)
  • GitHub Check: E2E-tests (next-formdata)
  • GitHub Check: E2E-tests (tanstack-start)
  • GitHub Check: E2E-tests (next-prisma-todomvc)
  • GitHub Check: E2E-tests (.test/diagnostics-big-router)
  • GitHub Check: E2E-tests (next-minimal-starter)
  • GitHub Check: E2E-tests (.test/internal-types-export)
  • GitHub Check: E2E-tests (cloudflare-workers)
  • GitHub Check: E2E-tests (express-server)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
  • GitHub Check: Test a monorepo using built declaration files
  • GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
  • GitHub Check: E2E-tests (Deno) (deno-deploy)
  • GitHub Check: test
🔇 Additional comments (9)
packages/server/src/vendor/is-plain-object.ts (2)

1-32: LGTM! Comprehensive plain object detection implementation.

The new isPlainObject utility provides robust detection logic that properly handles custom classes and edge cases. This addresses the core issue described in the PR objectives where custom classes were not properly identified.


8-10: No consolidation needed for isObject implementations.

The private isObject in packages/server/src/vendor/is-plain-object.ts is only used internally by isPlainObject to perform a strict “[object Object]” check (matching the upstream library’s behavior). The exported isObject in packages/server/src/unstable-core-do-not-import/utils.ts provides a broader generic object check (!!value && !Array.isArray(value) && typeof value === 'object') used throughout the codebase. They serve different purposes, have distinct semantics, and the vendored implementation isn’t even exported or imported elsewhere—merging them would change behavior.

Likely an incorrect or invalid review comment.

packages/server/src/unstable-core-do-not-import/stream/jsonl.ts (2)

1-1: Good consolidation of plain object detection logic.

Replacing the local isPlainObject implementation with the shared utility from the vendor module improves consistency and leverages the more robust detection logic that properly handles custom classes.


237-239: Critical fix for custom class handling.

This usage of the new isPlainObject function is where the fix for issue #6863 takes effect. The improved detection logic will correctly identify custom class instances as non-plain objects, preventing them from being incorrectly processed during encoding.

packages/client/src/links/httpBatchStreamLink.ts (1)

94-95: Proper transformer method invocation.

Wrapping the deserialize method in an arrow function ensures proper invocation regardless of the transformer implementation. This standardizes the function calling pattern and prevents potential issues with method context or reference handling.

packages/server/src/unstable-core-do-not-import/http/resolveResponse.ts (1)

564-564: Consistent transformer method invocation pattern.

This change mirrors the client-side pattern in httpBatchStreamLink.ts, ensuring consistent handling of transformer methods across the codebase. The arrow function wrapper guarantees proper method invocation regardless of transformer implementation details.

packages/tests/server/transformer.test.ts (3)

7-7: Good addition of missing import.

Adding the httpBatchStreamLink import enables comprehensive testing of the custom transformer functionality across all HTTP link types.


550-586: Comprehensive test setup for custom class serialization.

The test suite properly sets up a custom class with superjson registration, creating a realistic scenario that matches the issue described in #6863. The custom transformer configuration correctly handles both serialization and deserialization of custom class instances.


588-630: Excellent regression test coverage.

These tests verify that custom class instances are properly handled across all HTTP link types, with specific focus on the httpBatchStreamLink regression mentioned in issue #6863. The tests confirm that custom objects maintain their class identity after serialization/deserialization round trips.

trpc-bot
trpc-bot previously approved these changes Aug 4, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Aug 5, 2025

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

3 participants