trpc: bug: Disabling batching breaks serialization of string values

Provide environment information

  System:
    OS: macOS 13.0.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 2.14 GB / 16.00 GB
    Shell: 3.5.1 - /usr/local/bin/fish
  Binaries:
    Node: 16.18.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.19.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 112.0.5615.137
    Firefox: 109.0
    Safari: 16.1
  npmPackages:
    @tanstack/react-query: ^4.18.0 => 4.29.3 
    @trpc/client: ^10.21.1 => 10.21.1 
    @trpc/next: ^10.21.1 => 10.21.1 
    @trpc/react-query: ^10.21.1 => 10.21.1 
    @trpc/server: ^10.21.1 => 10.21.1 
    next: ^13.2.1 => 13.3.1 
    react: ^18.2.0 => 18.2.0 
    typescript: ^4.8.3 => 4.9.5

Describe the bug

When batching is disabled, and the input is supposed to be string (defined with z.string()), it is unable properly parse the request body. The error:

{
    "error": {
        "message": "Unexpected number in JSON at position 1",
        "code": -32700,
        "data": {
            "code": "PARSE_ERROR",
            "httpStatus": 400,
            "stack": "TRPCError: Unexpected number in JSON at position 1\n    at getRawProcedureInputOrThrow (file:///PATH/trpc-next-minimal/node_modules/@trpc/server/dist/resolveHTTPResponse-6c8965e4.mjs:24:15)\n    at resolveHTTPResponse (file:///PATH/trpc-next-minimal/node_modules/@trpc/server/dist/resolveHTTPResponse-6c8965e4.mjs:90:26)\n    at file:///PATHs/trpc-next-minimal/node_modules/@trpc/server/dist/nodeHTTPRequestHandler-2718f9d8.mjs:61:30\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)\n    at async file:///PATH/trpc-next-minimal/node_modules/@trpc/server/dist/adapters/next.mjs:39:9"
        }
    }
}

Link to reproduction

https://stackblitz.com/edit/github-ferjpz?file=src/utils/trpc.ts

To reproduce

In the stackblitz sandbox, click “As string”, observe error. Then basically the same thing, but payload is passed as object with one field, and no problem.

Additional information

Seems like adding data transformers can fix the issue. But it’d still be great not to have the need to apply this solution

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I’d be down to file a PR fixing this bug!

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (19 by maintainers)

Commits related to this issue

Most upvoted comments

I’ll go to discord now to connect with you guys, and discuss the changes I’m attempting to make

@sudo97 I did a start in https://github.com/trpc/trpc/tree/issues/4243 (this file), feel free to fork that. I didn’t manage to repro it yet

Run test with pnpm vitest 4243

I’d love to contribute by fixing, but okay, let me try making a failing test as a start

That’s weird, thanks for the reproduction. It also seems to occur with the useMutation hook:

https://stackblitz.com/edit/github-ferjpz-tfao48?file=src/pages/index.tsx

I’m going to tag this as a bug since it does look like an issue with our handling of strings specifically.