kubb-labs / kubb

The ultimate toolkit for working with APIs.
https://kubb.dev
MIT License
742 stars 67 forks source link

Zod creates default for nullable property #1409

Closed Thijmen closed 1 week ago

Thijmen commented 1 week ago

What version of kubb is running?

3.0.6

What platform is your computer?

MacOS

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

No response

What steps can reproduce the bug?

Given following model:

"UpdateStackCommandCommand": {
        "required": [
          "stackId"
        ],
        "type": "object",
        "properties": {
          "stackId": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string",
            "default": null,
            "nullable": true
          },
          "description": {
            "type": "string",
            "default": null,
            "nullable": true
          },
          "gitSettings": {
            "$ref": "#/components/schemas/GitSettings"
          }
        }
      },

Produces following zod schema:


 export const updateStackCommandCommandSchema = z.object({ "stackId": z.string().uuid(), "name": z.string().default().nullable().nullish(), "description": z.string().default().nullable().nullish(), "gitSettings": z.lazy(() => gitSettingsSchema).optional() });

Name and description now get the default() method too. I believe this is not intended, as it's nullable where the default is null; "description": z.string().default().nullable().nullish(),

This leads to the following typescript error:

Expected 1 arguments, but got 0.ts(2554)
types.d.ts(85, 13): An argument for 'def' was not provided.

How often does this bug happen?

Every time

What is the expected behavior?

default() is not present, because the default is null and the property is nullable.

Swagger/OpenAPI file?

No response

Additional information

No response

linear[bot] commented 1 week ago

KUBB-72 Zod creates default for nullable property

Thijmen commented 1 week ago

I am more than happy to open a PR for this, let me know if this is appreciated.

stijnvanhulle commented 1 week ago

@Thijmen Feel free to open a pr, this is more than appreciated. Probably another check will be needed here: https://github.com/kubb-labs/kubb/blob/main/packages/plugin-oas/src/SchemaGenerator.ts#L399 to not add default to our custom AST.

How it works:

Thijmen commented 1 week ago

@stijnvanhulle I'll do my best! How can I run the tests locally at best?

Thijmen commented 1 week ago

I've drafted up #1415, with the addition of the "Toy" schema. Hope you like this! 😄 I had fun while making this minor change! 🚀

stijnvanhulle commented 1 week ago

@Thijmen A new version of Kubb has been release with your change: 3.0.9

Thijmen commented 1 week ago

@stijnvanhulle Awesome, thanks for the quick release!