Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zod creates default for nullable property #1409

Closed
Thijmen opened this issue Nov 16, 2024 · 7 comments · Fixed by #1415
Closed

Zod creates default for nullable property #1409

Thijmen opened this issue Nov 16, 2024 · 7 comments · Fixed by #1415
Labels
bug Something isn't working @kubb/plugin-zod

Comments

@Thijmen
Copy link
Contributor

Thijmen commented Nov 16, 2024

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

@Thijmen Thijmen added the bug Something isn't working label Nov 16, 2024
Copy link

linear bot commented Nov 16, 2024

@Thijmen
Copy link
Contributor Author

Thijmen commented Nov 16, 2024

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

@stijnvanhulle
Copy link
Collaborator

@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:

  • SchemaGenerator goes over the the openAPI/Swagger file(plugin-oas)
  • SchemaGeneratorcreates a custom AST(array of objects withtype` and some extra meta data.
  • TypeScript, Zod and faker uses this generated AST to parse the data(see parser/index.ts)
  • Types, faker functions and Zod schemas are being created and save to a file

@Thijmen
Copy link
Contributor Author

Thijmen commented Nov 18, 2024

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

@Thijmen
Copy link
Contributor Author

Thijmen commented Nov 18, 2024

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
Copy link
Collaborator

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

@Thijmen
Copy link
Contributor Author

Thijmen commented Nov 18, 2024

@stijnvanhulle Awesome, thanks for the quick release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working @kubb/plugin-zod
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants