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(python): walk only schema positions when checking dynamic refs
The dynamic-key reference walker descended into every nested dict, so a $ref-shaped value stored under const, default, enum, or examples was treated as a schema reference and raised while the tool was wrapped. Restrict the walk to Draft 7 schema positions.
jkomyno committed
Aug 7, 2026 at 19:39 UTC
1f941675951a2f74692e7d9bbea8a13e84eb36e4
2 files changed
+108
-3
python/composio/utils/schema_converter.py
+48
-3
@@ -335,12 +335,41 @@ def _resolve_local_json_pointer(
335
return current
336
337
338
+# Draft 7 keywords whose value is itself a schema. `items` and `dependencies`
339
+# are handled separately because they also accept non-schema forms.
340
+_SCHEMA_VALUED_KEYWORDS = frozenset(
341
+ {
342
+ "additionalItems",
343
+ "additionalProperties",
344
+ "contains",
345
+ "else",
346
+ "if",
347
+ "not",
348
+ "propertyNames",
349
+ "then",
350
+ }
351
+)
352
+
353
+# Draft 7 keywords whose value is a list of schemas.
354
+_SCHEMA_LIST_KEYWORDS = frozenset({"allOf", "anyOf", "oneOf"})
355
+
356
+# Draft 7 keywords whose value maps names to schemas.
357
+_SCHEMA_MAP_KEYWORDS = frozenset(
358
+ {"$defs", "definitions", "patternProperties", "properties"}
359
+)
360
+
361
+
362
def _check_dynamic_references(
363
schema: t.Any,
364
root_schema: t.Dict[str, t.Any],
365
checked_references: t.Optional[t.Set[str]] = None,
366
) -> None:
343
- """Reject external, anchored, and unresolved references before validation."""
367
+ """Reject external, anchored, and unresolved references before validation.
368
+
369
+ Only schema positions are walked. ``const``, ``default``, ``enum``, and
370
+ ``examples`` hold instance data, so a ``$ref``-shaped value stored there is
371
+ a payload rather than a reference and must not block tool wrapping.
372
+ """
373
if checked_references is None:
374
checked_references = set()
375
if isinstance(schema, list):
@@ -363,8 +392,24 @@ def _check_dynamic_references(
392
checked_references.add(reference)
393
_check_dynamic_references(resolved, root_schema, checked_references)
394
366
- for value in schema.values():
367
- _check_dynamic_references(value, root_schema, checked_references)
395
+ for keyword, value in schema.items():
396
+ if keyword in _SCHEMA_VALUED_KEYWORDS or keyword in _SCHEMA_LIST_KEYWORDS:
397
+ _check_dynamic_references(value, root_schema, checked_references)
398
+ elif keyword in _SCHEMA_MAP_KEYWORDS:
399
+ if isinstance(value, dict):
400
+ for entry in value.values():
401
+ _check_dynamic_references(entry, root_schema, checked_references)
402
+ elif keyword == "items":
403
+ # A schema, or a list of schemas for tuple validation.
404
+ _check_dynamic_references(value, root_schema, checked_references)
405
+ elif keyword == "dependencies":
406
+ # Each entry is either a schema or a list of required property names.
407
+ if isinstance(value, dict):
408
+ for entry in value.values():
409
+ if isinstance(entry, (dict, bool)):
410
+ _check_dynamic_references(
411
+ entry, root_schema, checked_references
412
+ )
413
414
415
def _dynamic_key_validator(
python/tests/test_schema_parser.py
+60
@@ -1865,3 +1865,63 @@ class TestSharedObjectCorpusThroughJsonSchemaToModel:
1865
1866
with pytest.raises(ValueError, match="must be a local JSON Pointer"):
1867
json_schema_to_model(schema)
1868
+
1869
+ @pytest.mark.parametrize(
1870
+ "keyword,value,accepted",
1871
+ [
1872
+ (
1873
+ "default",
1874
+ {"$ref": "https://example.com/schema.json"},
1875
+ {"$ref": "kept"},
1876
+ ),
1877
+ ("const", {"$ref": "other.json#/thing"}, {"$ref": "other.json#/thing"}),
1878
+ ("enum", [{"$ref": "#/$defs/missing"}], {"$ref": "#/$defs/missing"}),
1879
+ ("examples", [{"$ref": "#anchor"}], {"$ref": "kept"}),
1880
+ ],
1881
+ )
1882
+ def test_dynamic_schema_ignores_reference_shaped_instance_data(
1883
+ self,
1884
+ keyword: str,
1885
+ value: t.Any,
1886
+ accepted: t.Dict[str, t.Any],
1887
+ ) -> None:
1888
+ """`$ref`-shaped payloads are data, not references, and must not raise."""
1889
+ schema = {
1890
+ "type": "object",
1891
+ "patternProperties": {"^value_": {"type": "object", keyword: value}},
1892
+ }
1893
+
1894
+ model = json_schema_to_model(schema)
1895
+
1896
+ assert model.model_validate({"value_a": accepted}).model_dump() == {
1897
+ "value_a": accepted
1898
+ }
1899
+
1900
+ @pytest.mark.parametrize(
1901
+ "dynamic_schema",
1902
+ [
1903
+ {"type": "array", "items": {"$ref": "https://example.com/schema.json"}},
1904
+ {"allOf": [{"$ref": "https://example.com/schema.json"}]},
1905
+ {
1906
+ "type": "object",
1907
+ "properties": {"inner": {"$ref": "https://example.com/schema.json"}},
1908
+ },
1909
+ {
1910
+ "type": "object",
1911
+ "additionalProperties": {
1912
+ "$ref": "https://example.com/schema.json",
1913
+ },
1914
+ },
1915
+ ],
1916
+ )
1917
+ def test_dynamic_schema_checks_references_in_nested_schema_positions(
1918
+ self,
1919
+ dynamic_schema: t.Dict[str, t.Any],
1920
+ ) -> None:
1921
+ schema = {
1922
+ "type": "object",
1923
+ "patternProperties": {"^value_": dynamic_schema},
1924
+ }
1925
+
1926
+ with pytest.raises(ValueError, match="must be a local JSON Pointer"):
1927
+ json_schema_to_model(schema)