Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Fix: JSON Schema to Zod Parsing errors in default/arrays (#1761)

Musthaq Ahamad77e96e43dc5c
diff --git a/.changeset/soft-corners-tell.md b/.changeset/soft-corners-tell.md
new file mode 100644
index 000000000..84f3d2508
--- /dev/null
+++ b/.changeset/soft-corners-tell.md
@@ -0,0 +1,14 @@
+---
+'@composio/core': patch
+'@composio/json-schema-to-zod': patch
+'@composio/anthropic': patch
+'@composio/cloudflare': patch
+'@composio/google': patch
+'@composio/langchain': patch
+'@composio/mastra': patch
+'@composio/openai': patch
+'@composio/openai-agents': patch
+'@composio/vercel': patch
+---
+
+Fix JSON Schema to Zod Parsing
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fc4c0bfe9..789a9d6a2 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -9350,14 +9350,6 @@ snapshots:
chai: 5.2.0
tinyrainbow: 2.0.0
- dependencies:
- '@vitest/spy': 3.2.4
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
dependencies:
'@vitest/spy': 3.2.4
@@ -9366,14 +9358,6 @@ snapshots:
optionalDependencies:
- dependencies:
- '@vitest/spy': 3.2.4
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
'@vitest/[email protected]':
dependencies:
tinyrainbow: 2.0.0
@@ -9403,7 +9387,7 @@ snapshots:
sirv: 3.0.1
tinyglobby: 0.2.14
tinyrainbow: 2.0.0
'@vitest/[email protected]':
dependencies:
@@ -12234,7 +12218,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.4
'@vitest/pretty-format': 3.2.4
'@vitest/runner': 3.2.4
'@vitest/snapshot': 3.2.4
@@ -12318,7 +12302,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.4
'@vitest/pretty-format': 3.2.4
'@vitest/runner': 3.2.4
'@vitest/snapshot': 3.2.4
diff --git a/ts/packages/json-schema-to-zod/src/parsers/parse-array.ts b/ts/packages/json-schema-to-zod/src/parsers/parse-array.ts
index 564e40c07..ce69d8b0a 100644
--- a/ts/packages/json-schema-to-zod/src/parsers/parse-array.ts
+++ b/ts/packages/json-schema-to-zod/src/parsers/parse-array.ts
@@ -14,7 +14,7 @@ export const parseArray = (jsonSchema: JsonSchemaObject & { type: 'array' }, ref
// Handle anyOf pattern first
if (its.an.anyOf(jsonSchema)) {
const types = new Set<string>();
- let items: JsonSchema | undefined;
+ const itemsSchemas: JsonSchema[] = [];
// Collect all types and items from anyOf array
jsonSchema.anyOf.forEach(option => {
@@ -24,15 +24,25 @@ export const parseArray = (jsonSchema: JsonSchemaObject & { type: 'array' }, ref
if (typeof option === 'object' && option.items) {
const optionItems = option.items;
if (!Array.isArray(optionItems) && typeof optionItems === 'object') {
- items = optionItems;
+ itemsSchemas.push(optionItems);
}
}
});
+ // If we have multiple item schemas, create a union
+ let finalItems: JsonSchema | undefined;
+ if (itemsSchemas.length === 1) {
+ finalItems = itemsSchemas[0];
+ } else if (itemsSchemas.length > 1) {
+ finalItems = { anyOf: itemsSchemas };
+ }
+
// Create new schema with combined types
const newSchema: JsonSchemaObject = {
- type: Array.from(types) as JSONSchema7TypeName[],
- ...(items && { items }),
+ ...(types.size > 0
+ ? { type: Array.from(types) as JSONSchema7TypeName[] }
+ : { type: 'array' }),
+ ...(finalItems && { items: finalItems }),
};
// Copy over metadata fields
diff --git a/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts b/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
index 7590da8b8..2c83cb79e 100644
--- a/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
+++ b/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
@@ -38,7 +38,12 @@ function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object'
typeof propJsonSchema === 'object' &&
'default' in propJsonSchema
) {
- properties[key] = propZodSchema.optional().default(propJsonSchema.default);
+ // If default is null, make the field nullable with the null default
+ if (propJsonSchema.default === null) {
+ properties[key] = propZodSchema.nullable().optional().default(null);
+ } else {
+ properties[key] = propZodSchema.optional().default(propJsonSchema.default);
+ }
} else {
properties[key] = required ? propZodSchema : propZodSchema.optional();
}
diff --git a/ts/packages/json-schema-to-zod/test/json-to-zod.test.ts b/ts/packages/json-schema-to-zod/test/json-to-zod.test.ts
index 6bdc6bab6..99aae3afa 100644
--- a/ts/packages/json-schema-to-zod/test/json-to-zod.test.ts
+++ b/ts/packages/json-schema-to-zod/test/json-to-zod.test.ts
@@ -204,6 +204,99 @@ describe('jsonSchemaToZod', () => {
expect(() => zodSchema.parse([])).toThrow();
expect(() => zodSchema.parse(['one', 'two', 'three', 'four'])).toThrow();
});
+
+ it('should handle arrays with anyOf patterns', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ anyOf: [
+ {
+ type: 'array',
+ items: { type: 'string' },
+ },
+ {
+ type: 'array',
+ items: { type: 'number' },
+ },
+ ],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ // Should accept string arrays
+ expect(zodSchema.parse(['hello', 'world'])).toEqual(['hello', 'world']);
+
+ // Should accept number arrays
+ expect(zodSchema.parse([1, 2, 3])).toEqual([1, 2, 3]);
+
+ // Should accept mixed arrays (union of item types)
+ expect(zodSchema.parse(['hello', 123])).toEqual(['hello', 123]);
+ });
+
+ it('should handle arrays with anyOf at top level without explicit types', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ anyOf: [
+ {
+ items: { type: 'string' },
+ },
+ {
+ items: { type: 'number' },
+ },
+ ],
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ // Should accept string arrays
+ expect(zodSchema.parse(['hello', 'world'])).toEqual(['hello', 'world']);
+
+ // Should accept number arrays
+ expect(zodSchema.parse([1, 2, 3])).toEqual([1, 2, 3]);
+ });
+
+ it('should handle arrays with object items having default null', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ items: {
+ type: 'object',
+ properties: {
+ name: { type: 'string' },
+ status: { type: 'string', default: null },
+ },
+ required: ['name'],
+ },
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ // Should apply default null for missing status
+ expect(zodSchema.parse([{ name: 'test1' }, { name: 'test2', status: 'active' }])).toEqual([
+ { name: 'test1', status: null },
+ { name: 'test2', status: 'active' },
+ ]);
+
+ // Should fail for missing required field
+ expect(() => zodSchema.parse([{ status: 'active' }])).toThrow();
+ });
+
+ it('should handle nested arrays', () => {
+ const schema: JsonSchema = {
+ type: 'array',
+ items: {
+ type: 'array',
+ items: { type: 'number' },
+ },
+ };
+ const zodSchema = jsonSchemaToZod(schema);
+
+ expect(
+ zodSchema.parse([
+ [1, 2],
+ [3, 4, 5],
+ ])
+ ).toEqual([
+ [1, 2],
+ [3, 4, 5],
+ ]);
+ expect(() => zodSchema.parse([['string', 2]])).toThrow();
+ });
});
describe('Enum Schemas', () => {
@@ -484,6 +577,51 @@ describe('jsonSchemaToZod', () => {
});
});
+ it('should handle default null values', () => {
+ const schema: JsonSchema = {
+ type: 'object',
+ properties: {
+ name: {
+ type: 'string',
+ description: 'The name field',
+ },
+ status: {
+ type: 'string',
+ default: null,
+ description: 'A field with default null',
+ },
+ age: {
+ type: 'number',
+ description: 'Age field without default',
+ },
+ },
+ required: ['name'],
+ };
+
+ const zodSchema = jsonSchemaToZod(schema);
+
+ // Test with required field only - should include null default
+ expect(zodSchema.parse({ name: 'John' })).toEqual({
+ name: 'John',
+ status: null,
+ });
+
+ // Test with status explicitly set to null
+ expect(zodSchema.parse({ name: 'John', status: null })).toEqual({
+ name: 'John',
+ status: null,
+ });
+
+ // Test with status set to a string value
+ expect(zodSchema.parse({ name: 'John', status: 'active' })).toEqual({
+ name: 'John',
+ status: 'active',
+ });
+
+ // Test that required field validation still works
+ expect(() => zodSchema.parse({})).toThrow();
+ });
+
it('should transform GetUserRequest schema correctly', () => {
const schema: JsonSchema = {
type: 'object',
@@ -513,7 +651,7 @@ describe('jsonSchemaToZod', () => {
});
// Test property description
- const shape = zodSchema._def.shape();
+ const shape = (zodSchema as any)._def.shape();
expect(shape.username.description).toBe('The username of the Hacker News user to retrieve.');
});
});