Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client, server): prevent toJSON problem #279

Merged
merged 1 commit into from
Mar 21, 2025

Conversation

unnoq
Copy link
Owner

@unnoq unnoq commented Mar 21, 2025

Skip custom toJSON methods to avoid JSON.stringify invoking them,
which could cause meta and serialized data mismatches during deserialization.
Instead, rely on custom serializers.

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced JSON serialization to ensure consistent data handling by bypassing unintended custom conversion routines.
  • Tests

    • Expanded coverage with new cases that validate the improved behavior in processing objects with custom conversion logic.

Copy link

vercel bot commented Mar 21, 2025

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

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 21, 2025 7:27am

Copy link

coderabbitai bot commented Mar 21, 2025

Walkthrough

This update enhances the serialization logic in both the RPC and OpenAPI adapters. New test cases cover scenarios where objects have a toJSON method or an invalid toJSON value. In addition, the serialization methods now include a conditional check to skip properties named toJSON when they are functions. The assertion mechanism in the RPC tests has also been modified to stringify and then parse the serialized output before deserialization.

Changes

Files Change Summary
packages/client/.../rpc-json-serializer.test.ts
packages/openapi-client/.../openapi-json-serializer.test.ts
Added test cases verifying serializer behavior with objects having a toJSON property (function vs non-function) and adjusted assertion logic (stringify & parse) in the RPC tests.
packages/client/.../rpc-json-serializer.ts
packages/openapi-client/.../openapi-json-serializer.ts
Updated serialize methods to include a conditional check that skips processing properties named toJSON when their value is a function, with explanatory comments added.

Sequence Diagram(s)

Loading
sequenceDiagram
    participant Client
    participant Serializer
    Client->>Serializer: Call serialize(inputObject)
    Serializer->>Serializer: Iterate over input keys
    alt Key is "toJSON" and is a function
        Serializer->>Serializer: Skip property
    else
        Serializer->>Serializer: Process property normally
    end
    Serializer->>Serializer: Convert result to JSON string and parse (RPC flow)
    Serializer->>Client: Return serialized output

Possibly related PRs

Poem

Oh, how I hop with glee,
Skipping functions as easy as can be,
No more rogue toJSON in my lair,
Serialization's now fair and square,
CodeRabbit cheers, with a twitch and a hop,
New tests and checks—my heart will never stop!
🐰✨


📜 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 c0dd7cd and 3f042a4.

📒 Files selected for processing (4)
  • packages/client/src/adapters/standard/rpc-json-serializer.test.ts (2 hunks)
  • packages/client/src/adapters/standard/rpc-json-serializer.ts (1 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-json-serializer.test.ts (1 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-json-serializer.ts (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/client/src/adapters/standard/rpc-json-serializer.ts (2)
packages/client/src/adapters/standard/rpc-serializer.ts (2) (2)
  • data (28-52)
  • data (80-93)
packages/openapi-client/src/adapters/standard/openapi-serializer.ts (1) (1)
  • data (30-53)
🔇 Additional comments (5)
packages/client/src/adapters/standard/rpc-json-serializer.ts (1)

115-122: Good fix for the toJSON serialization issue.

This change correctly prevents JSON.stringify from invoking custom toJSON methods during serialization, which would cause mismatches between meta and serialized data. The implementation is clean and properly documented with clear comments explaining the reasoning.

This approach ensures that custom serializers remain the appropriate way to handle special object serialization rather than relying on potentially problematic toJSON methods.

packages/openapi-client/src/adapters/standard/openapi-json-serializer.test.ts (1)

148-155: Appropriate test cases for toJSON handling.

These test cases properly verify the new behavior for handling objects with toJSON properties:

  1. When toJSON is a function, it should be skipped during serialization
  2. When toJSON is a non-function value, it should be preserved in the serialized output

These tests provide good coverage for the changes made to the serializer implementation.

packages/openapi-client/src/adapters/standard/openapi-json-serializer.ts (1)

52-59: Good implementation of toJSON handling.

The implementation correctly skips toJSON methods during serialization, consistent with the changes in the RPC serializer. This ensures that custom serializers remain the proper way to handle specialized object serialization across all serializers in the codebase.

packages/client/src/adapters/standard/rpc-json-serializer.test.ts (2)

43-52: Good test coverage for toJSON handling scenarios.

These test cases appropriately verify the behavior when:

  1. Objects have a toJSON method (it should be skipped)
  2. Objects have a toJSON property that's not a function (it should be preserved)

This provides good validation of the serializer changes.


79-86: Improved test accuracy with JSON stringification.

This change properly tests the real-world scenario where serialized data goes through a JSON stringify/parse cycle before being deserialized. By adding this intermediate step, the test better represents how the code would behave in production environments where data is transmitted over the network.

This approach helps ensure that any issues with string serialization would be caught by the tests.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

Sorry, something went wrong.

Copy link

codecov bot commented Mar 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

Copy link

pkg-pr-new bot commented Mar 21, 2025

Open in Stackblitz

More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@279

@orpc/client

npm i https://pkg.pr.new/@orpc/client@279

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@279

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@279

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@279

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@279

@orpc/server

npm i https://pkg.pr.new/@orpc/server@279

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@279

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@279

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@279

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@279

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@279

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@279

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@279

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@279

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@279

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@279

commit: 3f042a4

@unnoq unnoq merged commit 1ea8333 into main Mar 21, 2025
8 checks passed
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.

None yet

1 participant