3
import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
4
5
const MAX_REF_CHAIN_DEPTH = 100;
6
+const MAX_NODE_DEPTH = 512;
7
const CYCLE_BREAK_SENTINEL = Object.freeze({ type: 'object', additionalProperties: true });
8
const INTERNAL_REF_PREFIX = '#';
9
103
if (!isPlainObject(schema)) return schema;
104
105
const root = schema as Record<string, unknown>;
106
+ const visiting = new WeakSet<object>();
107
106
- const walk = (node: unknown, path: string[], depth: number): unknown => {
108
+ const walk = (node: unknown, path: string[], depth: number, nodeDepth: number): unknown => {
109
+ if (nodeDepth >= MAX_NODE_DEPTH) {
110
+ throw new JsonSchemaRefResolutionError(
111
+ `JSON Schema node depth exceeded cap (${MAX_NODE_DEPTH})`
112
+ );
113
+ }
114
if (Array.isArray(node)) {
108
- return node.map((item, index) => walk(item, [...path, String(index)], depth));
115
+ if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
116
+ visiting.add(node);
117
+ try {
118
+ return node.map((item, index) =>
119
+ walk(item, [...path, String(index)], depth, nodeDepth + 1)
120
+ );
121
+ } finally {
122
+ visiting.delete(node);
123
+ }
124
}
125
if (!isPlainObject(node)) return node;
111
-
112
- if (typeof node.$ref === 'string') {
113
- const ref = node.$ref;
114
- // External refs are out of scope; leave them alone.
115
- if (!ref.startsWith(INTERNAL_REF_PREFIX)) {
116
- const cloned: Record<string, unknown> = {};
117
- for (const [key, value] of Object.entries(node)) {
118
- cloned[key] = walk(value, [...path, key], depth);
126
+ if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
127
+ visiting.add(node);
128
+ try {
129
+ if (typeof node.$ref === 'string') {
130
+ const ref = node.$ref;
131
+ // External refs are out of scope; leave them alone.
132
+ if (!ref.startsWith(INTERNAL_REF_PREFIX)) {
133
+ const cloned: Record<string, unknown> = {};
134
+ for (const [key, value] of Object.entries(node)) {
135
+ cloned[key] = walk(value, [...path, key], depth, nodeDepth + 1);
136
+ }
137
+ return cloned;
138
}
120
- return cloned;
121
- }
139
123
- if (depth >= MAX_REF_CHAIN_DEPTH) {
124
- throw new JsonSchemaRefResolutionError(
125
- `JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`,
126
- { meta: { ref } }
127
- );
128
- }
140
+ if (depth >= MAX_REF_CHAIN_DEPTH) {
141
+ throw new JsonSchemaRefResolutionError(
142
+ `JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`,
143
+ { meta: { ref } }
144
+ );
145
+ }
146
130
- // Cycle detection: if `ref` already appears in the current path, this
131
- // resolution would re-enter itself. Break the cycle.
132
- if (path.includes(`@ref:${ref}`)) {
133
- return { ...CYCLE_BREAK_SENTINEL };
134
- }
147
+ // Cycle detection: if `ref` already appears in the current path, this
148
+ // resolution would re-enter itself. Break the cycle.
149
+ if (path.includes(`@ref:${ref}`)) {
150
+ return { ...CYCLE_BREAK_SENTINEL };
151
+ }
152
136
- const { target } = resolvePointer(root, ref);
137
- const resolved = walk(target, [...path, `@ref:${ref}`], depth + 1);
153
+ const { target } = resolvePointer(root, ref);
154
+ const resolved = walk(target, [...path, `@ref:${ref}`], depth + 1, nodeDepth + 1);
155
139
- // Shallow-merge sibling keywords (Draft 2020-12 semantics: siblings win
140
- // on collision). Draft 7 ignores siblings entirely, but the Composio
141
- // tool surface admits both drafts so we honor siblings for safety.
142
- const { $ref: _omit, ...siblings } = node;
143
- void _omit;
144
- if (Object.keys(siblings).length === 0) {
145
- return resolved;
146
- }
147
- if (!isPlainObject(resolved)) {
148
- return resolved;
149
- }
150
- const merged: Record<string, unknown> = { ...resolved };
151
- for (const [key, value] of Object.entries(siblings)) {
152
- merged[key] = walk(value, [...path, key], depth);
156
+ // Shallow-merge sibling keywords (Draft 2020-12 semantics: siblings win
157
+ // on collision). Draft 7 ignores siblings entirely, but the Composio
158
+ // tool surface admits both drafts so we honor siblings for safety.
159
+ const { $ref: _omit, ...siblings } = node;
160
+ void _omit;
161
+ if (Object.keys(siblings).length === 0) {
162
+ return resolved;
163
+ }
164
+ if (!isPlainObject(resolved)) {
165
+ return resolved;
166
+ }
167
+ const merged: Record<string, unknown> = { ...resolved };
168
+ for (const [key, value] of Object.entries(siblings)) {
169
+ merged[key] = walk(value, [...path, key], depth, nodeDepth + 1);
170
+ }
171
+ return merged;
172
}
154
- return merged;
155
- }
173
157
- const cloned: Record<string, unknown> = {};
158
- for (const [key, value] of Object.entries(node)) {
159
- cloned[key] = walk(value, [...path, key], depth);
174
+ const cloned: Record<string, unknown> = {};
175
+ for (const [key, value] of Object.entries(node)) {
176
+ cloned[key] = walk(value, [...path, key], depth, nodeDepth + 1);
177
+ }
178
+ return cloned;
179
+ } finally {
180
+ visiting.delete(node);
181
}
161
- return cloned;
182
};
183
164
- const out = walk(root, [], 0);
184
+ const out = walk(root, [], 0, 0);
185
if (isPlainObject(out)) {
186
delete out.$defs;
187
delete out.definitions;