Skip to content

Commit

Permalink
fix: Zod generated types for all parameters as optional is missing .o…
Browse files Browse the repository at this point in the history
…ptional() method call on root object
  • Loading branch information
stijnvanhulle committed Jan 10, 2024
1 parent 2c20339 commit f343fbb
Show file tree
Hide file tree
Showing 60 changed files with 309 additions and 156 deletions.
7 changes: 7 additions & 0 deletions .changeset/tough-hats-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@kubb/swagger-zodios": patch
"@kubb/swagger-zod": patch
"@kubb/swagger": patch
---

Zod generated types for all parameters as optional is missing .optional() method call on root object
1 change: 1 addition & 0 deletions e2e/kubb.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from '@kubb/core'

const schemas = [
['petStoreV3', 'https://petstore3.swagger.io/api/v3/openapi.json'],
['optionalParameters', './schemas/optionalParameters.json'],
['allOf', './schemas/allOf.json'],
['anyOf', './schemas/anyOf.json'],
['petStoreContent', './schemas/petStoreContent.json'],
Expand Down
39 changes: 39 additions & 0 deletions e2e/schemas/optionalParameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"openapi": "3.0.3",
"info": {
"title": "test",
"description": "",
"license": {
"name": ""
},
"version": "0.1.0"
},
"paths": {
"/test": {
"get": {
"tags": ["test"],
"operationId": "SomeName",
"summary": "",
"description": "Some description",
"parameters": [
{
"name": "foo",
"required": false,
"in": "query",
"schema": { "minimum": 1, "format": "int32", "default": 1, "type": "integer" }
},
{
"name": "baz",
"required": false,
"in": "query",
"schema": { "minimum": 1, "format": "int32", "default": 10, "type": "integer" }
}
]
}
}
},
"components": {
"schemas": {
}
}
}
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/addressSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const addressSchema = z.object({
'city': z.string().optional(),
'state': z.string().optional(),
'zip': z.string().optional(),
})
}).optional()
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/apiResponseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const apiResponseSchema = z.object({ 'code': z.number().optional(), 'type': z.string().optional(), 'message': z.string().optional() })
export const apiResponseSchema = z.object({ 'code': z.number().optional(), 'type': z.string().optional(), 'message': z.string().optional() }).optional()
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/categorySchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const categorySchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
export const categorySchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() }).optional()
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/customerSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const customerSchema = z.object({
'id': z.number().optional(),
'username': z.string().optional(),
'address': z.array(z.lazy(() => addressSchema)).optional(),
})
}).optional()
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/orderSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const orderSchema = z.object({
'status': z.enum([`placed`, `approved`, `delivered`]).describe(`Order Status`).optional(),
'http_status': z.enum([`ok`, `not_found`]).describe(`HTTP Status`).optional(),
'complete': z.boolean().optional(),
})
}).optional()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod'
import { addPetRequestSchema } from '../addPetRequestSchema'
import { petSchema } from '../petSchema'

export const addPet405Schema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() })
export const addPet405Schema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() }).optional()

/**
* @description Create a new pet in the store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { z } from 'zod'
* @description Invalid pet value
*/
export const deletePet400Schema = z.any()
export const deletePetHeaderParamsSchema = z.object({ 'api_key': z.string().optional() })
export const deletePetHeaderParamsSchema = z.object({ 'api_key': z.string().optional() }).optional()
export const deletePetMutationResponseSchema = z.any()
export const deletePetPathParamsSchema = z.object({ 'petId': z.number().describe(`Pet id to delete`) })
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { petSchema } from '../petSchema'
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
'status': z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
})
}).optional()

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const findPetsByTagsQueryParamsSchema = z.object({
'tags': z.array(z.string()).describe(`Tags to filter by`).optional(),
'page': z.string().describe(`to request with required page number or pagination`).optional(),
'pageSize': z.string().describe(`to request with required page size`).optional(),
})
}).optional()

/**
* @description successful operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const updatePetWithFormPathParamsSchema = z.object({ 'petId': z.number().
export const updatePetWithFormQueryParamsSchema = z.object({
'name': z.string().describe(`Name of pet that needs to be updated`).optional(),
'status': z.string().describe(`Status of pet that needs to be updated`).optional(),
})
}).optional()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apiResponseSchema } from '../apiResponseSchema'

export const uploadFileMutationRequestSchema = z.string()
export const uploadFilePathParamsSchema = z.object({ 'petId': z.number().describe(`ID of pet to update`) })
export const uploadFileQueryParamsSchema = z.object({ 'additionalMetadata': z.string().describe(`Additional Metadata`).optional() })
export const uploadFileQueryParamsSchema = z.object({ 'additionalMetadata': z.string().describe(`Additional Metadata`).optional() }).optional()

/**
* @description successful operation
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/petNotFoundSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const petNotFoundSchema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() })
export const petNotFoundSchema = z.object({ 'code': z.number().optional(), 'message': z.string().optional() }).optional()
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const createPetsHeaderParamsSchema = z.object({ 'X-EXAMPLE': z.enum([`ONE
export const createPetsMutationRequestSchema = z.object({ 'name': z.string(), 'tag': z.string() })
export const createPetsMutationResponseSchema = z.any()
export const createPetsPathParamsSchema = z.object({ 'uuid': z.string().describe(`UUID`) })
export const createPetsQueryParamsSchema = z.object({ 'offset': z.number().describe(`Offset`).optional() })
export const createPetsQueryParamsSchema = z.object({ 'offset': z.number().describe(`Offset`).optional() }).optional()

/**
* @description unexpected error
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/tagSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const tagSchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() })
export const tagSchema = z.object({ 'id': z.number().optional(), 'name': z.string().optional() }).optional()
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const loginUser400Schema = z.any()
export const loginUserQueryParamsSchema = z.object({
'username': z.string().describe(`The user name for login`).optional(),
'password': z.string().describe(`The password for login in clear text`).optional(),
})
}).optional()

/**
* @description successful operation
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/src/gen/zod/userSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const userSchema = z.object({
'password': z.string().optional(),
'phone': z.string().optional(),
'userStatus': z.number().describe(`User Status`).optional(),
})
}).optional()
22 changes: 11 additions & 11 deletions examples/advanced/src/gen/zodios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const endpoints = makeApi([
name: 'offset',
description: `Offset`,
type: 'Query',
schema: createPetsQueryParamsSchema.shape['offset'],
schema: createPetsQueryParamsSchema.unwrap().shape['offset'],
},
{
name: 'X-EXAMPLE',
Expand Down Expand Up @@ -162,7 +162,7 @@ export const endpoints = makeApi([
name: 'status',
description: `Status values that need to be considered for filter`,
type: 'Query',
schema: findPetsByStatusQueryParamsSchema.shape['status'],
schema: findPetsByStatusQueryParamsSchema.unwrap().shape['status'],
},
],
response: findPetsByStatusQueryResponseSchema,
Expand All @@ -184,19 +184,19 @@ export const endpoints = makeApi([
name: 'tags',
description: `Tags to filter by`,
type: 'Query',
schema: findPetsByTagsQueryParamsSchema.shape['tags'],
schema: findPetsByTagsQueryParamsSchema.unwrap().shape['tags'],
},
{
name: 'page',
description: `to request with required page number or pagination`,
type: 'Query',
schema: findPetsByTagsQueryParamsSchema.shape['page'],
schema: findPetsByTagsQueryParamsSchema.unwrap().shape['page'],
},
{
name: 'pageSize',
description: `to request with required page size`,
type: 'Query',
schema: findPetsByTagsQueryParamsSchema.shape['pageSize'],
schema: findPetsByTagsQueryParamsSchema.unwrap().shape['pageSize'],
},
{
name: 'X-EXAMPLE',
Expand Down Expand Up @@ -257,13 +257,13 @@ export const endpoints = makeApi([
name: 'name',
description: `Name of pet that needs to be updated`,
type: 'Query',
schema: updatePetWithFormQueryParamsSchema.shape['name'],
schema: updatePetWithFormQueryParamsSchema.unwrap().shape['name'],
},
{
name: 'status',
description: `Status of pet that needs to be updated`,
type: 'Query',
schema: updatePetWithFormQueryParamsSchema.shape['status'],
schema: updatePetWithFormQueryParamsSchema.unwrap().shape['status'],
},
],
response: updatePetWithFormMutationResponseSchema,
Expand Down Expand Up @@ -291,7 +291,7 @@ export const endpoints = makeApi([
name: 'api_key',
description: ``,
type: 'Header',
schema: deletePetHeaderParamsSchema.shape['api_key'],
schema: deletePetHeaderParamsSchema.unwrap().shape['api_key'],
},
],
response: deletePetMutationResponseSchema,
Expand Down Expand Up @@ -319,7 +319,7 @@ export const endpoints = makeApi([
name: 'additionalMetadata',
description: `Additional Metadata`,
type: 'Query',
schema: uploadFileQueryParamsSchema.shape['additionalMetadata'],
schema: uploadFileQueryParamsSchema.unwrap().shape['additionalMetadata'],
},
{
name: 'UploadFileMutationRequest',
Expand Down Expand Up @@ -373,13 +373,13 @@ export const endpoints = makeApi([
name: 'username',
description: `The user name for login`,
type: 'Query',
schema: loginUserQueryParamsSchema.shape['username'],
schema: loginUserQueryParamsSchema.unwrap().shape['username'],
},
{
name: 'password',
description: `The password for login in clear text`,
type: 'Query',
schema: loginUserQueryParamsSchema.shape['password'],
schema: loginUserQueryParamsSchema.unwrap().shape['password'],
},
],
response: loginUserQueryResponseSchema,
Expand Down
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/addPetSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod'
import { addPetRequestSchema } from './addPetRequestSchema'
import { petSchema } from './petSchema'

export const addPet405Schema = z.object({ code: z.number().optional(), message: z.string().optional() })
export const addPet405Schema = z.object({ code: z.number().optional(), message: z.string().optional() }).optional()

/**
* @description Create a new pet in the store
Expand Down
4 changes: 3 additions & 1 deletion examples/zod/src/gen/zod/addressSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { z } from 'zod'

export const addressSchema = z.object({ street: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional() })
export const addressSchema = z
.object({ street: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional() })
.optional()
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/apiResponseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const apiResponseSchema = z.object({ code: z.number().optional(), type: z.string().optional(), message: z.string().optional() })
export const apiResponseSchema = z.object({ code: z.number().optional(), type: z.string().optional(), message: z.string().optional() }).optional()
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/categorySchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const categorySchema = z.object({ id: z.number().optional(), name: z.string().optional() })
export const categorySchema = z.object({ id: z.number().optional(), name: z.string().optional() }).optional()
4 changes: 3 additions & 1 deletion examples/zod/src/gen/zod/customerSchema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { addressSchema } from './addressSchema'
import { z } from 'zod'

export const customerSchema = z.object({ id: z.number().optional(), username: z.string().optional(), address: z.array(z.lazy(() => addressSchema)).optional() })
export const customerSchema = z
.object({ id: z.number().optional(), username: z.string().optional(), address: z.array(z.lazy(() => addressSchema)).optional() })
.optional()
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/deletePetSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { z } from 'zod'
* @description Invalid pet value
*/
export const deletePet400Schema = z.any()
export const deletePetHeaderParamsSchema = z.object({ api_key: z.string().optional() })
export const deletePetHeaderParamsSchema = z.object({ api_key: z.string().optional() }).optional()
export const deletePetMutationResponseSchema = z.any()
export const deletePetPathParamsSchema = z.object({ petId: z.number().describe(`Pet id to delete`) })
6 changes: 3 additions & 3 deletions examples/zod/src/gen/zod/findPetsByStatusSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { petSchema } from './petSchema'
* @description Invalid status value
*/
export const findPetsByStatus400Schema = z.any()
export const findPetsByStatusQueryParamsSchema = z.object({
status: z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional(),
})
export const findPetsByStatusQueryParamsSchema = z
.object({ status: z.enum([`available`, `pending`, `sold`]).default('available').describe(`Status values that need to be considered for filter`).optional() })
.optional()

/**
* @description successful operation
Expand Down
12 changes: 7 additions & 5 deletions examples/zod/src/gen/zod/findPetsByTagsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { petSchema } from './petSchema'
* @description Invalid tag value
*/
export const findPetsByTags400Schema = z.any()
export const findPetsByTagsQueryParamsSchema = z.object({
tags: z.array(z.string()).describe(`Tags to filter by`).optional(),
page: z.string().describe(`to request with required page number or pagination`).optional(),
pageSize: z.string().describe(`to request with required page size`).optional(),
})
export const findPetsByTagsQueryParamsSchema = z
.object({
tags: z.array(z.string()).describe(`Tags to filter by`).optional(),
page: z.string().describe(`to request with required page number or pagination`).optional(),
pageSize: z.string().describe(`to request with required page size`).optional(),
})
.optional()

/**
* @description successful operation
Expand Down
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/getInventorySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import { z } from 'zod'
/**
* @description successful operation
*/
export const getInventoryQueryResponseSchema = z.object({}).catchall(z.number().min(-2147483648).max(2147483647))
export const getInventoryQueryResponseSchema = z.object({}).catchall(z.number().min(-2147483648).max(2147483647)).optional()
10 changes: 6 additions & 4 deletions examples/zod/src/gen/zod/loginUserSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { z } from 'zod'
* @description Invalid username/password supplied
*/
export const loginUser400Schema = z.any()
export const loginUserQueryParamsSchema = z.object({
username: z.string().describe(`The user name for login`).optional(),
password: z.string().describe(`The password for login in clear text`).optional(),
})
export const loginUserQueryParamsSchema = z
.object({
username: z.string().describe(`The user name for login`).optional(),
password: z.string().describe(`The password for login in clear text`).optional(),
})
.optional()

/**
* @description successful operation
Expand Down
26 changes: 14 additions & 12 deletions examples/zod/src/gen/zod/orderSchema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { z } from 'zod'

export const orderSchema = z.object({
id: z.number().optional(),
petId: z.number().optional(),
quantity: z.number().optional(),
shipDate: z.string().datetime().optional(),
status: z.enum([`placed`, `approved`, `delivered`]).describe(`Order Status`).optional(),
http_status: z
.union([z.literal(200), z.literal(400), z.literal(500)])
.describe(`HTTP Status`)
.optional(),
complete: z.boolean().optional(),
})
export const orderSchema = z
.object({
id: z.number().optional(),
petId: z.number().optional(),
quantity: z.number().optional(),
shipDate: z.string().datetime().optional(),
status: z.enum([`placed`, `approved`, `delivered`]).describe(`Order Status`).optional(),
http_status: z
.union([z.literal(200), z.literal(400), z.literal(500)])
.describe(`HTTP Status`)
.optional(),
complete: z.boolean().optional(),
})
.optional()
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/petNotFoundSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const petNotFoundSchema = z.object({ code: z.number().optional(), message: z.string().optional() })
export const petNotFoundSchema = z.object({ code: z.number().optional(), message: z.string().optional() }).optional()
2 changes: 1 addition & 1 deletion examples/zod/src/gen/zod/tagSchema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { z } from 'zod'

export const tagSchema = z.object({ id: z.number().optional(), name: z.string().optional() })
export const tagSchema = z.object({ id: z.number().optional(), name: z.string().optional() }).optional()
Loading

0 comments on commit f343fbb

Please sign in to comment.