diff --git a/src/generator/schema.ts b/src/generator/schema.ts index fb9bb1d3..a278ccc2 100644 --- a/src/generator/schema.ts +++ b/src/generator/schema.ts @@ -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 = {}; + 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 } : {}), diff --git a/src/utils/zod.ts b/src/utils/zod.ts index 1ec8f13e..ae58f180 100644 --- a/src/utils/zod.ts +++ b/src/utils/zod.ts @@ -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;