trpc-openapi icon indicating copy to clipboard operation
trpc-openapi copied to clipboard

zod array objects break this

Open davidawad opened this issue 2 years ago • 7 comments

heres' an example:

image

if I have this code defining a schema and try to make TRPC calls (on ANY API, not just this one), I'll get this error:

error - TRPCError: [query.question.read] - Input parser key: "labels" must be ZodString, ZodNumber, ZodBoolean, ZodBigInt or ZodDate
    at /Users/david/Projects/WORK/DFDF/DDQ360/node_modules/trpc-openapi/dist/generator/schema.js:72:27
    at Array.map (<anonymous>)
    at getParameterObjects (/Users/david/Projects/WORK/DFDF/DDQ360/node_modules/trpc-openapi/dist/generator/schema.js:65:10)
    at /Users/david/Projects/WORK/DFDF/DDQ360/node_modules/trpc-openapi/dist/generator/paths.js:61:66
    at forEachOpenApiProcedure (/Users/david/Projects/WORK/DFDF/DDQ360/node_modules/trpc-openapi/dist/utils/procedure.js:34:13)
    at getOpenApiPathsObject (/Users/david/Projects/WORK/DFDF/DDQ360/node_modules/trpc-openapi/dist/generator/paths.js:13:45)
    at generateOpenApiDocument (/Users/david/Projects/WORK/DFDF/DDQ360/node_modules/trpc-openapi/dist/generator/index.js:27:50)
    at eval (webpack-internal:///(api)/./src/server/api/root.ts:51:98)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'INTERNAL_SERVER_ERROR',
  [cause]: undefined
}

davidawad avatar Sep 12 '23 01:09 davidawad

I believe this happens because there is no "default" way to pass an array of values as a query parameter. It might work for tRPC, but there's no easy widely adopted method of passing arrays as a query parameter and most implementations are framework specific. Which one of the following endpoints should even be generated?

?cars[]=Saab&cars[]=Audi
?cars=Saab&cars=Audi
?cars=Saab,Audi

I suggest using a POST request instead and passing everything in the body. Alternatively, you can make "labels" into a string, and parse it manually (using the format you chose) inside the procedure. But that's a bit weird IMO.

Pridestalkerr avatar Sep 13 '23 14:09 Pridestalkerr

I've also been unable to use arrays in inputs for PUT. See #255

ntindle avatar Sep 20 '23 04:09 ntindle

+1

popuguytheparrot avatar Oct 05 '23 09:10 popuguytheparrot

image

image I'm building a pagination router with the above schema but also facing the same ordeal. Hope this get fixed soon :(

tdanh2k avatar Mar 22 '24 17:03 tdanh2k

image

image I'm building a pagination router with the above schema but also facing the same ordeal. Hope this get fixed soon :(

image

Or maybe just comment the .meta() method and move on.

tdanh2k avatar Mar 22 '24 17:03 tdanh2k