From d41493dfd64740eb33a0888ab2528003842775e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=8Bnh=20L=C3=AA?= Date: Mon, 23 Nov 2026 16:30:43 +0700 Subject: [PATCH] fix(trpc): carry every chained tRPC input schema into the oRPC procedure --- apps/content/docs/integrations/trpc.mdx | 2 + packages/trpc/src/to-orpc-router.test.ts | 73 +++++++++++++++++++++++- packages/trpc/src/to-orpc-router.ts | 6 +- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/apps/content/docs/integrations/trpc.mdx b/apps/content/docs/integrations/trpc.mdx index 90454a39c..35c2bce0f 100644 --- a/apps/content/docs/integrations/trpc.mdx +++ b/apps/content/docs/integrations/trpc.mdx @@ -23,6 +23,8 @@ const orpcRouter = toORPCRouter(trpcRouter) The result is a regular oRPC router that works with any oRPC feature. For example, you can expose it through an [RPC Handler](/docs/rpc/handler) or [OpenAPI Handler](/docs/openapi/handler), or call it directly with [Server-Side Clients](/docs/client/server-side). +Input and output schemas that implement [Standard Schema](https://standardschema.dev) are carried over, including every chained `.input()`. Validation still runs inside tRPC. + ### Error Formatting `toORPCRouter` does not support [tRPC Error Formatting](https://trpc.io/docs/server/error-formatting). Instead, errors thrown by tRPC are wrapped in `ORPCError`. diff --git a/packages/trpc/src/to-orpc-router.test.ts b/packages/trpc/src/to-orpc-router.test.ts index e14fe2c64..2e1734e93 100644 --- a/packages/trpc/src/to-orpc-router.test.ts +++ b/packages/trpc/src/to-orpc-router.test.ts @@ -1,5 +1,5 @@ import type { OpenAPIMeta } from '@orpc/openapi' -import { getOpenAPIMeta, openapi } from '@orpc/openapi' +import { getOpenAPIMeta, openapi, OpenAPIGenerator } from '@orpc/openapi' import { call, createRouterClient, @@ -124,6 +124,77 @@ describe('toORPCRouter', () => { await expect(call((orpcRouter as any).ping, 'invalid')).rejects.toThrow('Invalid input') // validation happen at tRPC level }) + describe('chained inputs', () => { + const first = z.object({ x: z.string() }) + const second = z.object({ y: z.number() }) + const third = z.object({ z: z.boolean().optional() }) + + const orpcRouter = toORPCRouter( + t.router({ + two: t.procedure + .input(first) + .input(second) + .query(({ input }) => input), + three: t.procedure + .input(first) + .input(second) + .input(third) + .mutation(({ input }) => input), + }), + ) + + it('keeps every input schema in the contract', () => { + expect(orpcRouter.two['~orpc'].inputSchemas).toEqual([first, second]) + expect(orpcRouter.three['~orpc'].inputSchemas).toEqual([first, second, third]) + }) + + it('skips non-standard parsers', () => { + const mixed = toORPCRouter( + t.router({ + ping: (t.procedure.input(first) as any) + .input((value: unknown) => value) + .query(() => 'pong'), + }), + ) as any + + expect(mixed.ping['~orpc'].inputSchemas).toEqual([first]) + }) + + it('generates an OpenAPI request body with every input', async () => { + const spec = await new OpenAPIGenerator().generate(orpcRouter) + + const properties = (path: string) => { + const schema = (spec.paths as any)[path].post.requestBody.content['application/json'].schema + + return schema.allOf.flatMap((item: any) => Object.keys(item.properties)) + } + + expect(properties('/two')).toEqual(['x', 'y']) + expect(properties('/three')).toEqual(['x', 'y', 'z']) + }) + + it('calls', async () => { + const options = { context: { a: 'test' } } + + await expect(call(orpcRouter.two, { x: 'x', y: 1 }, options)).resolves.toEqual({ + x: 'x', + y: 1, + }) + await expect(call(orpcRouter.three, { x: 'x', y: 1, z: true }, options)).resolves.toEqual({ + x: 'x', + y: 1, + z: true, + }) + + await expect(call(orpcRouter.two, { y: 1 } as any, options)).rejects.toSatisfy( + (err: any) => err instanceof ORPCError && err.code === 'BAD_REQUEST', + ) + await expect(call(orpcRouter.three, { x: 'x' } as any, options)).rejects.toSatisfy( + (err: any) => err instanceof ORPCError && err.code === 'BAD_REQUEST', + ) + }) + }) + it('meta', async () => { const orpcRouter = toORPCRouter( t.router({ diff --git a/packages/trpc/src/to-orpc-router.ts b/packages/trpc/src/to-orpc-router.ts index ea78c627a..1eaadeb72 100644 --- a/packages/trpc/src/to-orpc-router.ts +++ b/packages/trpc/src/to-orpc-router.ts @@ -75,7 +75,9 @@ function recordToORPCRouterRecord(records: AnyRouter['_def']['record']) { } function toORPCProcedure(procedure: AnyProcedure) { - const inputSchema = toStandardSchema(procedure._def.inputs.at(-1)) + const inputSchemas = procedure._def.inputs + .map(toStandardSchema) + .filter((schema) => schema !== undefined) const outputSchema = toStandardSchema((procedure._def as any).output) const meta = (procedure._def.meta ?? {}) as ORPC.Meta @@ -86,7 +88,7 @@ function toORPCProcedure(procedure: AnyProcedure) { // so the tRPC meta must be a plugin too, otherwise it would be dropped. metaPlugins: [toTRPCMetaPlugin(meta)], orderedMiddlewares: [], - inputSchemas: inputSchema ? [inputSchema] : undefined, + inputSchemas: inputSchemas.length ? inputSchemas : undefined, outputSchemas: outputSchema ? [outputSchema] : undefined, // tRPC procedure calling already validates the input/output disableInputValidation: true,