Skip to content

Commit cb869f7

Browse files
authored
Merge pull request #2946 from hey-api/copilot/fix-defaults-zod-generation
Fix missing defaults in Zod generation for $ref and anyOf schemas (OpenAPI 3.1.x only)
2 parents 83d5749 + fcf0e90 commit cb869f7

File tree

14 files changed

+254
-14
lines changed

14 files changed

+254
-14
lines changed

.changeset/chilled-wolves-pump.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+
parser: merge `default` keyword with `$ref` in OpenAPI 3.1

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/plugins/valibot/default/valibot.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,9 @@ export const vCallWithDefaultParametersData = v.object({
14551455
'Error'
14561456
])),
14571457
parameterModel: v.optional(v.union([
1458-
vModelWithString,
1458+
v.optional(vModelWithString, {
1459+
prop: 'Hello World!'
1460+
}),
14591461
v.null()
14601462
]))
14611463
}))
@@ -1473,7 +1475,9 @@ export const vCallWithDefaultOptionalParametersData = v.object({
14731475
'Warning',
14741476
'Error'
14751477
])),
1476-
parameterModel: v.optional(vModelWithString)
1478+
parameterModel: v.optional(vModelWithString, {
1479+
prop: 'Hello World!'
1480+
})
14771481
}))
14781482
});
14791483

packages/openapi-ts-tests/zod/v3/__snapshots__/3.1.x/mini/default/zod.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,9 @@ export const zCallWithDefaultParametersData = z.object({
14861486
'Error'
14871487
])),
14881488
parameterModel: z.optional(z.union([
1489-
zModelWithString,
1489+
z._default(zModelWithString, {
1490+
prop: 'Hello World!'
1491+
}),
14901492
z.null()
14911493
]))
14921494
}))
@@ -1504,7 +1506,9 @@ export const zCallWithDefaultOptionalParametersData = z.object({
15041506
'Warning',
15051507
'Error'
15061508
])),
1507-
parameterModel: z.optional(zModelWithString)
1509+
parameterModel: z._default(z.optional(zModelWithString), {
1510+
prop: 'Hello World!'
1511+
})
15081512
}))
15091513
});
15101514

packages/openapi-ts-tests/zod/v3/__snapshots__/3.1.x/v3/default/zod.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,9 @@ export const zCallWithDefaultParametersData = z.object({
14841484
'Error'
14851485
]).optional(),
14861486
parameterModel: z.union([
1487-
zModelWithString,
1487+
zModelWithString.default({
1488+
prop: 'Hello World!'
1489+
}),
14881490
z.null()
14891491
]).optional()
14901492
}).optional()
@@ -1502,7 +1504,9 @@ export const zCallWithDefaultOptionalParametersData = z.object({
15021504
'Warning',
15031505
'Error'
15041506
]).optional(),
1505-
parameterModel: zModelWithString.optional()
1507+
parameterModel: zModelWithString.optional().default({
1508+
prop: 'Hello World!'
1509+
})
15061510
}).optional()
15071511
});
15081512

packages/openapi-ts-tests/zod/v3/__snapshots__/3.1.x/v4/default/zod.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,9 @@ export const zCallWithDefaultParametersData = z.object({
14861486
'Error'
14871487
])),
14881488
parameterModel: z.optional(z.union([
1489-
zModelWithString,
1489+
zModelWithString.default({
1490+
prop: 'Hello World!'
1491+
}),
14901492
z.null()
14911493
]))
14921494
}))
@@ -1504,7 +1506,9 @@ export const zCallWithDefaultOptionalParametersData = z.object({
15041506
'Warning',
15051507
'Error'
15061508
])),
1507-
parameterModel: z.optional(zModelWithString)
1509+
parameterModel: z.optional(zModelWithString).default({
1510+
prop: 'Hello World!'
1511+
})
15081512
}))
15091513
});
15101514

packages/openapi-ts-tests/zod/v4/__snapshots__/3.1.x/mini/default/zod.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,9 @@ export const zCallWithDefaultParametersData = z.object({
14861486
'Error'
14871487
])),
14881488
parameterModel: z.optional(z.union([
1489-
zModelWithString,
1489+
z._default(zModelWithString, {
1490+
prop: 'Hello World!'
1491+
}),
14901492
z.null()
14911493
]))
14921494
}))
@@ -1504,7 +1506,9 @@ export const zCallWithDefaultOptionalParametersData = z.object({
15041506
'Warning',
15051507
'Error'
15061508
])),
1507-
parameterModel: z.optional(zModelWithString)
1509+
parameterModel: z._default(z.optional(zModelWithString), {
1510+
prop: 'Hello World!'
1511+
})
15081512
}))
15091513
});
15101514

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import * as z from 'zod/mini';
4+
5+
export const zAudioFormat = z.enum([
6+
'pcm_16bit_44.1khz',
7+
'pcm_16bit_24khz'
8+
]);
9+
10+
export const zLanguage = z.enum([
11+
'en',
12+
'es',
13+
'fr'
14+
]);
15+
16+
export const zNestedConfig = z.object({
17+
model: z._default(z.string(), 'gpt-4'),
18+
temperature: z._default(z.number(), 1)
19+
});
20+
21+
export const zTestSchema = z.object({
22+
primitiveDefault: z._default(z.number(), 42),
23+
refWithPrimitiveDefault: z._default(zAudioFormat, 'pcm_16bit_44.1khz'),
24+
refWithObjectDefault: z._default(zNestedConfig, {
25+
model: 'gpt-4',
26+
temperature: 1
27+
}),
28+
anyOfWithNullDefault: z._default(z.union([
29+
z.number(),
30+
z.null()
31+
]), null),
32+
anyOfWithRefAndNullDefault: z._default(z.union([
33+
zLanguage,
34+
z.null()
35+
]), null),
36+
optionalAnyOfWithDefault: z._default(z.optional(z.union([
37+
z.string(),
38+
z.null()
39+
])), null)
40+
});

packages/openapi-ts-tests/zod/v4/__snapshots__/3.1.x/v3/default/zod.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,9 @@ export const zCallWithDefaultParametersData = z.object({
14841484
'Error'
14851485
]).optional(),
14861486
parameterModel: z.union([
1487-
zModelWithString,
1487+
zModelWithString.default({
1488+
prop: 'Hello World!'
1489+
}),
14881490
z.null()
14891491
]).optional()
14901492
}).optional()
@@ -1502,7 +1504,9 @@ export const zCallWithDefaultOptionalParametersData = z.object({
15021504
'Warning',
15031505
'Error'
15041506
]).optional(),
1505-
parameterModel: zModelWithString.optional()
1507+
parameterModel: zModelWithString.optional().default({
1508+
prop: 'Hello World!'
1509+
})
15061510
}).optional()
15071511
});
15081512

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file is auto-generated by @hey-api/openapi-ts
2+
3+
import { z } from 'zod/v3';
4+
5+
export const zAudioFormat = z.enum([
6+
'pcm_16bit_44.1khz',
7+
'pcm_16bit_24khz'
8+
]);
9+
10+
export const zLanguage = z.enum([
11+
'en',
12+
'es',
13+
'fr'
14+
]);
15+
16+
export const zNestedConfig = z.object({
17+
model: z.string().default('gpt-4'),
18+
temperature: z.number().default(1)
19+
});
20+
21+
export const zTestSchema = z.object({
22+
primitiveDefault: z.number().default(42),
23+
refWithPrimitiveDefault: zAudioFormat.default('pcm_16bit_44.1khz'),
24+
refWithObjectDefault: zNestedConfig.default({
25+
model: 'gpt-4',
26+
temperature: 1
27+
}),
28+
anyOfWithNullDefault: z.union([
29+
z.number(),
30+
z.null()
31+
]).default(null),
32+
anyOfWithRefAndNullDefault: z.union([
33+
zLanguage,
34+
z.null()
35+
]).default(null),
36+
optionalAnyOfWithDefault: z.union([
37+
z.string(),
38+
z.null()
39+
]).optional().default(null)
40+
});

packages/openapi-ts-tests/zod/v4/__snapshots__/3.1.x/v4/default/zod.gen.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,9 @@ export const zCallWithDefaultParametersData = z.object({
14861486
'Error'
14871487
])),
14881488
parameterModel: z.optional(z.union([
1489-
zModelWithString,
1489+
zModelWithString.default({
1490+
prop: 'Hello World!'
1491+
}),
14901492
z.null()
14911493
]))
14921494
}))
@@ -1504,7 +1506,9 @@ export const zCallWithDefaultOptionalParametersData = z.object({
15041506
'Warning',
15051507
'Error'
15061508
])),
1507-
parameterModel: z.optional(zModelWithString)
1509+
parameterModel: z.optional(zModelWithString).default({
1510+
prop: 'Hello World!'
1511+
})
15081512
}))
15091513
});
15101514

0 commit comments

Comments
 (0)