From 3bc29c859cc5532df0ffd633ce7febab18a1514e Mon Sep 17 00:00:00 2001 From: HamishWHC <20359560+HamishWHC@users.noreply.github.com> Date: Sun, 26 Jan 2025 13:31:28 +1100 Subject: [PATCH] fix: recurse into ZodArray during openapi generation --- packages/zod/src/converter.test.ts | 28 ++++++++++++++++++++++++++++ packages/zod/src/converter.ts | 2 ++ 2 files changed, 30 insertions(+) diff --git a/packages/zod/src/converter.test.ts b/packages/zod/src/converter.test.ts index b64a23350..b1e661489 100644 --- a/packages/zod/src/converter.test.ts +++ b/packages/zod/src/converter.test.ts @@ -69,6 +69,7 @@ describe('array types', () => { const schema = z.array(z.string()) expect(zodToJsonSchema(schema)).toEqual({ type: 'array', + items: { type: 'string' }, }) }) @@ -76,6 +77,9 @@ describe('array types', () => { const schema = z.array(z.string()).min(1).max(5) expect(zodToJsonSchema(schema)).toEqual({ type: 'array', + items: { + type: 'string', + }, minItems: 1, maxItems: 5, }) @@ -268,12 +272,36 @@ describe('lazy types', () => { }), ) + const tree1 = { + type: 'object', + properties: { + value: { type: 'string' }, + children: { + type: 'array', + items: {}, + }, + }, + required: ['value'], + } + const tree2 = { + type: 'object', + properties: { + value: { type: 'string' }, + children: { + type: 'array', + items: tree1, + }, + }, + required: ['value'], + } + expect(zodToJsonSchema(treeSchema, { maxLazyDepth: 2 })).toEqual({ type: 'object', properties: { value: { type: 'string' }, children: { type: 'array', + items: tree2, }, }, required: ['value'], diff --git a/packages/zod/src/converter.ts b/packages/zod/src/converter.ts index 15d4be1ef..978a58a45 100644 --- a/packages/zod/src/converter.ts +++ b/packages/zod/src/converter.ts @@ -364,6 +364,8 @@ export function zodToJsonSchema( const json: JSONSchema.JSONSchema = { type: 'array' } + json.items = zodToJsonSchema(def.type, childOptions) + if (def.exactLength) { json.maxItems = def.exactLength.value json.minItems = def.exactLength.value