@devries-cameron20 / composio Mirror · read-only / commits / 5e57815af
Fork 0

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 21, 16:00 UTC

fix(json-schema): accept and preserve dynamic object content across converters

Property-less objects (`{ type: "object" }`, `properties: {}`) were being collapsed to a strict empty object by the Zod converter and given an implicit `additionalProperties: false` by the Effect converter, so valid free-form payloads such as METABASE_POST_API_CARD.dataset_query were rejected at the CLI boundary. `ToolSchema.parse` separately dropped root `patternProperties` and rejected a schema-valued root `additionalProperties`. Dynamic keys are now routed to exactly the schemas that apply to them, and the shared acceptance contract lives in one checked-in corpus with byte-identical TypeScript and Python copies.

jkomyno committed Aug 6, 2026 at 20:33 UTC 5e57815af16ef70758a5b4cf9909c59f44538cfd
19 files changed +1539 -96
.changeset/fix-property-less-object-conversion.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/json-schema-to-zod': patch
3 +---
4 +
5 +Accept and preserve arbitrary content in property-less objects (`{ type: "object" }` or `properties: {}`) at root, nested, and array-item positions, instead of collapsing them to a strict empty object. Dynamic keys are also routed correctly: every matching `patternProperties` entry validates its key, and a schema-valued `additionalProperties` applies only to keys no property or pattern claims.
.changeset/preserve-root-dynamic-key-constraints.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Preserve root `patternProperties` and boolean- or schema-valued `additionalProperties` when parsing tool parameters. `ToolSchema.parse` previously stripped `patternProperties` as an unknown key and rejected a schema-valued `additionalProperties` outright, so both dynamic-key constraints were lost before any converter could see them.
.github/workflows/py.test.yml
+2
@@ -5,6 +5,7 @@ on:
5 branches: [master, next]
6 paths:
7 - 'python/**/*.py'
8 + - 'python/**/*.json'
9 - '.github/workflows/py.test.yml'
10 - '.github/actions/setup-python-uv/action.yml'
11 - 'mise.toml'
@@ -16,6 +17,7 @@ on:
17 branches: [master, next]
18 paths:
19 - 'python/**/*.py'
20 + - 'python/**/*.json'
21 - '.github/workflows/py.test.yml'
22 - '.github/actions/setup-python-uv/action.yml'
23 - 'mise.toml'
python/tests/conftest.py
+18
@@ -30,6 +30,24 @@ def get_ts_fixtures_dir() -> Path:
30 )
31
32
33 +def get_py_json_schema_corpus_dir() -> Path:
34 + """Get the Python JSON Schema conversion corpus directory path."""
35 + return Path(__file__).parent / "fixtures" / "json-schema-conversion"
36 +
37 +
38 +def get_ts_json_schema_corpus_dir() -> Path:
39 + """Get the TypeScript JSON Schema conversion corpus directory path."""
40 + return (
41 + Path(__file__).parent.parent.parent
42 + / "ts"
43 + / "packages"
44 + / "core"
45 + / "test"
46 + / "fixtures"
47 + / "json-schema-conversion"
48 + )
49 +
50 +
51 def compute_signature(
52 webhook_id: str, timestamp: str, payload: str, secret: str
53 ) -> str:
python/tests/fixtures/json-schema-conversion/object-cases.json new
+422
@@ -0,0 +1,422 @@
1 +{
2 + "cases": [
3 + {
4 + "id": "nested-free-form-object",
5 + "description": "A nested `{ type: \"object\" }` property must accept and preserve arbitrary content (issue #4064, METABASE_POST_API_CARD.dataset_query).",
6 + "schema": {
7 + "type": "object",
8 + "properties": { "dataset_query": { "type": "object" } }
9 + },
10 + "ingress": {
11 + "accepted": true,
12 + "preserved": {
13 + "type": "object",
14 + "properties": { "dataset_query": { "type": "object" } }
15 + }
16 + },
17 + "instances": [
18 + {
19 + "input": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } },
20 + "accepted": true,
21 + "zod": {
22 + "output": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } }
23 + },
24 + "effect": {
25 + "output": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } }
26 + },
27 + "python": {
28 + "output": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } }
29 + }
30 + }
31 + ]
32 + },
33 + {
34 + "id": "root-free-form-absent-properties",
35 + "description": "A root object with no `properties` member and omitted `additionalProperties` is open.",
36 + "schema": { "type": "object" },
37 + "instances": [
38 + {
39 + "input": { "anything": { "a": 1 }, "other": "x" },
40 + "accepted": true,
41 + "zod": { "output": { "anything": { "a": 1 }, "other": "x" } },
42 + "effect": { "output": { "anything": { "a": 1 }, "other": "x" } },
43 + "python": { "output": { "anything": { "a": 1 }, "other": "x" } }
44 + }
45 + ]
46 + },
47 + {
48 + "id": "root-free-form-empty-properties",
49 + "description": "`properties: {}` is property-less too, and stays open when `additionalProperties` is omitted.",
50 + "schema": { "type": "object", "properties": {} },
51 + "ingress": {
52 + "accepted": true,
53 + "preserved": { "type": "object", "properties": {} }
54 + },
55 + "instances": [
56 + {
57 + "input": { "foo": 1, "bar": { "baz": true } },
58 + "accepted": true,
59 + "zod": { "output": { "foo": 1, "bar": { "baz": true } } },
60 + "effect": { "output": { "foo": 1, "bar": { "baz": true } } },
61 + "python": { "output": { "foo": 1, "bar": { "baz": true } } }
62 + }
63 + ]
64 + },
65 + {
66 + "id": "free-form-object-in-array-items",
67 + "description": "Property-less objects placed as array items preserve arbitrary content.",
68 + "schema": {
69 + "type": "object",
70 + "properties": {
71 + "records": { "type": "array", "items": { "type": "object" } }
72 + }
73 + },
74 + "ingress": {
75 + "accepted": true,
76 + "preserved": {
77 + "type": "object",
78 + "properties": {
79 + "records": { "type": "array", "items": { "type": "object" } }
80 + }
81 + }
82 + },
83 + "instances": [
84 + {
85 + "input": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] },
86 + "accepted": true,
87 + "zod": { "output": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] } },
88 + "effect": { "output": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] } },
89 + "python": { "output": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] } }
90 + }
91 + ]
92 + },
93 + {
94 + "id": "explicit-additional-properties-false",
95 + "description": "An explicit `additionalProperties: false` closes an otherwise property-less object.",
96 + "schema": { "type": "object", "properties": {}, "additionalProperties": false },
97 + "ingress": {
98 + "accepted": true,
99 + "preserved": { "type": "object", "properties": {}, "additionalProperties": false }
100 + },
101 + "instances": [
102 + {
103 + "input": {},
104 + "accepted": true,
105 + "zod": { "output": {} },
106 + "effect": { "output": {} },
107 + "python": { "output": {} }
108 + },
109 + {
110 + "input": { "foo": 1 },
111 + "accepted": false
112 + }
113 + ]
114 + },
115 + {
116 + "id": "explicit-additional-properties-true",
117 + "description": "An explicit `additionalProperties: true` preserves unknown keys alongside named properties.",
118 + "schema": {
119 + "type": "object",
120 + "properties": { "name": { "type": "string" } },
121 + "additionalProperties": true
122 + },
123 + "ingress": {
124 + "accepted": true,
125 + "preserved": {
126 + "type": "object",
127 + "properties": { "name": { "type": "string" } },
128 + "additionalProperties": true
129 + }
130 + },
131 + "instances": [
132 + {
133 + "input": { "name": "a", "extra": 1 },
134 + "accepted": true,
135 + "zod": { "output": { "name": "a", "extra": 1 } },
136 + "effect": { "output": { "name": "a", "extra": 1 } },
137 + "python": { "output": { "name": "a", "extra": 1 } }
138 + }
139 + ]
140 + },
141 + {
142 + "id": "named-properties-strict-by-default",
143 + "description": "Composio divergence: named `properties` with omitted `additionalProperties` stay strict for tool inputs, while JSON Schema itself would allow unknown keys.",
144 + "divergesFromJsonSchema": "JSON Schema treats omitted `additionalProperties` as permissive; Composio tool inputs keep it strict.",
145 + "schema": {
146 + "type": "object",
147 + "properties": { "name": { "type": "string" } },
148 + "required": ["name"]
149 + },
150 + "ingress": {
151 + "accepted": true,
152 + "preserved": {
153 + "type": "object",
154 + "properties": { "name": { "type": "string" } },
155 + "required": ["name"]
156 + }
157 + },
158 + "instances": [
159 + {
160 + "input": { "name": "a" },
161 + "accepted": true,
162 + "zod": { "output": { "name": "a" } },
163 + "effect": { "output": { "name": "a" } },
164 + "python": { "output": { "name": "a" } }
165 + },
166 + {
167 + "input": { "name": "a", "extra": 1 },
168 + "accepted": false
169 + }
170 + ]
171 + },
172 + {
173 + "id": "named-properties-with-pattern-properties",
174 + "description": "With named `properties` plus `patternProperties` and omitted `additionalProperties`, pattern-matching keys are admitted and validated while other unknown keys stay rejected.",
175 + "schema": {
176 + "type": "object",
177 + "properties": { "name": { "type": "string" } },
178 + "patternProperties": { "^x-": { "type": "number" } }
179 + },
180 + "ingress": {
181 + "accepted": true,
182 + "preserved": {
183 + "type": "object",
184 + "properties": { "name": { "type": "string" } },
185 + "patternProperties": { "^x-": { "type": "number" } }
186 + }
187 + },
188 + "instances": [
189 + {
190 + "input": { "name": "a", "x-count": 3 },
191 + "accepted": true,
192 + "zod": { "output": { "name": "a", "x-count": 3 } },
193 + "effect": { "output": { "name": "a", "x-count": 3 } },
194 + "python": { "output": { "name": "a", "x-count": 3 } }
195 + },
196 + {
197 + "input": { "name": "a", "x-count": "nope" },
198 + "accepted": false
199 + },
200 + {
201 + "input": { "name": "a", "other": 1 },
202 + "accepted": false
203 + }
204 + ]
205 + },
206 + {
207 + "id": "root-additional-properties-schema-valued",
208 + "description": "A schema-valued root `additionalProperties` survives `ToolSchema.parse` and validates unknown keys.",
209 + "schema": {
210 + "type": "object",
211 + "properties": { "name": { "type": "string" } },
212 + "additionalProperties": { "type": "number" }
213 + },
214 + "ingress": {
215 + "accepted": true,
216 + "preserved": {
217 + "type": "object",
218 + "properties": { "name": { "type": "string" } },
219 + "additionalProperties": { "type": "number" }
220 + }
221 + },
222 + "instances": [
223 + {
224 + "input": { "name": "a", "count": 2 },
225 + "accepted": true,
226 + "zod": { "output": { "name": "a", "count": 2 } },
227 + "effect": { "output": { "name": "a", "count": 2 } },
228 + "python": { "output": { "name": "a", "count": 2 } }
229 + },
230 + {
231 + "input": { "name": "a", "count": "x" },
232 + "accepted": false
233 + }
234 + ]
235 + },
236 + {
237 + "id": "pattern-only-object",
238 + "description": "A pattern-only object with no named `properties` accepts unmatched keys while still validating matched ones.",
239 + "schema": {
240 + "type": "object",
241 + "patternProperties": { "^s_": { "type": "string" } }
242 + },
243 + "instances": [
244 + {
245 + "input": { "s_a": "x", "other": 1 },
246 + "accepted": true,
247 + "zod": { "output": { "s_a": "x", "other": 1 } },
248 + "effect": { "output": { "s_a": "x", "other": 1 } },
249 + "python": { "output": { "s_a": "x", "other": 1 } }
250 + },
251 + {
252 + "input": { "s_a": 5 },
253 + "accepted": false
254 + }
255 + ]
256 + },
257 + {
258 + "id": "multiple-matching-patterns",
259 + "description": "A key matching several non-transforming patterns must satisfy all of them.",
260 + "schema": {
261 + "type": "object",
262 + "patternProperties": {
263 + "^a": { "type": "string" },
264 + "b$": { "type": "string", "minLength": 2 }
265 + }
266 + },
267 + "instances": [
268 + {
269 + "input": { "ab": "xy" },
270 + "accepted": true,
271 + "zod": { "output": { "ab": "xy" } },
272 + "effect": { "output": { "ab": "xy" } },
273 + "python": { "output": { "ab": "xy" } }
274 + },
275 + {
276 + "input": { "ab": "x" },
277 + "accepted": false
278 + },
279 + {
280 + "input": { "ab": 5 },
281 + "accepted": false
282 + }
283 + ]
284 + },
285 + {
286 + "id": "declared-property-also-matching-a-pattern",
287 + "description": "A key declared in `properties` is still checked against every matching pattern.",
288 + "schema": {
289 + "type": "object",
290 + "properties": { "x_id": { "type": "string" } },
291 + "patternProperties": { "^x_": { "type": "string", "minLength": 3 } }
292 + },
293 + "ingress": {
294 + "accepted": true,
295 + "preserved": {
296 + "type": "object",
297 + "properties": { "x_id": { "type": "string" } },
298 + "patternProperties": { "^x_": { "type": "string", "minLength": 3 } }
299 + }
300 + },
301 + "instances": [
302 + {
303 + "input": { "x_id": "abc" },
304 + "accepted": true,
305 + "zod": { "output": { "x_id": "abc" } },
306 + "effect": { "output": { "x_id": "abc" } },
307 + "python": { "output": { "x_id": "abc" } }
308 + },
309 + {
310 + "input": { "x_id": "ab" },
311 + "accepted": false
312 + }
313 + ]
314 + },
315 + {
316 + "id": "schema-valued-additional-applies-only-to-unmatched-keys",
317 + "description": "Schema-valued `additionalProperties` must not rescue a key that a `patternProperties` entry already claims and rejects.",
318 + "schema": {
319 + "type": "object",
320 + "properties": { "name": { "type": "string" } },
321 + "patternProperties": { "^n_": { "type": "number" } },
322 + "additionalProperties": { "type": "boolean" }
323 + },
324 + "ingress": {
325 + "accepted": true,
326 + "preserved": {
327 + "type": "object",
328 + "properties": { "name": { "type": "string" } },
329 + "patternProperties": { "^n_": { "type": "number" } },
330 + "additionalProperties": { "type": "boolean" }
331 + }
332 + },
333 + "instances": [
334 + {
335 + "input": { "name": "a", "n_x": 1, "flag": true },
336 + "accepted": true,
337 + "zod": { "output": { "name": "a", "n_x": 1, "flag": true } },
338 + "effect": { "output": { "name": "a", "n_x": 1, "flag": true } },
339 + "python": { "output": { "name": "a", "n_x": 1, "flag": true } }
340 + },
341 + {
342 + "input": { "name": "a", "n_x": true },
343 + "accepted": false
344 + },
345 + {
346 + "input": { "name": "a", "flag": 1 },
347 + "accepted": false
348 + }
349 + ]
350 + },
351 + {
352 + "id": "pattern-property-default-materialization",
353 + "description": "Composio divergence: Zod and Pydantic materialize `default` inside a matching pattern schema, while Effect decoding is validation-only and returns the input unchanged.",
354 + "divergesFromJsonSchema": "`default` is annotation-only in JSON Schema; the Zod and Pydantic converters materialize it.",
355 + "schema": {
356 + "type": "object",
357 + "properties": {},
358 + "patternProperties": {
359 + "^p_": {
360 + "type": "object",
361 + "properties": { "mode": { "type": "string", "default": "auto" } }
362 + }
363 + }
364 + },
365 + "ingress": {
366 + "accepted": true,
367 + "preserved": {
368 + "type": "object",
369 + "properties": {},
370 + "patternProperties": {
371 + "^p_": {
372 + "type": "object",
373 + "properties": { "mode": { "type": "string", "default": "auto" } }
374 + }
375 + }
376 + }
377 + },
378 + "instances": [
379 + {
380 + "input": { "p_a": {} },
381 + "accepted": true,
382 + "zod": { "output": { "p_a": { "mode": "auto" } } },
383 + "effect": { "output": { "p_a": {} } },
384 + "python": { "output": { "p_a": { "mode": "auto" } } }
385 + }
386 + ]
387 + },
388 + {
389 + "id": "additional-property-default-materialization",
390 + "description": "Composio divergence: a schema-valued `additionalProperties` also materializes defaults in Zod and Pydantic while Effect leaves the input unchanged.",
391 + "divergesFromJsonSchema": "`default` is annotation-only in JSON Schema; the Zod and Pydantic converters materialize it.",
392 + "schema": {
393 + "type": "object",
394 + "properties": { "name": { "type": "string" } },
395 + "additionalProperties": {
396 + "type": "object",
397 + "properties": { "enabled": { "type": "boolean", "default": false } }
398 + }
399 + },
400 + "ingress": {
401 + "accepted": true,
402 + "preserved": {
403 + "type": "object",
404 + "properties": { "name": { "type": "string" } },
405 + "additionalProperties": {
406 + "type": "object",
407 + "properties": { "enabled": { "type": "boolean", "default": false } }
408 + }
409 + }
410 + },
411 + "instances": [
412 + {
413 + "input": { "name": "a", "extra": {} },
414 + "accepted": true,
415 + "zod": { "output": { "name": "a", "extra": { "enabled": false } } },
416 + "effect": { "output": { "name": "a", "extra": {} } },
417 + "python": { "output": { "name": "a", "extra": { "enabled": false } } }
418 + }
419 + ]
420 + }
421 + ]
422 +}
python/tests/test_cross_sdk_compatibility.py
+16 -1
@@ -9,7 +9,12 @@ import json
9
10 import pytest
11
12 -from tests.conftest import get_py_fixtures_dir, get_ts_fixtures_dir
12 +from tests.conftest import (
13 + get_py_fixtures_dir,
14 + get_py_json_schema_corpus_dir,
15 + get_ts_fixtures_dir,
16 + get_ts_json_schema_corpus_dir,
17 +)
18
19
20 class TestCrossSDKFixtureCompatibility:
@@ -38,3 +43,13 @@ class TestCrossSDKFixtureCompatibility:
43 f"Fixture {fixture_name} differs between TypeScript and Python SDKs. "
44 "Ensure fixtures are kept in sync."
45 )
46 +
47 + def test_json_schema_conversion_corpus_is_identical(self) -> None:
48 + """The shared JSON Schema conversion corpus has one copy per language."""
49 + ts_path = get_ts_json_schema_corpus_dir() / "object-cases.json"
50 + py_path = get_py_json_schema_corpus_dir() / "object-cases.json"
51 +
52 + assert ts_path.read_bytes() == py_path.read_bytes(), (
53 + "object-cases.json differs between the TypeScript and Python SDKs. "
54 + "Both copies must stay byte-identical."
55 + )
ts/packages/cli/test/fixtures/corpus.ts new
+85
@@ -0,0 +1,85 @@
1 +/**
2 + * Loader for the shared cross-SDK JSON Schema conversion corpus.
3 + *
4 + * The corpus is a single logical fixture with one byte-identical copy per
5 + * language (`ts/packages/core/test/fixtures/json-schema-conversion/` and
6 + * `python/tests/fixtures/json-schema-conversion/`), following the same
7 + * convention as the webhook fixtures. Every consumer resolves its own copy
8 + * module-relatively — no working-directory or repository-root assumptions.
9 + */
10 +import * as z from 'zod';
11 +
12 +import objectCases from '../../../core/test/fixtures/json-schema-conversion/object-cases.json';
13 +
14 +const JsonObject = z.record(z.string(), z.unknown());
15 +
16 +const LanguageExpectation = z.object({
17 + output: z.unknown().optional(),
18 + accepted: z.boolean().optional(),
19 +});
20 +
21 +const CorpusInstance = z.object({
22 + input: JsonObject,
23 + accepted: z.boolean(),
24 + zod: LanguageExpectation.optional(),
25 + effect: LanguageExpectation.optional(),
26 + python: LanguageExpectation.optional(),
27 + divergence: z.object({ reason: z.string().min(1) }).optional(),
28 +});
29 +
30 +const CorpusCase = z.object({
31 + id: z.string().min(1),
32 + description: z.string().optional(),
33 + divergesFromJsonSchema: z.string().optional(),
34 + schema: JsonObject,
35 + ingress: z.object({ accepted: z.boolean(), preserved: JsonObject }).optional(),
36 + instances: z.array(CorpusInstance).min(1),
37 +});
38 +
39 +const Corpus = z.object({ cases: z.array(CorpusCase).min(1) });
40 +
41 +export type CorpusInstance = z.infer<typeof CorpusInstance>;
42 +export type CorpusCase = z.infer<typeof CorpusCase>;
43 +export type CorpusLanguage = 'zod' | 'effect' | 'python';
44 +
45 +/**
46 + * Enforce the invariants that keep the corpus a shared contract rather than a
47 + * per-language wish list: ids are unique, and a language may only disagree about
48 + * acceptance when the instance declares why.
49 + */
50 +export const assertCorpusInvariants = (cases: ReadonlyArray<CorpusCase>): void => {
51 + const seen = new Set<string>();
52 + for (const testCase of cases) {
53 + if (seen.has(testCase.id)) {
54 + throw new Error(`Duplicate corpus case id: ${testCase.id}`);
55 + }
56 + seen.add(testCase.id);
57 +
58 + for (const [index, instance] of testCase.instances.entries()) {
59 + for (const language of ['zod', 'effect', 'python'] as const) {
60 + const override = instance[language]?.accepted;
61 + if (override !== undefined && override !== instance.accepted && !instance.divergence) {
62 + throw new Error(
63 + `Corpus case ${testCase.id} instance ${index} overrides ${language} acceptance without a divergence reason`
64 + );
65 + }
66 + }
67 + }
68 + }
69 +};
70 +
71 +let cached: ReadonlyArray<CorpusCase> | undefined;
72 +
73 +/** Decode the shared corpus and enforce its cross-language invariants. */
74 +export const loadObjectCases = (): ReadonlyArray<CorpusCase> => {
75 + if (!cached) {
76 + const { cases } = Corpus.parse(objectCases);
77 + assertCorpusInvariants(cases);
78 + cached = cases;
79 + }
80 + return cached;
81 +};
82 +
83 +/** Acceptance for one language: the shared flag unless a declared divergence overrides it. */
84 +export const acceptedFor = (instance: CorpusInstance, language: CorpusLanguage): boolean =>
85 + instance[language]?.accepted ?? instance.accepted;
ts/packages/cli/test/src/services/tool-input-validation.test.ts
+37
@@ -10,6 +10,7 @@ import {
10 validateToolInputArgumentsWithDefinition,
11 } from 'src/services/tool-input-validation';
12 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
13 +import { loadObjectCases } from '../../fixtures/corpus';
14
15 const definition = {
16 schemaPath: '/tmp/GMAIL_SEND_EMAIL.json',
@@ -162,3 +163,39 @@ describe('tool input validation', () => {
163 })
164 );
165 });
166 +
167 +describe('free-form object tool inputs', () => {
168 + const freeFormCase = loadObjectCases().find(entry => entry.id === 'nested-free-form-object');
169 + if (!freeFormCase) {
170 + throw new Error('Corpus is missing the nested-free-form-object case');
171 + }
172 +
173 + const freeFormDefinition = {
174 + schemaPath: '/tmp/METABASE_POST_API_CARD.json',
175 + schema: freeFormCase.schema,
176 + } as const;
177 +
178 + it.effect('accepts arbitrary content inside a property-less object', () =>
179 + Effect.gen(function* () {
180 + const result = yield* validateToolInputArgumentsWithDefinition(
181 + 'METABASE_POST_API_CARD',
182 + freeFormCase.instances[0].input,
183 + freeFormDefinition
184 + );
185 +
186 + expect(result).toEqual(freeFormDefinition);
187 + })
188 + );
189 +
190 + it.effect('still reports unknown keys for named-property schemas', () =>
191 + Effect.gen(function* () {
192 + const error = yield* validateToolInputArgumentsWithDefinition(
193 + 'GMAIL_SEND_EMAIL',
194 + { recipient_email: 'karan@composio.dev', nope: 1 },
195 + definition
196 + ).pipe(Effect.flip);
197 +
198 + expect(error).toBeInstanceOf(ToolInputValidationError);
199 + })
200 + );
201 +});
ts/packages/core/src/types/tool.types.ts
+7 -1
@@ -145,7 +145,13 @@ const ParametersSchema = z.preprocess(
145 default: z.unknown().optional(),
146 nullable: z.boolean().optional(),
147 description: z.string().optional(),
148 - additionalProperties: z.boolean().default(false).optional(),
148 + // Dynamic-key constraints the Composio API ships at the parameters root.
149 + // Both were previously lost on parse: `patternProperties` was stripped as an
150 + // unknown key, and a schema-valued `additionalProperties` was rejected
151 + // outright by the boolean-only schema. Omission still stays omission —
152 + // downstream converters, not the parser, decide the implicit policy.
153 + patternProperties: z.record(z.string(), JSONSchemaPropertySchema).optional(),
154 + additionalProperties: z.union([z.boolean(), JSONSchemaPropertySchema]).optional(),
155 // Definition blocks targeted by `$ref` pointers. The Composio API ships
156 // these at the parameters root (e.g. `data` → `$ref` → `#/$defs/...`), but
157 // because `z.object` strips unknown keys they were being dropped on parse —
ts/packages/core/test/fixtures/json-schema-conversion/corpus.ts new
+85
@@ -0,0 +1,85 @@
1 +/**
2 + * Loader for the shared cross-SDK JSON Schema conversion corpus.
3 + *
4 + * The corpus is a single logical fixture with one byte-identical copy per
5 + * language (`ts/packages/core/test/fixtures/json-schema-conversion/` and
6 + * `python/tests/fixtures/json-schema-conversion/`), following the same
7 + * convention as the webhook fixtures. Every consumer resolves its own copy
8 + * module-relatively — no working-directory or repository-root assumptions.
9 + */
10 +import * as z from 'zod';
11 +
12 +import objectCases from './object-cases.json';
13 +
14 +const JsonObject = z.record(z.string(), z.unknown());
15 +
16 +const LanguageExpectation = z.object({
17 + output: z.unknown().optional(),
18 + accepted: z.boolean().optional(),
19 +});
20 +
21 +const CorpusInstance = z.object({
22 + input: JsonObject,
23 + accepted: z.boolean(),
24 + zod: LanguageExpectation.optional(),
25 + effect: LanguageExpectation.optional(),
26 + python: LanguageExpectation.optional(),
27 + divergence: z.object({ reason: z.string().min(1) }).optional(),
28 +});
29 +
30 +const CorpusCase = z.object({
31 + id: z.string().min(1),
32 + description: z.string().optional(),
33 + divergesFromJsonSchema: z.string().optional(),
34 + schema: JsonObject,
35 + ingress: z.object({ accepted: z.boolean(), preserved: JsonObject }).optional(),
36 + instances: z.array(CorpusInstance).min(1),
37 +});
38 +
39 +const Corpus = z.object({ cases: z.array(CorpusCase).min(1) });
40 +
41 +export type CorpusInstance = z.infer<typeof CorpusInstance>;
42 +export type CorpusCase = z.infer<typeof CorpusCase>;
43 +export type CorpusLanguage = 'zod' | 'effect' | 'python';
44 +
45 +/**
46 + * Enforce the invariants that keep the corpus a shared contract rather than a
47 + * per-language wish list: ids are unique, and a language may only disagree about
48 + * acceptance when the instance declares why.
49 + */
50 +export const assertCorpusInvariants = (cases: ReadonlyArray<CorpusCase>): void => {
51 + const seen = new Set<string>();
52 + for (const testCase of cases) {
53 + if (seen.has(testCase.id)) {
54 + throw new Error(`Duplicate corpus case id: ${testCase.id}`);
55 + }
56 + seen.add(testCase.id);
57 +
58 + for (const [index, instance] of testCase.instances.entries()) {
59 + for (const language of ['zod', 'effect', 'python'] as const) {
60 + const override = instance[language]?.accepted;
61 + if (override !== undefined && override !== instance.accepted && !instance.divergence) {
62 + throw new Error(
63 + `Corpus case ${testCase.id} instance ${index} overrides ${language} acceptance without a divergence reason`
64 + );
65 + }
66 + }
67 + }
68 + }
69 +};
70 +
71 +let cached: ReadonlyArray<CorpusCase> | undefined;
72 +
73 +/** Decode the shared corpus and enforce its cross-language invariants. */
74 +export const loadObjectCases = (): ReadonlyArray<CorpusCase> => {
75 + if (!cached) {
76 + const { cases } = Corpus.parse(objectCases);
77 + assertCorpusInvariants(cases);
78 + cached = cases;
79 + }
80 + return cached;
81 +};
82 +
83 +/** Acceptance for one language: the shared flag unless a declared divergence overrides it. */
84 +export const acceptedFor = (instance: CorpusInstance, language: CorpusLanguage): boolean =>
85 + instance[language]?.accepted ?? instance.accepted;
ts/packages/core/test/fixtures/json-schema-conversion/object-cases.json new
+422
@@ -0,0 +1,422 @@
1 +{
2 + "cases": [
3 + {
4 + "id": "nested-free-form-object",
5 + "description": "A nested `{ type: \"object\" }` property must accept and preserve arbitrary content (issue #4064, METABASE_POST_API_CARD.dataset_query).",
6 + "schema": {
7 + "type": "object",
8 + "properties": { "dataset_query": { "type": "object" } }
9 + },
10 + "ingress": {
11 + "accepted": true,
12 + "preserved": {
13 + "type": "object",
14 + "properties": { "dataset_query": { "type": "object" } }
15 + }
16 + },
17 + "instances": [
18 + {
19 + "input": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } },
20 + "accepted": true,
21 + "zod": {
22 + "output": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } }
23 + },
24 + "effect": {
25 + "output": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } }
26 + },
27 + "python": {
28 + "output": { "dataset_query": { "database": 1, "query": { "source-table": 2 } } }
29 + }
30 + }
31 + ]
32 + },
33 + {
34 + "id": "root-free-form-absent-properties",
35 + "description": "A root object with no `properties` member and omitted `additionalProperties` is open.",
36 + "schema": { "type": "object" },
37 + "instances": [
38 + {
39 + "input": { "anything": { "a": 1 }, "other": "x" },
40 + "accepted": true,
41 + "zod": { "output": { "anything": { "a": 1 }, "other": "x" } },
42 + "effect": { "output": { "anything": { "a": 1 }, "other": "x" } },
43 + "python": { "output": { "anything": { "a": 1 }, "other": "x" } }
44 + }
45 + ]
46 + },
47 + {
48 + "id": "root-free-form-empty-properties",
49 + "description": "`properties: {}` is property-less too, and stays open when `additionalProperties` is omitted.",
50 + "schema": { "type": "object", "properties": {} },
51 + "ingress": {
52 + "accepted": true,
53 + "preserved": { "type": "object", "properties": {} }
54 + },
55 + "instances": [
56 + {
57 + "input": { "foo": 1, "bar": { "baz": true } },
58 + "accepted": true,
59 + "zod": { "output": { "foo": 1, "bar": { "baz": true } } },
60 + "effect": { "output": { "foo": 1, "bar": { "baz": true } } },
61 + "python": { "output": { "foo": 1, "bar": { "baz": true } } }
62 + }
63 + ]
64 + },
65 + {
66 + "id": "free-form-object-in-array-items",
67 + "description": "Property-less objects placed as array items preserve arbitrary content.",
68 + "schema": {
69 + "type": "object",
70 + "properties": {
71 + "records": { "type": "array", "items": { "type": "object" } }
72 + }
73 + },
74 + "ingress": {
75 + "accepted": true,
76 + "preserved": {
77 + "type": "object",
78 + "properties": {
79 + "records": { "type": "array", "items": { "type": "object" } }
80 + }
81 + }
82 + },
83 + "instances": [
84 + {
85 + "input": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] },
86 + "accepted": true,
87 + "zod": { "output": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] } },
88 + "effect": { "output": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] } },
89 + "python": { "output": { "records": [{ "a": 1 }, { "b": { "c": 2 } }] } }
90 + }
91 + ]
92 + },
93 + {
94 + "id": "explicit-additional-properties-false",
95 + "description": "An explicit `additionalProperties: false` closes an otherwise property-less object.",
96 + "schema": { "type": "object", "properties": {}, "additionalProperties": false },
97 + "ingress": {
98 + "accepted": true,
99 + "preserved": { "type": "object", "properties": {}, "additionalProperties": false }
100 + },
101 + "instances": [
102 + {
103 + "input": {},
104 + "accepted": true,
105 + "zod": { "output": {} },
106 + "effect": { "output": {} },
107 + "python": { "output": {} }
108 + },
109 + {
110 + "input": { "foo": 1 },
111 + "accepted": false
112 + }
113 + ]
114 + },
115 + {
116 + "id": "explicit-additional-properties-true",
117 + "description": "An explicit `additionalProperties: true` preserves unknown keys alongside named properties.",
118 + "schema": {
119 + "type": "object",
120 + "properties": { "name": { "type": "string" } },
121 + "additionalProperties": true
122 + },
123 + "ingress": {
124 + "accepted": true,
125 + "preserved": {
126 + "type": "object",
127 + "properties": { "name": { "type": "string" } },
128 + "additionalProperties": true
129 + }
130 + },
131 + "instances": [
132 + {
133 + "input": { "name": "a", "extra": 1 },
134 + "accepted": true,
135 + "zod": { "output": { "name": "a", "extra": 1 } },
136 + "effect": { "output": { "name": "a", "extra": 1 } },
137 + "python": { "output": { "name": "a", "extra": 1 } }
138 + }
139 + ]
140 + },
141 + {
142 + "id": "named-properties-strict-by-default",
143 + "description": "Composio divergence: named `properties` with omitted `additionalProperties` stay strict for tool inputs, while JSON Schema itself would allow unknown keys.",
144 + "divergesFromJsonSchema": "JSON Schema treats omitted `additionalProperties` as permissive; Composio tool inputs keep it strict.",
145 + "schema": {
146 + "type": "object",
147 + "properties": { "name": { "type": "string" } },
148 + "required": ["name"]
149 + },
150 + "ingress": {
151 + "accepted": true,
152 + "preserved": {
153 + "type": "object",
154 + "properties": { "name": { "type": "string" } },
155 + "required": ["name"]
156 + }
157 + },
158 + "instances": [
159 + {
160 + "input": { "name": "a" },
161 + "accepted": true,
162 + "zod": { "output": { "name": "a" } },
163 + "effect": { "output": { "name": "a" } },
164 + "python": { "output": { "name": "a" } }
165 + },
166 + {
167 + "input": { "name": "a", "extra": 1 },
168 + "accepted": false
169 + }
170 + ]
171 + },
172 + {
173 + "id": "named-properties-with-pattern-properties",
174 + "description": "With named `properties` plus `patternProperties` and omitted `additionalProperties`, pattern-matching keys are admitted and validated while other unknown keys stay rejected.",
175 + "schema": {
176 + "type": "object",
177 + "properties": { "name": { "type": "string" } },
178 + "patternProperties": { "^x-": { "type": "number" } }
179 + },
180 + "ingress": {
181 + "accepted": true,
182 + "preserved": {
183 + "type": "object",
184 + "properties": { "name": { "type": "string" } },
185 + "patternProperties": { "^x-": { "type": "number" } }
186 + }
187 + },
188 + "instances": [
189 + {
190 + "input": { "name": "a", "x-count": 3 },
191 + "accepted": true,
192 + "zod": { "output": { "name": "a", "x-count": 3 } },
193 + "effect": { "output": { "name": "a", "x-count": 3 } },
194 + "python": { "output": { "name": "a", "x-count": 3 } }
195 + },
196 + {
197 + "input": { "name": "a", "x-count": "nope" },
198 + "accepted": false
199 + },
200 + {
201 + "input": { "name": "a", "other": 1 },
202 + "accepted": false
203 + }
204 + ]
205 + },
206 + {
207 + "id": "root-additional-properties-schema-valued",
208 + "description": "A schema-valued root `additionalProperties` survives `ToolSchema.parse` and validates unknown keys.",
209 + "schema": {
210 + "type": "object",
211 + "properties": { "name": { "type": "string" } },
212 + "additionalProperties": { "type": "number" }
213 + },
214 + "ingress": {
215 + "accepted": true,
216 + "preserved": {
217 + "type": "object",
218 + "properties": { "name": { "type": "string" } },
219 + "additionalProperties": { "type": "number" }
220 + }
221 + },
222 + "instances": [
223 + {
224 + "input": { "name": "a", "count": 2 },
225 + "accepted": true,
226 + "zod": { "output": { "name": "a", "count": 2 } },
227 + "effect": { "output": { "name": "a", "count": 2 } },
228 + "python": { "output": { "name": "a", "count": 2 } }
229 + },
230 + {
231 + "input": { "name": "a", "count": "x" },
232 + "accepted": false
233 + }
234 + ]
235 + },
236 + {
237 + "id": "pattern-only-object",
238 + "description": "A pattern-only object with no named `properties` accepts unmatched keys while still validating matched ones.",
239 + "schema": {
240 + "type": "object",
241 + "patternProperties": { "^s_": { "type": "string" } }
242 + },
243 + "instances": [
244 + {
245 + "input": { "s_a": "x", "other": 1 },
246 + "accepted": true,
247 + "zod": { "output": { "s_a": "x", "other": 1 } },
248 + "effect": { "output": { "s_a": "x", "other": 1 } },
249 + "python": { "output": { "s_a": "x", "other": 1 } }
250 + },
251 + {
252 + "input": { "s_a": 5 },
253 + "accepted": false
254 + }
255 + ]
256 + },
257 + {
258 + "id": "multiple-matching-patterns",
259 + "description": "A key matching several non-transforming patterns must satisfy all of them.",
260 + "schema": {
261 + "type": "object",
262 + "patternProperties": {
263 + "^a": { "type": "string" },
264 + "b$": { "type": "string", "minLength": 2 }
265 + }
266 + },
267 + "instances": [
268 + {
269 + "input": { "ab": "xy" },
270 + "accepted": true,
271 + "zod": { "output": { "ab": "xy" } },
272 + "effect": { "output": { "ab": "xy" } },
273 + "python": { "output": { "ab": "xy" } }
274 + },
275 + {
276 + "input": { "ab": "x" },
277 + "accepted": false
278 + },
279 + {
280 + "input": { "ab": 5 },
281 + "accepted": false
282 + }
283 + ]
284 + },
285 + {
286 + "id": "declared-property-also-matching-a-pattern",
287 + "description": "A key declared in `properties` is still checked against every matching pattern.",
288 + "schema": {
289 + "type": "object",
290 + "properties": { "x_id": { "type": "string" } },
291 + "patternProperties": { "^x_": { "type": "string", "minLength": 3 } }
292 + },
293 + "ingress": {
294 + "accepted": true,
295 + "preserved": {
296 + "type": "object",
297 + "properties": { "x_id": { "type": "string" } },
298 + "patternProperties": { "^x_": { "type": "string", "minLength": 3 } }
299 + }
300 + },
301 + "instances": [
302 + {
303 + "input": { "x_id": "abc" },
304 + "accepted": true,
305 + "zod": { "output": { "x_id": "abc" } },
306 + "effect": { "output": { "x_id": "abc" } },
307 + "python": { "output": { "x_id": "abc" } }
308 + },
309 + {
310 + "input": { "x_id": "ab" },
311 + "accepted": false
312 + }
313 + ]
314 + },
315 + {
316 + "id": "schema-valued-additional-applies-only-to-unmatched-keys",
317 + "description": "Schema-valued `additionalProperties` must not rescue a key that a `patternProperties` entry already claims and rejects.",
318 + "schema": {
319 + "type": "object",
320 + "properties": { "name": { "type": "string" } },
321 + "patternProperties": { "^n_": { "type": "number" } },
322 + "additionalProperties": { "type": "boolean" }
323 + },
324 + "ingress": {
325 + "accepted": true,
326 + "preserved": {
327 + "type": "object",
328 + "properties": { "name": { "type": "string" } },
329 + "patternProperties": { "^n_": { "type": "number" } },
330 + "additionalProperties": { "type": "boolean" }
331 + }
332 + },
333 + "instances": [
334 + {
335 + "input": { "name": "a", "n_x": 1, "flag": true },
336 + "accepted": true,
337 + "zod": { "output": { "name": "a", "n_x": 1, "flag": true } },
338 + "effect": { "output": { "name": "a", "n_x": 1, "flag": true } },
339 + "python": { "output": { "name": "a", "n_x": 1, "flag": true } }
340 + },
341 + {
342 + "input": { "name": "a", "n_x": true },
343 + "accepted": false
344 + },
345 + {
346 + "input": { "name": "a", "flag": 1 },
347 + "accepted": false
348 + }
349 + ]
350 + },
351 + {
352 + "id": "pattern-property-default-materialization",
353 + "description": "Composio divergence: Zod and Pydantic materialize `default` inside a matching pattern schema, while Effect decoding is validation-only and returns the input unchanged.",
354 + "divergesFromJsonSchema": "`default` is annotation-only in JSON Schema; the Zod and Pydantic converters materialize it.",
355 + "schema": {
356 + "type": "object",
357 + "properties": {},
358 + "patternProperties": {
359 + "^p_": {
360 + "type": "object",
361 + "properties": { "mode": { "type": "string", "default": "auto" } }
362 + }
363 + }
364 + },
365 + "ingress": {
366 + "accepted": true,
367 + "preserved": {
368 + "type": "object",
369 + "properties": {},
370 + "patternProperties": {
371 + "^p_": {
372 + "type": "object",
373 + "properties": { "mode": { "type": "string", "default": "auto" } }
374 + }
375 + }
376 + }
377 + },
378 + "instances": [
379 + {
380 + "input": { "p_a": {} },
381 + "accepted": true,
382 + "zod": { "output": { "p_a": { "mode": "auto" } } },
383 + "effect": { "output": { "p_a": {} } },
384 + "python": { "output": { "p_a": { "mode": "auto" } } }
385 + }
386 + ]
387 + },
388 + {
389 + "id": "additional-property-default-materialization",
390 + "description": "Composio divergence: a schema-valued `additionalProperties` also materializes defaults in Zod and Pydantic while Effect leaves the input unchanged.",
391 + "divergesFromJsonSchema": "`default` is annotation-only in JSON Schema; the Zod and Pydantic converters materialize it.",
392 + "schema": {
393 + "type": "object",
394 + "properties": { "name": { "type": "string" } },
395 + "additionalProperties": {
396 + "type": "object",
397 + "properties": { "enabled": { "type": "boolean", "default": false } }
398 + }
399 + },
400 + "ingress": {
401 + "accepted": true,
402 + "preserved": {
403 + "type": "object",
404 + "properties": { "name": { "type": "string" } },
405 + "additionalProperties": {
406 + "type": "object",
407 + "properties": { "enabled": { "type": "boolean", "default": false } }
408 + }
409 + }
410 + },
411 + "instances": [
412 + {
413 + "input": { "name": "a", "extra": {} },
414 + "accepted": true,
415 + "zod": { "output": { "name": "a", "extra": { "enabled": false } } },
416 + "effect": { "output": { "name": "a", "extra": {} } },
417 + "python": { "output": { "name": "a", "extra": { "enabled": false } } }
418 + }
419 + ]
420 + }
421 + ]
422 +}
ts/packages/core/test/utils/jsonSchema.test.ts
+84
@@ -6,6 +6,11 @@ import {
6 import { JsonSchemaRefResolutionError } from '../../src/errors/ValidationErrors';
7 import logger from '../../src/utils/logger';
8 import { ToolSchema } from '../../src/types/tool.types';
9 +import {
10 + assertCorpusInvariants,
11 + loadObjectCases,
12 + type CorpusCase,
13 +} from '../fixtures/json-schema-conversion/corpus';
14
15 const containsRef = (value: unknown): boolean => {
16 if (value === null || typeof value !== 'object') return false;
@@ -594,3 +599,82 @@ describe('deduplicateJsonSchemaRequiredArrays', () => {
599 expect(tool.inputParameters?.properties.options.required).toEqual(['enabled']);
600 });
601 });
602 +
603 +describe('ToolSchema parameter-root preservation', () => {
604 + for (const testCase of loadObjectCases()) {
605 + const { ingress } = testCase;
606 + if (!ingress) {
607 + continue;
608 + }
609 +
610 + it(`preserves the ${testCase.id} parameter root exactly`, () => {
611 + const result = ToolSchema.safeParse({
612 + slug: 'TEST_TOOL',
613 + name: 'Test tool',
614 + inputParameters: testCase.schema,
615 + });
616 +
617 + expect(result.success).toBe(ingress.accepted);
618 + if (result.success) {
619 + expect(result.data.inputParameters).toEqual(ingress.preserved);
620 + }
621 + });
622 + }
623 +
624 + it('leaves an omitted root additionalProperties omitted', () => {
625 + const tool = ToolSchema.parse({
626 + slug: 'TEST_TOOL',
627 + name: 'Test tool',
628 + inputParameters: { type: 'object', properties: { name: { type: 'string' } } },
629 + });
630 +
631 + expect(tool.inputParameters).not.toHaveProperty('additionalProperties');
632 + });
633 +});
634 +
635 +describe('shared corpus invariants', () => {
636 + const baseCase = (id: string): CorpusCase => ({
637 + id,
638 + schema: { type: 'object' },
639 + instances: [{ input: {}, accepted: true }],
640 + });
641 +
642 + it('accepts the checked-in corpus', () => {
643 + expect(() => assertCorpusInvariants(loadObjectCases())).not.toThrow();
644 + });
645 +
646 + it('rejects duplicate case ids', () => {
647 + expect(() => assertCorpusInvariants([baseCase('dup'), baseCase('dup')])).toThrow(
648 + 'Duplicate corpus case id: dup'
649 + );
650 + });
651 +
652 + it('rejects a per-language acceptance override without a declared divergence', () => {
653 + const undeclared: CorpusCase = {
654 + id: 'undeclared',
655 + schema: { type: 'object' },
656 + instances: [{ input: {}, accepted: true, python: { accepted: false } }],
657 + };
658 +
659 + expect(() => assertCorpusInvariants([undeclared])).toThrow(
660 + 'overrides python acceptance without a divergence reason'
661 + );
662 + });
663 +
664 + it('allows a per-language acceptance override that declares its reason', () => {
665 + const declared: CorpusCase = {
666 + id: 'declared',
667 + schema: { type: 'object' },
668 + instances: [
669 + {
670 + input: {},
671 + accepted: true,
672 + python: { accepted: false },
673 + divergence: { reason: 'documented Pydantic limitation' },
674 + },
675 + ],
676 + };
677 +
678 + expect(() => assertCorpusInvariants([declared])).not.toThrow();
679 + });
680 +});
ts/packages/json-schema-to-effect-schema/src/index.ts
+13 -1
@@ -53,6 +53,9 @@ const isObjectSchema = (schema: JsonObject): boolean =>
53 'patternProperties' in schema ||
54 'additionalProperties' in schema;
55
56 +const hasNamedProperties = (schema: JsonObject): boolean =>
57 + isJsonObject(schema.properties) && Object.keys(schema.properties).length > 0;
58 +
59 const appendAllOf = (schema: JsonObject, constraint: JsonObject): void => {
60 const current = schema.allOf;
61 schema.allOf = Array.isArray(current) ? [...current, constraint] : [constraint];
@@ -162,7 +165,16 @@ const normalizeSchemaNode = (
165 }
166 }
167
165 - if (isObjectSchema(normalized) && normalized.additionalProperties === undefined) {
168 + // Composio tool inputs treat an omitted `additionalProperties` as strict, but
169 + // only for objects that actually name properties. Injecting `false` into a
170 + // property-less object (`{ type: "object" }`, `properties: {}`) or a
171 + // pattern-only object turned every valid free-form payload into an unknown-key
172 + // rejection.
173 + if (
174 + isObjectSchema(normalized) &&
175 + normalized.additionalProperties === undefined &&
176 + hasNamedProperties(normalized)
177 + ) {
178 normalized.additionalProperties = false;
179 }
180
ts/packages/json-schema-to-effect-schema/test/fixtures/corpus.ts new
+85
@@ -0,0 +1,85 @@
1 +/**
2 + * Loader for the shared cross-SDK JSON Schema conversion corpus.
3 + *
4 + * The corpus is a single logical fixture with one byte-identical copy per
5 + * language (`ts/packages/core/test/fixtures/json-schema-conversion/` and
6 + * `python/tests/fixtures/json-schema-conversion/`), following the same
7 + * convention as the webhook fixtures. Every consumer resolves its own copy
8 + * module-relatively — no working-directory or repository-root assumptions.
9 + */
10 +import * as z from 'zod';
11 +
12 +import objectCases from '../../../core/test/fixtures/json-schema-conversion/object-cases.json';
13 +
14 +const JsonObject = z.record(z.string(), z.unknown());
15 +
16 +const LanguageExpectation = z.object({
17 + output: z.unknown().optional(),
18 + accepted: z.boolean().optional(),
19 +});
20 +
21 +const CorpusInstance = z.object({
22 + input: JsonObject,
23 + accepted: z.boolean(),
24 + zod: LanguageExpectation.optional(),
25 + effect: LanguageExpectation.optional(),
26 + python: LanguageExpectation.optional(),
27 + divergence: z.object({ reason: z.string().min(1) }).optional(),
28 +});
29 +
30 +const CorpusCase = z.object({
31 + id: z.string().min(1),
32 + description: z.string().optional(),
33 + divergesFromJsonSchema: z.string().optional(),
34 + schema: JsonObject,
35 + ingress: z.object({ accepted: z.boolean(), preserved: JsonObject }).optional(),
36 + instances: z.array(CorpusInstance).min(1),
37 +});
38 +
39 +const Corpus = z.object({ cases: z.array(CorpusCase).min(1) });
40 +
41 +export type CorpusInstance = z.infer<typeof CorpusInstance>;
42 +export type CorpusCase = z.infer<typeof CorpusCase>;
43 +export type CorpusLanguage = 'zod' | 'effect' | 'python';
44 +
45 +/**
46 + * Enforce the invariants that keep the corpus a shared contract rather than a
47 + * per-language wish list: ids are unique, and a language may only disagree about
48 + * acceptance when the instance declares why.
49 + */
50 +export const assertCorpusInvariants = (cases: ReadonlyArray<CorpusCase>): void => {
51 + const seen = new Set<string>();
52 + for (const testCase of cases) {
53 + if (seen.has(testCase.id)) {
54 + throw new Error(`Duplicate corpus case id: ${testCase.id}`);
55 + }
56 + seen.add(testCase.id);
57 +
58 + for (const [index, instance] of testCase.instances.entries()) {
59 + for (const language of ['zod', 'effect', 'python'] as const) {
60 + const override = instance[language]?.accepted;
61 + if (override !== undefined && override !== instance.accepted && !instance.divergence) {
62 + throw new Error(
63 + `Corpus case ${testCase.id} instance ${index} overrides ${language} acceptance without a divergence reason`
64 + );
65 + }
66 + }
67 + }
68 + }
69 +};
70 +
71 +let cached: ReadonlyArray<CorpusCase> | undefined;
72 +
73 +/** Decode the shared corpus and enforce its cross-language invariants. */
74 +export const loadObjectCases = (): ReadonlyArray<CorpusCase> => {
75 + if (!cached) {
76 + const { cases } = Corpus.parse(objectCases);
77 + assertCorpusInvariants(cases);
78 + cached = cases;
79 + }
80 + return cached;
81 +};
82 +
83 +/** Acceptance for one language: the shared flag unless a declared divergence overrides it. */
84 +export const acceptedFor = (instance: CorpusInstance, language: CorpusLanguage): boolean =>
85 + instance[language]?.accepted ?? instance.accepted;
ts/packages/json-schema-to-effect-schema/test/json-schema-to-effect-schema.test.ts
+25
@@ -2,6 +2,7 @@ import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
2 import { Either, Schema } from 'effect';
3 import { describe, expect, it } from 'vitest';
4 import { jsonSchemaToEffectSchema, type JsonSchemaValidationIssue } from '../src/index';
5 +import { acceptedFor, loadObjectCases } from './fixtures/corpus';
6
7 type JsonSchema = Record<string, unknown>;
8
@@ -116,3 +117,27 @@ describe('jsonSchemaToEffectSchema', () => {
117 );
118 });
119 });
120 +
121 +describe('shared cross-SDK object corpus', () => {
122 + for (const testCase of loadObjectCases()) {
123 + describe(testCase.id, () => {
124 + for (const [index, instance] of testCase.instances.entries()) {
125 + const expected = acceptedFor(instance, 'effect');
126 +
127 + it(`instance ${index} is ${expected ? 'accepted' : 'rejected'}`, () => {
128 + const result = Schema.decodeUnknownEither(
129 + jsonSchemaToEffectSchema(testCase.schema as JsonSchema),
130 + { errors: 'all' }
131 + )(instance.input);
132 +
133 + expect(Either.isRight(result)).toBe(expected);
134 + if (Either.isRight(result) && instance.effect && 'output' in instance.effect) {
135 + // Effect decoding is validation-only: a successful decode returns the
136 + // input value unchanged, with no default materialization.
137 + expect(result.right).toEqual(instance.effect.output);
138 + }
139 + });
140 + }
141 + });
142 + }
143 +});
ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
+114 -92
@@ -7,15 +7,25 @@ import { parseSchema } from './parse-schema';
7 import type { JsonSchemaObject, Refs, JsonSchema } from '../types';
8 import { its } from '../utils/its';
9
10 +/**
11 + * Build the static part of an object parser, or `undefined` when the schema
12 + * declares no named properties.
13 + *
14 + * A property-less object (`properties` absent or `{}`) deliberately returns
15 + * `undefined` so the caller can apply the open-object policy. This supersedes
16 + * an earlier `z.object({})` fallback that was kept "or else this will break
17 + * openai responses": collapsing `{ type: "object" }` to a strict empty object
18 + * is the defect it was masking, and the OpenAI provider suite is part of this
19 + * package's verification precisely so the claim stays checked.
20 + */
21 function parseObjectProperties(objectSchema: JsonSchemaObject & { type?: 'object' }, refs: Refs) {
22 if (!objectSchema.properties) {
12 - // leave it as an empty object or else this will break openai responses
13 - return z.object({});
23 + return undefined;
24 }
25
26 const propertyKeys = Object.keys(objectSchema.properties);
27 if (propertyKeys.length === 0) {
18 - return z.object({});
28 + return undefined;
29 }
30
31 const properties: Record<string, z.ZodTypeAny> = {};
@@ -78,6 +88,96 @@ function parseObjectProperties(objectSchema: JsonSchemaObject & { type?: 'object
88 return z.object(properties);
89 }
90
91 +/**
92 + * Object parser for schemas carrying `patternProperties`.
93 + *
94 + * Every key is routed to exactly the schemas that apply to it — each matching
95 + * pattern, and `additionalProperties` only when no pattern and no named
96 + * property claims the key. A catch-all union would instead let a key sneak past
97 + * its own pattern by satisfying an unrelated one.
98 + */
99 +function parseDynamicKeyObject(
100 + normalizedSchema: JsonSchemaObject & { type?: 'object' },
101 + propertiesSchema: z.ZodObject<Record<string, z.ZodTypeAny>, 'strip', z.ZodTypeAny> | undefined,
102 + additionalProperties: z.ZodTypeAny | undefined,
103 + refs: Refs
104 +): z.ZodTypeAny {
105 + const patterns = Object.entries(normalizedSchema.patternProperties ?? {}).map(
106 + ([pattern, schema]) => ({
107 + regex: new RegExp(pattern),
108 + parser: parseSchema(schema, {
109 + ...refs,
110 + path: [...refs.path, 'patternProperties', pattern],
111 + }),
112 + })
113 + );
114 +
115 + const declaredKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
116 + const rejectsUnmatched =
117 + additionalProperties instanceof z.ZodNever ||
118 + (additionalProperties === undefined && declaredKeys.size > 0);
119 + const validatesUnmatched =
120 + additionalProperties !== undefined &&
121 + !(additionalProperties instanceof z.ZodNever) &&
122 + normalizedSchema.additionalProperties !== true;
123 +
124 + const base = (propertiesSchema ?? z.object({})).passthrough();
125 +
126 + return base.transform((value: Record<string, unknown>, ctx) => {
127 + const result: Record<string, unknown> = { ...value };
128 + const unrecognizedKeys: string[] = [];
129 +
130 + for (const key of Object.keys(value)) {
131 + const matching = patterns.filter(({ regex }) => regex.test(key));
132 +
133 + for (const { parser } of matching) {
134 + const parsed = parser.safeParse(value[key]);
135 + if (!parsed.success) {
136 + ctx.addIssue({
137 + path: [...ctx.path, key],
138 + code: 'custom',
139 + message: `Invalid input: key "${key}" must match its patternProperties schema`,
140 + params: { issues: parsed.error.issues },
141 + });
142 + } else if (!declaredKeys.has(key)) {
143 + result[key] = parsed.data;
144 + }
145 + }
146 +
147 + if (matching.length > 0 || declaredKeys.has(key)) {
148 + continue;
149 + }
150 +
151 + if (rejectsUnmatched) {
152 + unrecognizedKeys.push(key);
153 + } else if (validatesUnmatched) {
154 + const parsed = additionalProperties!.safeParse(value[key]);
155 + if (!parsed.success) {
156 + ctx.addIssue({
157 + path: [...ctx.path, key],
158 + code: 'custom',
159 + message: `Invalid input: key "${key}" must match the additionalProperties schema`,
160 + params: { issues: parsed.error.issues },
161 + });
162 + } else {
163 + result[key] = parsed.data;
164 + }
165 + }
166 + }
167 +
168 + if (unrecognizedKeys.length > 0) {
169 + ctx.addIssue({
170 + path: [...ctx.path],
171 + code: 'unrecognized_keys',
172 + keys: unrecognizedKeys,
173 + message: `Unrecognized key(s) in object: ${unrecognizedKeys.map(key => `'${key}'`).join(', ')}`,
174 + });
175 + }
176 +
177 + return result;
178 + });
179 +}
180 +
181 export function parseObject(
182 objectSchema: JsonSchemaObject & { type?: 'object' },
183 refs: Refs
@@ -89,9 +189,8 @@ export function parseObject(
189 objectSchema.type === 'object' ? objectSchema : { ...objectSchema, type: 'object' as const };
190
191 const propertiesSchema:
92 - | z.ZodObject<Record<string, z.ZodTypeAny>, 'strip', z.ZodTypeAny>
93 - | undefined = parseObjectProperties(normalizedSchema, refs);
94 - let zodSchema: z.ZodTypeAny | undefined = propertiesSchema;
192 + z.ZodObject<Record<string, z.ZodTypeAny>, 'strip', z.ZodTypeAny> | undefined =
193 + parseObjectProperties(normalizedSchema, refs);
194
195 const additionalProperties =
196 normalizedSchema.additionalProperties !== undefined
@@ -104,88 +203,11 @@ export function parseObject(
203 // Track if additionalProperties was explicitly set to true
204 const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
205
107 - if (normalizedSchema.patternProperties) {
108 - const parsedPatternProperties = Object.fromEntries(
109 - Object.entries(normalizedSchema.patternProperties).map(([key, value]) => {
110 - return [
111 - key,
112 - parseSchema(value, {
113 - ...refs,
114 - path: [...refs.path, 'patternProperties', key],
115 - }),
116 - ];
117 - })
118 - );
119 - const patternPropertyValues = Object.values(parsedPatternProperties);
120 -
121 - if (propertiesSchema) {
122 - if (additionalProperties) {
123 - zodSchema = propertiesSchema.catchall(
124 - z.union([...patternPropertyValues, additionalProperties] as [z.ZodTypeAny, z.ZodTypeAny])
125 - );
126 - } else if (Object.keys(parsedPatternProperties).length > 1) {
127 - zodSchema = propertiesSchema.catchall(
128 - z.union(patternPropertyValues as [z.ZodTypeAny, z.ZodTypeAny])
129 - );
130 - } else {
131 - zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
132 - }
133 - } else {
134 - if (additionalProperties) {
135 - zodSchema = z.record(
136 - z.union([...patternPropertyValues, additionalProperties] as [z.ZodTypeAny, z.ZodTypeAny])
137 - );
138 - } else if (patternPropertyValues.length > 1) {
139 - zodSchema = z.record(z.union(patternPropertyValues as [z.ZodTypeAny, z.ZodTypeAny]));
140 - } else {
141 - zodSchema = z.record(patternPropertyValues[0]);
142 - }
143 - }
144 -
145 - const objectPropertyKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
146 - zodSchema = zodSchema.superRefine((value: Record<string, unknown>, ctx) => {
147 - for (const key in value) {
148 - let wasMatched = objectPropertyKeys.has(key);
149 -
150 - for (const patternPropertyKey in normalizedSchema.patternProperties) {
151 - const regex = new RegExp(patternPropertyKey);
152 - if (key.match(regex)) {
153 - wasMatched = true;
154 - const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
155 - if (!result.success) {
156 - ctx.addIssue({
157 - path: [...ctx.path, key],
158 - code: 'custom',
159 - message: `Invalid input: Key matching regex /${key}/ must match schema`,
160 - params: {
161 - issues: result.error.issues,
162 - },
163 - });
164 - }
165 - }
166 - }
167 -
168 - if (!wasMatched && additionalProperties) {
169 - const result = additionalProperties.safeParse(value[key]);
170 - if (!result.success) {
171 - ctx.addIssue({
172 - path: [...ctx.path, key],
173 - code: 'custom',
174 - message: 'Invalid input: must match catchall schema',
175 - params: {
176 - issues: result.error.issues,
177 - },
178 - });
179 - }
180 - }
181 - }
182 - });
183 - }
206 let output: z.ZodTypeAny;
185 - if (propertiesSchema) {
186 - if (hasPatternProperties) {
187 - output = zodSchema!;
188 - } else if (additionalProperties) {
207 + if (hasPatternProperties) {
208 + output = parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs);
209 + } else if (propertiesSchema) {
210 + if (additionalProperties) {
211 if (additionalProperties instanceof z.ZodNever) {
212 output = propertiesSchema.strict();
213 } else if (isAdditionalPropertiesTrue) {
@@ -195,13 +217,12 @@ export function parseObject(
217 output = propertiesSchema.catchall(additionalProperties);
218 }
219 } else {
198 - // When additionalProperties is not specified, treat it as false
220 + // Named properties with an omitted `additionalProperties` stay strict for
221 + // tool inputs, even though JSON Schema itself would allow unknown keys.
222 output = propertiesSchema.strict();
223 }
224 } else {
202 - if (hasPatternProperties) {
203 - output = zodSchema!;
204 - } else if (additionalProperties) {
225 + if (additionalProperties) {
226 if (additionalProperties instanceof z.ZodNever) {
227 // When additionalProperties is false, create strict empty object
228 output = z.object({}).strict();
@@ -212,7 +233,8 @@ export function parseObject(
233 output = z.record(additionalProperties);
234 }
235 } else {
215 - // When no properties and no additionalProperties specified, default to allowing additional properties
236 + // A property-less object with no `additionalProperties` keyword is open:
237 + // arbitrary content is accepted and preserved verbatim.
238 output = z.object({}).passthrough();
239 }
240 }
ts/packages/json-schema-to-zod/test/fixtures/corpus.ts new
+85
@@ -0,0 +1,85 @@
1 +/**
2 + * Loader for the shared cross-SDK JSON Schema conversion corpus.
3 + *
4 + * The corpus is a single logical fixture with one byte-identical copy per
5 + * language (`ts/packages/core/test/fixtures/json-schema-conversion/` and
6 + * `python/tests/fixtures/json-schema-conversion/`), following the same
7 + * convention as the webhook fixtures. Every consumer resolves its own copy
8 + * module-relatively — no working-directory or repository-root assumptions.
9 + */
10 +import * as z from 'zod';
11 +
12 +import objectCases from '../../../core/test/fixtures/json-schema-conversion/object-cases.json';
13 +
14 +const JsonObject = z.record(z.string(), z.unknown());
15 +
16 +const LanguageExpectation = z.object({
17 + output: z.unknown().optional(),
18 + accepted: z.boolean().optional(),
19 +});
20 +
21 +const CorpusInstance = z.object({
22 + input: JsonObject,
23 + accepted: z.boolean(),
24 + zod: LanguageExpectation.optional(),
25 + effect: LanguageExpectation.optional(),
26 + python: LanguageExpectation.optional(),
27 + divergence: z.object({ reason: z.string().min(1) }).optional(),
28 +});
29 +
30 +const CorpusCase = z.object({
31 + id: z.string().min(1),
32 + description: z.string().optional(),
33 + divergesFromJsonSchema: z.string().optional(),
34 + schema: JsonObject,
35 + ingress: z.object({ accepted: z.boolean(), preserved: JsonObject }).optional(),
36 + instances: z.array(CorpusInstance).min(1),
37 +});
38 +
39 +const Corpus = z.object({ cases: z.array(CorpusCase).min(1) });
40 +
41 +export type CorpusInstance = z.infer<typeof CorpusInstance>;
42 +export type CorpusCase = z.infer<typeof CorpusCase>;
43 +export type CorpusLanguage = 'zod' | 'effect' | 'python';
44 +
45 +/**
46 + * Enforce the invariants that keep the corpus a shared contract rather than a
47 + * per-language wish list: ids are unique, and a language may only disagree about
48 + * acceptance when the instance declares why.
49 + */
50 +export const assertCorpusInvariants = (cases: ReadonlyArray<CorpusCase>): void => {
51 + const seen = new Set<string>();
52 + for (const testCase of cases) {
53 + if (seen.has(testCase.id)) {
54 + throw new Error(`Duplicate corpus case id: ${testCase.id}`);
55 + }
56 + seen.add(testCase.id);
57 +
58 + for (const [index, instance] of testCase.instances.entries()) {
59 + for (const language of ['zod', 'effect', 'python'] as const) {
60 + const override = instance[language]?.accepted;
61 + if (override !== undefined && override !== instance.accepted && !instance.divergence) {
62 + throw new Error(
63 + `Corpus case ${testCase.id} instance ${index} overrides ${language} acceptance without a divergence reason`
64 + );
65 + }
66 + }
67 + }
68 + }
69 +};
70 +
71 +let cached: ReadonlyArray<CorpusCase> | undefined;
72 +
73 +/** Decode the shared corpus and enforce its cross-language invariants. */
74 +export const loadObjectCases = (): ReadonlyArray<CorpusCase> => {
75 + if (!cached) {
76 + const { cases } = Corpus.parse(objectCases);
77 + assertCorpusInvariants(cases);
78 + cached = cases;
79 + }
80 + return cached;
81 +};
82 +
83 +/** Acceptance for one language: the shared flag unless a declared divergence overrides it. */
84 +export const acceptedFor = (instance: CorpusInstance, language: CorpusLanguage): boolean =>
85 + instance[language]?.accepted ?? instance.accepted;
ts/packages/json-schema-to-zod/test/json-to-zod.test.ts
+21
@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest';
2 import { z } from 'zod/v3';
3 import { jsonSchemaToZod } from '../src/json-schema-to-zod';
4 import type { JsonSchema } from '../src/types';
5 +import { acceptedFor, loadObjectCases } from './fixtures/corpus';
6
7 describe('jsonSchemaToZod', () => {
8 describe('Basic Types', () => {
@@ -1513,3 +1514,23 @@ describe('jsonSchemaToZod', () => {
1514 });
1515 });
1516 });
1517 +
1518 +describe('shared cross-SDK object corpus', () => {
1519 + for (const testCase of loadObjectCases()) {
1520 + describe(testCase.id, () => {
1521 + for (const [index, instance] of testCase.instances.entries()) {
1522 + const expected = acceptedFor(instance, 'zod');
1523 +
1524 + it(`instance ${index} is ${expected ? 'accepted' : 'rejected'}`, () => {
1525 + const zodSchema = jsonSchemaToZod(testCase.schema as JsonSchema);
1526 + const result = zodSchema.safeParse(instance.input);
1527 +
1528 + expect(result.success).toBe(expected);
1529 + if (result.success && instance.zod && 'output' in instance.zod) {
1530 + expect(result.data).toEqual(instance.zod.output);
1531 + }
1532 + });
1533 + }
1534 + });
1535 + }
1536 +});
turbo.jsonc
+8 -1
@@ -24,7 +24,14 @@
24 "outputs": ["dist/"]
25 },
26 "test": {
27 - "dependsOn": ["^build"]
27 + "dependsOn": ["^build"],
28 + // the shared cross-SDK JSON Schema conversion corpus is owned by
29 + // `@composio/core` but read by the Zod, Effect, and CLI test suites, whose
30 + // $TURBO_DEFAULT$ inputs stop at their own package directory
31 + "inputs": [
32 + "$TURBO_DEFAULT$",
33 + "../core/test/fixtures/json-schema-conversion/object-cases.json"
34 + ]
35 },
36 "test:e2e": {
37 "dependsOn": ["^build"],