Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/generator/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,21 @@ export const getRequestBodyObject = (
mask[pathParameter] = true;
});
const o = schema.meta();
const dedupedSchema = schema.omit(mask).meta({
// Use omit() when possible, fall back to manual shape filtering for refined schemas
let dedupedSchema;
try {
dedupedSchema = schema.omit(mask);
} catch {
// Zod 4 throws on .omit() for schemas with refinements - rebuild from shape
const filteredShape: Record<string, z.ZodType> = {};
for (const [key, value] of Object.entries(schema.shape)) {
if (!mask[key]) {
filteredShape[key] = value as z.ZodType;
}
}
dedupedSchema = z.object(filteredShape);
}
dedupedSchema = dedupedSchema.meta({
...(o?.title ? { title: o?.title } : {}),
...(o?.description ? { description: o?.description } : {}),
...(o?.examples ? { examples: o?.examples } : {}),
Expand Down
8 changes: 8 additions & 0 deletions src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ export const unwrapZodType = (type: $ZodType, unwrapPreprocess: boolean): ZodTyp
if (instanceofZodTypeKind(type, 'default')) {
return unwrapZodType((type as z.ZodDefault<$ZodTypes>).unwrap(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, 'prefault')) {
return unwrapZodType((type as z.ZodDefault<$ZodTypes>).unwrap(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, 'lazy')) {
return unwrapZodType((type as z.ZodLazy<$ZodTypes>).def.getter(), unwrapPreprocess);
}
if (instanceofZodTypeKind(type, 'pipe') && unwrapPreprocess) {
// For .transform() pipes in Zod 4, def.out is a 'transform' type -
// use def.in (the input schema shape) instead
if (instanceofZodTypeKind((type as z.ZodPipe<$ZodTypes>)._zod.def.out, 'transform')) {
return unwrapZodType((type as z.ZodPipe<$ZodTypes>)._zod.def.in, unwrapPreprocess);
}
return unwrapZodType((type as z.ZodPipe<$ZodTypes>).def.out, unwrapPreprocess);
}
return type as ZodType;
Expand Down