Skip to content

Commit 9fff32c

Browse files
committed
Merge branch 'master' of github.com:samchungy/zod-openapi
2 parents 69cd132 + 70a0734 commit 9fff32c

File tree

6 files changed

+73
-10
lines changed

6 files changed

+73
-10
lines changed

.changeset/moody-books-cover.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
'zod-openapi': minor
3+
---
4+
5+
Change `ZodUndefined` behaviour
6+
7+
This restores how `z.undefined()` is rendered to pre Zod v3.25.75.
8+
9+
It is now rendered as:
10+
11+
```json
12+
{
13+
"not": {}
14+
}
15+
```
16+
17+
If you want to override this behaviour you can customise this with the `override` function passed into the `createDocument` function.
18+
19+
eg.
20+
21+
```ts
22+
import { createDocument } from 'zod-openapi';
23+
24+
createDocument(
25+
z.object({
26+
name: z.undefined().optional(),
27+
}),
28+
{
29+
override: (ctx) => {
30+
if (ctx.zodSchema._zod.def.type === 'undefined') {
31+
// This will change the behaviour back to throwing an error
32+
delete ctx.jsonSchema.not;
33+
}
34+
},
35+
},
36+
);
37+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"skuba": "11.1.0",
6666
"tsdown": "0.12.9",
6767
"yaml": "2.8.0",
68-
"zod": "4.0.1"
68+
"zod": "4.0.5"
6969
},
7070
"peerDependencies": {
7171
"zod": "^3.25.74 || ^4.0.0"

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/create/schema/override.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export const override: Override = (ctx) => {
8484
}
8585
break;
8686
}
87+
case 'undefined': {
88+
ctx.jsonSchema.not = {};
89+
break;
90+
}
8791
}
8892
};
8993

src/create/schema/tests/object.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ describe('required', () => {
220220
const ref = z.string().meta({ id: 'ref' });
221221
const oref = z.string().optional().meta({ id: 'oref' });
222222
const schema = z.object({
223+
a: z.undefined(),
223224
b: z.never().optional(),
224225
d: z.string().optional(),
225226
e: z.string().nullish(),
@@ -242,6 +243,7 @@ describe('required', () => {
242243
schema: {
243244
type: 'object',
244245
properties: {
246+
a: { not: {} },
245247
b: { not: {} },
246248
d: { type: 'string' },
247249
e: { anyOf: [{ type: 'string' }, { type: 'null' }] },
@@ -275,6 +277,7 @@ describe('required', () => {
275277
d: z.literal(null),
276278
e: z.union([z.string(), z.number()]),
277279
f: z.custom((r) => r !== undefined),
280+
g: z.undefined(),
278281
});
279282

280283
const ctx = createInputContext();
@@ -293,6 +296,7 @@ describe('required', () => {
293296
d: { type: 'null', const: null },
294297
e: { anyOf: [{ type: 'string' }, { type: 'number' }] },
295298
f: {},
299+
g: { not: {} },
296300
},
297301
required: ['a', 'b', 'c', 'd', 'e', 'f'],
298302
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as z from 'zod/v4';
2+
3+
import { type SchemaResult, createSchema } from '../schema';
4+
5+
describe('undefined', () => {
6+
it('should create an empty schema for undefined', () => {
7+
const schema = z.undefined();
8+
9+
const result = createSchema(schema);
10+
11+
expect(result).toEqual<SchemaResult>({
12+
schema: {
13+
not: {},
14+
},
15+
components: {},
16+
});
17+
});
18+
});

0 commit comments

Comments
 (0)