Skip to content

Commit 6439bbd

Browse files
author
Eric Butler
authoredAug 16, 2023
fix: zod client crash when schema has circular ref (#901)
Fixes the error: > RangeError: Maximum call stack size exceeded
1 parent b9cf531 commit 6439bbd

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed
 

‎packages/zod/src/index.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,25 @@ const deference = (
307307
schema: SchemaObject | ReferenceObject,
308308
context: ContextSpecs,
309309
): SchemaObject => {
310-
const { schema: resolvedSchema } = resolveRef<SchemaObject>(schema, context);
310+
const refName = '$ref' in schema ? schema.$ref : undefined;
311+
if (refName && context.parents?.includes(refName)) {
312+
return {};
313+
}
311314

312-
return Object.entries(resolvedSchema).reduce((acc, [key, value]) => {
313-
acc[key] = deferenceScalar(value, context);
315+
const childContext: ContextSpecs = {
316+
...context,
317+
...(refName
318+
? { parents: [...(context.parents || []), refName] }
319+
: undefined),
320+
};
314321

322+
const { schema: resolvedSchema } = resolveRef<SchemaObject>(
323+
schema,
324+
childContext,
325+
);
326+
327+
return Object.entries(resolvedSchema).reduce((acc, [key, value]) => {
328+
acc[key] = deferenceScalar(value, childContext);
315329
return acc;
316330
}, {} as any);
317331
};

‎tests/configs/zod.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from 'orval';
2+
3+
export default defineConfig({
4+
basic: {
5+
output: {
6+
target: '../generated/zod',
7+
client: 'zod',
8+
},
9+
input: {
10+
target: '../specifications/circular.yaml',
11+
},
12+
},
13+
});

‎tests/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"generate:vue-query": "yarn orval --config ./configs/vue-query.config.ts",
1616
"generate:swr": "yarn orval --config ./configs/swr.config.ts",
1717
"generate:multi-file": "yarn orval --config ./configs/multi-file.config.ts",
18+
"generate:zod": "yarn orval --config ./configs/zod.config.ts",
1819
"build": "tsc"
1920
},
2021
"author": "Victor Bury",
@@ -33,6 +34,7 @@
3334
"axios": "^0.26.1",
3435
"msw": "^0.35.0",
3536
"swr": "^2.1.2",
36-
"vue": "3.2.47"
37+
"vue": "3.2.47",
38+
"zod": "^3.21.4"
3739
}
3840
}

‎tests/specifications/circular.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Circular references
4+
version: 0.0.0
5+
paths:
6+
/example:
7+
get:
8+
summary: Example
9+
responses:
10+
'200':
11+
description: 'Example'
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/Node'
16+
components:
17+
schemas:
18+
Node:
19+
type: object
20+
properties:
21+
id:
22+
type: integer
23+
name:
24+
type: string
25+
child:
26+
$ref: '#/components/schemas/Node'

‎tests/yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1373,3 +1373,8 @@ yargs@^17.0.1:
13731373
string-width "^4.2.0"
13741374
y18n "^5.0.5"
13751375
yargs-parser "^20.2.2"
1376+
1377+
zod@^3.21.4:
1378+
version "3.21.4"
1379+
resolved "https://registry.yarnpkg.com/zod/-/zod-3.21.4.tgz#10882231d992519f0a10b5dd58a38c9dabbb64db"
1380+
integrity sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw==

1 commit comments

Comments
 (1)

vercel[bot] commented on Aug 16, 2023

@vercel[bot]
Please sign in to comment.