Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 22, 22:00 UTC
Fix: JSON Schema to Zod Parsing errors in default/arrays (#1761)
Musthaq Ahamad committed
Jul 16, 2025 at 15:18 UTC
77e96e43dc5cb8fd81afe7348fc7a4d53e6f3444
5 files changed
+176
-25
.changeset/soft-corners-tell.md
new
+14
@@ -0,0 +1,14 @@
1
+---
2
+'@composio/core': patch
3
+'@composio/json-schema-to-zod': patch
4
+'@composio/anthropic': patch
5
+'@composio/cloudflare': patch
6
+'@composio/google': patch
7
+'@composio/langchain': patch
8
+'@composio/mastra': patch
9
+'@composio/openai': patch
10
+'@composio/openai-agents': patch
11
+'@composio/vercel': patch
12
+---
13
+
14
+Fix JSON Schema to Zod Parsing
pnpm-lock.yaml
+3
-19
@@ -9350,14 +9350,6 @@ snapshots:
9350
chai: 5.2.0
9351
tinyrainbow: 2.0.0
9352
9353
- '@vitest/mocker@3.2.4(vite@6.3.5(@types/node@20.19.1)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))':
9354
- dependencies:
9355
- '@vitest/spy': 3.2.4
9356
- estree-walker: 3.0.3
9357
- magic-string: 0.30.17
9358
- optionalDependencies:
9359
- vite: 6.3.5(@types/node@20.19.1)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
9360
-
9353
'@vitest/mocker@3.2.4(vite@6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))':
9354
dependencies:
9355
'@vitest/spy': 3.2.4
@@ -9366,14 +9358,6 @@ snapshots:
9358
optionalDependencies:
9359
vite: 6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
9360
9369
- '@vitest/mocker@3.2.4(vite@6.3.5(@types/node@24.0.3)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))':
9370
- dependencies:
9371
- '@vitest/spy': 3.2.4
9372
- estree-walker: 3.0.3
9373
- magic-string: 0.30.17
9374
- optionalDependencies:
9375
- vite: 6.3.5(@types/node@24.0.3)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
9376
-
9361
'@vitest/pretty-format@3.2.4':
9362
dependencies:
9363
tinyrainbow: 2.0.0
@@ -9403,7 +9387,7 @@ snapshots:
9387
sirv: 3.0.1
9388
tinyglobby: 0.2.14
9389
tinyrainbow: 2.0.0
9406
- vitest: 3.2.4(@types/node@24.0.3)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
9390
+ vitest: 3.2.4(@types/node@22.15.32)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
9391
9392
'@vitest/utils@3.2.4':
9393
dependencies:
@@ -12234,7 +12218,7 @@ snapshots:
12218
dependencies:
12219
'@types/chai': 5.2.2
12220
'@vitest/expect': 3.2.4
12237
- '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@20.19.1)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
12221
+ '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
12222
'@vitest/pretty-format': 3.2.4
12223
'@vitest/runner': 3.2.4
12224
'@vitest/snapshot': 3.2.4
@@ -12318,7 +12302,7 @@ snapshots:
12302
dependencies:
12303
'@types/chai': 5.2.2
12304
'@vitest/expect': 3.2.4
12321
- '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@24.0.3)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
12305
+ '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
12306
'@vitest/pretty-format': 3.2.4
12307
'@vitest/runner': 3.2.4
12308
'@vitest/snapshot': 3.2.4
ts/packages/json-schema-to-zod/src/parsers/parse-array.ts
+14
-4
@@ -14,7 +14,7 @@ export const parseArray = (jsonSchema: JsonSchemaObject & { type: 'array' }, ref
14
// Handle anyOf pattern first
15
if (its.an.anyOf(jsonSchema)) {
16
const types = new Set<string>();
17
- let items: JsonSchema | undefined;
17
+ const itemsSchemas: JsonSchema[] = [];
18
19
// Collect all types and items from anyOf array
20
jsonSchema.anyOf.forEach(option => {
@@ -24,15 +24,25 @@ export const parseArray = (jsonSchema: JsonSchemaObject & { type: 'array' }, ref
24
if (typeof option === 'object' && option.items) {
25
const optionItems = option.items;
26
if (!Array.isArray(optionItems) && typeof optionItems === 'object') {
27
- items = optionItems;
27
+ itemsSchemas.push(optionItems);
28
}
29
}
30
});
31
32
+ // If we have multiple item schemas, create a union
33
+ let finalItems: JsonSchema | undefined;
34
+ if (itemsSchemas.length === 1) {
35
+ finalItems = itemsSchemas[0];
36
+ } else if (itemsSchemas.length > 1) {
37
+ finalItems = { anyOf: itemsSchemas };
38
+ }
39
+
40
// Create new schema with combined types
41
const newSchema: JsonSchemaObject = {
34
- type: Array.from(types) as JSONSchema7TypeName[],
35
- ...(items && { items }),
42
+ ...(types.size > 0
43
+ ? { type: Array.from(types) as JSONSchema7TypeName[] }
44
+ : { type: 'array' }),
45
+ ...(finalItems && { items: finalItems }),
46
};
47
48
// Copy over metadata fields
ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
+6
-1
@@ -38,7 +38,12 @@ function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object'
38
typeof propJsonSchema === 'object' &&
39
'default' in propJsonSchema
40
) {
41
- properties[key] = propZodSchema.optional().default(propJsonSchema.default);
41
+ // If default is null, make the field nullable with the null default
42
+ if (propJsonSchema.default === null) {
43
+ properties[key] = propZodSchema.nullable().optional().default(null);
44
+ } else {
45
+ properties[key] = propZodSchema.optional().default(propJsonSchema.default);
46
+ }
47
} else {
48
properties[key] = required ? propZodSchema : propZodSchema.optional();
49
}
ts/packages/json-schema-to-zod/test/json-to-zod.test.ts
+139
-1
@@ -204,6 +204,99 @@ describe('jsonSchemaToZod', () => {
204
expect(() => zodSchema.parse([])).toThrow();
205
expect(() => zodSchema.parse(['one', 'two', 'three', 'four'])).toThrow();
206
});
207
+
208
+ it('should handle arrays with anyOf patterns', () => {
209
+ const schema: JsonSchema = {
210
+ type: 'array',
211
+ anyOf: [
212
+ {
213
+ type: 'array',
214
+ items: { type: 'string' },
215
+ },
216
+ {
217
+ type: 'array',
218
+ items: { type: 'number' },
219
+ },
220
+ ],
221
+ };
222
+ const zodSchema = jsonSchemaToZod(schema);
223
+
224
+ // Should accept string arrays
225
+ expect(zodSchema.parse(['hello', 'world'])).toEqual(['hello', 'world']);
226
+
227
+ // Should accept number arrays
228
+ expect(zodSchema.parse([1, 2, 3])).toEqual([1, 2, 3]);
229
+
230
+ // Should accept mixed arrays (union of item types)
231
+ expect(zodSchema.parse(['hello', 123])).toEqual(['hello', 123]);
232
+ });
233
+
234
+ it('should handle arrays with anyOf at top level without explicit types', () => {
235
+ const schema: JsonSchema = {
236
+ type: 'array',
237
+ anyOf: [
238
+ {
239
+ items: { type: 'string' },
240
+ },
241
+ {
242
+ items: { type: 'number' },
243
+ },
244
+ ],
245
+ };
246
+ const zodSchema = jsonSchemaToZod(schema);
247
+
248
+ // Should accept string arrays
249
+ expect(zodSchema.parse(['hello', 'world'])).toEqual(['hello', 'world']);
250
+
251
+ // Should accept number arrays
252
+ expect(zodSchema.parse([1, 2, 3])).toEqual([1, 2, 3]);
253
+ });
254
+
255
+ it('should handle arrays with object items having default null', () => {
256
+ const schema: JsonSchema = {
257
+ type: 'array',
258
+ items: {
259
+ type: 'object',
260
+ properties: {
261
+ name: { type: 'string' },
262
+ status: { type: 'string', default: null },
263
+ },
264
+ required: ['name'],
265
+ },
266
+ };
267
+ const zodSchema = jsonSchemaToZod(schema);
268
+
269
+ // Should apply default null for missing status
270
+ expect(zodSchema.parse([{ name: 'test1' }, { name: 'test2', status: 'active' }])).toEqual([
271
+ { name: 'test1', status: null },
272
+ { name: 'test2', status: 'active' },
273
+ ]);
274
+
275
+ // Should fail for missing required field
276
+ expect(() => zodSchema.parse([{ status: 'active' }])).toThrow();
277
+ });
278
+
279
+ it('should handle nested arrays', () => {
280
+ const schema: JsonSchema = {
281
+ type: 'array',
282
+ items: {
283
+ type: 'array',
284
+ items: { type: 'number' },
285
+ },
286
+ };
287
+ const zodSchema = jsonSchemaToZod(schema);
288
+
289
+ expect(
290
+ zodSchema.parse([
291
+ [1, 2],
292
+ [3, 4, 5],
293
+ ])
294
+ ).toEqual([
295
+ [1, 2],
296
+ [3, 4, 5],
297
+ ]);
298
+ expect(() => zodSchema.parse([['string', 2]])).toThrow();
299
+ });
300
});
301
302
describe('Enum Schemas', () => {
@@ -484,6 +577,51 @@ describe('jsonSchemaToZod', () => {
577
});
578
});
579
580
+ it('should handle default null values', () => {
581
+ const schema: JsonSchema = {
582
+ type: 'object',
583
+ properties: {
584
+ name: {
585
+ type: 'string',
586
+ description: 'The name field',
587
+ },
588
+ status: {
589
+ type: 'string',
590
+ default: null,
591
+ description: 'A field with default null',
592
+ },
593
+ age: {
594
+ type: 'number',
595
+ description: 'Age field without default',
596
+ },
597
+ },
598
+ required: ['name'],
599
+ };
600
+
601
+ const zodSchema = jsonSchemaToZod(schema);
602
+
603
+ // Test with required field only - should include null default
604
+ expect(zodSchema.parse({ name: 'John' })).toEqual({
605
+ name: 'John',
606
+ status: null,
607
+ });
608
+
609
+ // Test with status explicitly set to null
610
+ expect(zodSchema.parse({ name: 'John', status: null })).toEqual({
611
+ name: 'John',
612
+ status: null,
613
+ });
614
+
615
+ // Test with status set to a string value
616
+ expect(zodSchema.parse({ name: 'John', status: 'active' })).toEqual({
617
+ name: 'John',
618
+ status: 'active',
619
+ });
620
+
621
+ // Test that required field validation still works
622
+ expect(() => zodSchema.parse({})).toThrow();
623
+ });
624
+
625
it('should transform GetUserRequest schema correctly', () => {
626
const schema: JsonSchema = {
627
type: 'object',
@@ -513,7 +651,7 @@ describe('jsonSchemaToZod', () => {
651
});
652
653
// Test property description
516
- const shape = zodSchema._def.shape();
654
+ const shape = (zodSchema as any)._def.shape();
655
expect(shape.username.description).toBe('The username of the Hacker News user to retrieve.');
656
});
657
});