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

(v2.2.0) Optional properties with type: array allow it's values to be undefined in zod shape #768

Closed
NimmLor opened this issue Jan 12, 2024 · 0 comments · Fixed by #771
Closed
Assignees
Labels
bug Something isn't working

Comments

@NimmLor
Copy link

NimmLor commented Jan 12, 2024

What version of kubb is running?

2.2.0

What platform is your computer?

Windows 11

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

No response

What steps can reproduce the bug?

Upon upgrading from v2.1.5 to v2.2.0 kubb generates properties that are of type array and optional to shapes that have their items set to optional too.

This adds undefined as possible value to the type of the array and also allows to pass undefined as the value.

  1. Add @kubb/swagger-zod as plugin
  2. Create any property to be an array and don't specify it to be required
  3. Set the items option to ref to a openapi component
  4. Run kubb generate

The example spec generates (manually stiched together for demonstration):

import { z } from "zod"

const createUserSchema = z
  .object({ email: z.string().email().optional() })
  .optional()

const createUsersMutationRequestSchema = z
  .object({ users: z.array(z.lazy(() => createUserSchema)).optional() })
  .optional()


// Usage with unexpected behaviour:

createUsersMutationRequestSchema.safeParse({
  users: [undefined],
}) // => { success: true, data: { users: [undefined] } }

type CreateUserResponse = z.infer<typeof createUsersMutationRequestSchema>
//   ^? type CreateUserResponse = { users?: ({ email?: string | undefined; } | undefined)[] | undefined;} | undefined

How often does this bug happen?

Every time

What is the expected behavior?

Items of optional arrays are not set to be optional as in v2.1.5

Swagger/OpenAPI file?

components:
  schemas:
    CreateUser:
      properties:
        email:
          type: string
    CreateUserResult:
      properties:
        success:
          type: boolean
      required:
        - success
      type: object
info:
  license:
    name: MIT
  title: Kubb Sample
  version: 1.0.0
openapi: '3.0.0'
paths:
  /pets:
    post:
      operationId: createUsers
      requestBody:
        content:
          application/json:
            schema:
              properties:
                users:
                  items:
                    $ref: '#/components/schemas/CreateUser'
                  type: array
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateUserResult'
          description: OK

Additional information

No response

@NimmLor NimmLor added the bug Something isn't working label Jan 12, 2024
@stijnvanhulle stijnvanhulle self-assigned this Jan 14, 2024
@stijnvanhulle stijnvanhulle linked a pull request Jan 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants