orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.82k stars 313 forks source link

Zod: generation of typed tuples produces illegal zod array syntax #1592

Closed helt closed 2 weeks ago

helt commented 3 weeks ago

What are the steps to reproduce this issue?

  1. check out referenced PR
  2. in tests: generate and build

What happens?

zod generator produces zod.array(), which is illegal syntax since error TS2554: Expected 1-2 arguments, but got 0.

What were you expecting to happen?

Produce a typed tuple, similar to:

zod.tuple(zod.string(), zod.object())

Any logs, error output, etc?

generated/zod/example-v3-1.ts:25:24 - error TS2554: Expected 1-2 arguments, but got 0.

25   "example_tuple": zod.array().min(postApiSampleResponseExampleTupleMin).max(postApiSampleResponseExampleTupleMax).optional(),
                          ~~~~~~~

  node_modules/zod/lib/types.d.ts:1055:49
    1055 declare const arrayType: <T extends ZodTypeAny>(schema: T, params?: RawCreateParams) => ZodArray<T, "many">;
                                                         ~~~~~~~~~
    An argument for 'schema' was not provided.

Any other comments?

more issues pop up which are not my main concern, currently.

What versions are you using?

npx envinfo --system --npmPackages orval,zod,axios,msw,swr,@tanstack/react-query,@tanstack/vue-query,react,vue
Need to install the following packages:
envinfo@7.13.0
Ok to proceed? (y) y

  System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
    Memory: 13.61 GB / 31.31 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  npmPackages:
    @tanstack/react-query: ^4.22.0 => 4.22.0 
    @tanstack/vue-query: ^4.22.0 => 4.22.0 
    axios: ^0.26.1 => 0.26.1 
    msw: ^2.0.2 => 2.0.2 
    react: ^18.3.1 => 18.3.1 
    swr: ^2.2.4 => 2.2.4 
    vue: ^3.3.4 => 3.3.4 
    zod: ^3.23.8 => 3.23.8 
helt commented 3 weeks ago

reuses test cases of prefixItems added in https://github.com/orval-labs/orval/issues/890

melloware commented 3 weeks ago

awesome I assigned this to you. Thanks for looking into it!

helt commented 3 weeks ago

A similar issue was reported in kubb, but since kubb is deprecated, i was recommended to switch to orval. https://github.com/kubb-labs/kubb/issues/1018

melloware commented 3 weeks ago

Oh I didn't realize Kubb was depercated! Its one of the reasons @anymaniax moved this to an open source orval-labs from his personal project its too hard to maintain these kinds of projects by one person. So welcome aboard and we have other zod labeled tickets open if you want to improve anything else in zod support as well!

helt commented 3 weeks ago

https://github.com/kubb-labs/kubb/commit/35b7a0a7822716b9625c0c96a904ec0c7b9ebe4a

helt commented 3 weeks ago

Hey. I looked into the issue. So far, it is half-way working. An issue I have currently is that min and max are added to my tuple call. However, zod does not allow it to follow tuple() calls. Any ideas how to get rid of it?.

More questions:

Given a

const arrayWithPrefixItemsSchema: SchemaObject31 = {
  type: "array",
  prefixItems: [
    {type: "string"},
    {}
  ],
  items:
    {type: "number"},
}

According to openapi v3.1 spec, the goal of parsing prefixItems is to generate something like:

z.tuple([z.string(), z.object()]).rest(z.number())
melloware commented 2 weeks ago

Awesome job @helt if you don't mind reviewing there are a few more open Zod issues that would be a huge help if you wanted to tackle them; https://github.com/orval-labs/orval/issues?q=is%3Aissue+is%3Aopen+label%3Azod