Skip to content

Commit 7374708

Browse files
authored
Merge pull request #3759 from hey-api/fix/zod-default-order
fix: zod optional default order
2 parents cf1c82c + cda5297 commit 7374708

File tree

98 files changed

+399
-384
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+399
-384
lines changed

.changeset/odd-plums-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": patch
3+
---
4+
5+
**plugin(zod)**: fix: move `.default()` after `.optional()` to avoid `z.infer` producing required fields

dev/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,5 @@
3131
"valibot": "1.3.1",
3232
"vue": "3.5.25",
3333
"zod": "4.3.6"
34-
},
35-
"engines": {
36-
"node": ">=22.13.0"
3734
}
3835
}

packages/openapi-python-tests/pydantic/v2/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@
66
"devDependencies": {
77
"@hey-api/openapi-python": "workspace:*",
88
"typescript": "6.0.2"
9-
},
10-
"engines": {
11-
"node": ">=22.13.0"
129
}
1310
}

packages/openapi-python-tests/sdks/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@
66
"devDependencies": {
77
"@hey-api/openapi-python": "workspace:*",
88
"typescript": "6.0.2"
9-
},
10-
"engines": {
11-
"node": ">=22.13.0"
129
}
1310
}

packages/openapi-python/src/plugins/pydantic/v2/walker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export function createVisitor(
3737
applyModifiers(result, ctx, options = {}): PydanticFinal {
3838
const { optional } = options;
3939

40-
const hasDefault = result.meta.default !== undefined;
41-
const needsOptional = optional || hasDefault;
40+
const needsDefault = result.meta.default !== undefined;
41+
const needsOptional = optional || needsDefault;
4242
const needsNullable = result.meta.nullable;
4343

4444
let type = result.type;
@@ -48,7 +48,7 @@ export function createVisitor(
4848
const optionalType = ctx.plugin.external('typing.Optional');
4949
type = $(optionalType).slice(type ?? ctx.plugin.external('typing.Any'));
5050
if (needsOptional) {
51-
fieldConstraints.default = hasDefault ? result.meta.default : null;
51+
fieldConstraints.default = needsDefault ? result.meta.default : null;
5252
}
5353
}
5454

packages/openapi-ts-tests/main/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,5 @@
4444
"typescript": "6.0.2",
4545
"vue": "3.5.25",
4646
"zod": "4.3.6"
47-
},
48-
"engines": {
49-
"node": ">=22.13.0"
5047
}
5148
}

packages/openapi-ts-tests/nestjs/v11/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@
99
"devDependencies": {
1010
"@hey-api/openapi-ts": "workspace:*",
1111
"typescript": "6.0.2"
12-
},
13-
"engines": {
14-
"node": ">=22.13.0"
1512
}
1613
}

packages/openapi-ts-tests/orpc/v1/__snapshots__/3.0.x/default/zod.gen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ export const zPost = z.object({
3636
export const zCreatePostInput = z.object({
3737
title: z.string(),
3838
content: z.string(),
39-
status: z.enum(['draft', 'published']).optional().default('draft')
39+
status: z.enum(['draft', 'published']).default('draft').optional()
4040
});
4141

4242
export const zGetUsersQuery = z.object({
43-
limit: z.int().optional().default(10),
44-
offset: z.int().optional().default(0)
43+
limit: z.int().default(10).optional(),
44+
offset: z.int().default(0).optional()
4545
});
4646

4747
/**
@@ -119,7 +119,7 @@ export const zGetPostByIdPath = z.object({
119119
});
120120

121121
export const zGetPostByIdQuery = z.object({
122-
includeComments: z.boolean().optional().default(false)
122+
includeComments: z.boolean().default(false).optional()
123123
});
124124

125125
/**

packages/openapi-ts-tests/orpc/v1/__snapshots__/3.1.x/contracts-custom-naming/zod.gen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ export const zPost = z.object({
3636
export const zCreatePostInput = z.object({
3737
title: z.string(),
3838
content: z.string(),
39-
status: z.enum(['draft', 'published']).optional().default('draft')
39+
status: z.enum(['draft', 'published']).default('draft').optional()
4040
});
4141

4242
export const zGetUsersQuery = z.object({
43-
limit: z.int().optional().default(10),
44-
offset: z.int().optional().default(0)
43+
limit: z.int().default(10).optional(),
44+
offset: z.int().default(0).optional()
4545
});
4646

4747
/**
@@ -119,7 +119,7 @@ export const zGetPostByIdPath = z.object({
119119
});
120120

121121
export const zGetPostByIdQuery = z.object({
122-
includeComments: z.boolean().optional().default(false)
122+
includeComments: z.boolean().default(false).optional()
123123
});
124124

125125
/**

packages/openapi-ts-tests/orpc/v1/__snapshots__/3.1.x/contracts-nesting-id/zod.gen.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ export const zPost = z.object({
3636
export const zCreatePostInput = z.object({
3737
title: z.string(),
3838
content: z.string(),
39-
status: z.enum(['draft', 'published']).optional().default('draft')
39+
status: z.enum(['draft', 'published']).default('draft').optional()
4040
});
4141

4242
export const zGetUsersQuery = z.object({
43-
limit: z.int().optional().default(10),
44-
offset: z.int().optional().default(0)
43+
limit: z.int().default(10).optional(),
44+
offset: z.int().default(0).optional()
4545
});
4646

4747
/**
@@ -119,7 +119,7 @@ export const zGetPostByIdPath = z.object({
119119
});
120120

121121
export const zGetPostByIdQuery = z.object({
122-
includeComments: z.boolean().optional().default(false)
122+
includeComments: z.boolean().default(false).optional()
123123
});
124124

125125
/**

0 commit comments

Comments
 (0)