From cb3c42ea7c5278413682fb733b82522da3ad6a81 Mon Sep 17 00:00:00 2001 From: Bigint <69431456+bigint@users.noreply.github.com> Date: Tue, 22 Jul 2025 16:15:34 +0530 Subject: [PATCH] fix: zod optional schema --- .../zod-to-json-schema/parsers/optional.ts | 7 +++++-- tests/helpers/zod.test.ts | 15 ++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/_vendor/zod-to-json-schema/parsers/optional.ts b/src/_vendor/zod-to-json-schema/parsers/optional.ts index 9b3e9731f..6948d7c51 100644 --- a/src/_vendor/zod-to-json-schema/parsers/optional.ts +++ b/src/_vendor/zod-to-json-schema/parsers/optional.ts @@ -3,8 +3,11 @@ import { JsonSchema7Type, parseDef } from '../parseDef'; import { Refs } from '../Refs'; export const parseOptionalDef = (def: ZodOptionalDef, refs: Refs): JsonSchema7Type | undefined => { - if (refs.currentPath.toString() === refs.propertyPath?.toString()) { - return parseDef(def.innerType._def, refs); + if ( + refs.propertyPath && + refs.currentPath.slice(0, refs.propertyPath.length).toString() === refs.propertyPath.toString() + ) { + return parseDef(def.innerType._def, { ...refs, currentPath: refs.currentPath }); } const innerSchema = parseDef(def.innerType._def, { diff --git a/tests/helpers/zod.test.ts b/tests/helpers/zod.test.ts index 2eb8909a8..eb56ca778 100644 --- a/tests/helpers/zod.test.ts +++ b/tests/helpers/zod.test.ts @@ -71,18 +71,11 @@ describe('zodResponseFormat', () => { "units": { "anyOf": [ { - "anyOf": [ - { - "not": {}, - }, - { - "enum": [ - "c", - "f", - ], - "type": "string", - }, + "enum": [ + "c", + "f", ], + "type": "string", }, { "type": "null",