@devries-cameron20 / composio Mirror · read-only
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, 03:37 UTC

next
py 529 lines 16.3 KB
Raw
1 """Tests for the schema_converter boolean-schema pre-filter."""
2
3 import pytest
4 from jsonschema import Draft7Validator
5 from jsonschema.exceptions import SchemaError
6 from pydantic import TypeAdapter, ValidationError
7
8 from composio.utils.schema_converter import (
9 _filter_boolean_schemas,
10 json_schema_to_pydantic_type,
11 )
12 from composio.utils.shared import json_schema_to_model
13 from tests.fixtures.json_schema_conversion_corpus import load_object_cases
14
15
16 def test_true_becomes_empty_schema():
17 assert _filter_boolean_schemas(True) == {}
18
19
20 def test_false_becomes_none():
21 assert _filter_boolean_schemas(False) is None
22
23
24 def test_non_schema_value_passes_through():
25 assert _filter_boolean_schemas("passthrough") == "passthrough"
26
27
28 def test_list_drops_false_and_keeps_rest():
29 assert _filter_boolean_schemas([True, False, {"type": "string"}]) == [
30 {},
31 {"type": "string"},
32 ]
33
34
35 def test_list_of_all_false_becomes_none():
36 assert _filter_boolean_schemas([False, False]) is None
37
38
39 def test_combiner_drops_false_entries():
40 schema = {"anyOf": [{"type": "string"}, False]}
41 assert _filter_boolean_schemas(schema) == {"anyOf": [{"type": "string"}]}
42
43
44 def test_combiner_with_all_false_entries_rejects_every_value():
45 schema = {"type": "object", "anyOf": [False, False]}
46 adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
47 for value in (None, "some string", 123, {}, []):
48 with pytest.raises(ValidationError):
49 adapter.validate_python(value)
50
51
52 def test_false_property_is_preserved_as_rejecting():
53 schema = {"properties": {"a": False, "b": {"type": "string"}}}
54 filtered = _filter_boolean_schemas(schema)
55 assert "a" in filtered["properties"]
56 assert filtered["properties"]["b"] == {"type": "string"}
57
58
59 def test_plain_keys_are_preserved():
60 schema = {"type": "string", "title": "X"}
61 assert _filter_boolean_schemas(schema) == {"type": "string", "title": "X"}
62
63
64 @pytest.mark.schema
65 def test_schema_converter_allof_with_false_member_rejects():
66 """allOf: [false, X] must reject every value (false AND X == false)."""
67 schema_with_false = {
68 "allOf": [
69 False,
70 {"type": "object", "properties": {"a": {"type": "string"}}},
71 ]
72 }
73
74 pytype = json_schema_to_pydantic_type(schema_with_false)
75 adapter = TypeAdapter(pytype)
76 assert adapter.json_schema() == {"not": {}}
77 with pytest.raises(ValidationError):
78 adapter.validate_python({"a": "hello"})
79
80
81 @pytest.mark.schema
82 def test_schema_converter_allof_with_false_member_rejects_non_dict_values_too():
83 """The unsatisfiable-schema fallback must not just reject dicts by
84 coincidence (str previously accepted any string). It should match the
85 bare-`false`-schema convention used elsewhere in this module."""
86 schema = {"allOf": [False, {"type": "string"}]}
87 pytype = json_schema_to_pydantic_type(schema)
88 adapter = TypeAdapter(pytype)
89 for value in (None, "some string", 123, {"a": "hello"}):
90 with pytest.raises(ValidationError):
91 adapter.validate_python(value)
92
93
94 @pytest.mark.schema
95 def test_schema_converter_allof_without_false_member_still_validates():
96 """Control: the same allOf schema minus the `false` member must still work."""
97 schema_without_false = {
98 "allOf": [
99 {"type": "object", "properties": {"a": {"type": "string"}}},
100 ]
101 }
102
103 pytype = json_schema_to_pydantic_type(schema_without_false)
104 adapter = TypeAdapter(pytype)
105 result = adapter.validate_python({"a": "hello"})
106 assert result.a == "hello"
107
108
109 @pytest.mark.schema
110 def test_schema_converter_nested_unsatisfiable_allof_rejects():
111 schema = {"allOf": [{"allOf": [False, {"type": "string"}]}]}
112 adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
113
114 for value in (None, "some string", 123, {}, []):
115 with pytest.raises(ValidationError):
116 adapter.validate_python(value)
117
118
119 @pytest.mark.schema
120 def test_schema_converter_unsatisfiable_array_items_allow_only_empty_arrays():
121 schema = {
122 "type": "array",
123 "items": {"allOf": [False, {"type": "string"}]},
124 }
125 adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
126
127 assert adapter.validate_python([]) == []
128 with pytest.raises(ValidationError):
129 adapter.validate_python(["some string"])
130
131
132 @pytest.mark.schema
133 def test_schema_converter_unsatisfiable_property_stays_rejecting():
134 schema = {
135 "type": "object",
136 "properties": {
137 "allowed": {"type": "string"},
138 "impossible": {"allOf": [False, {"type": "string"}]},
139 },
140 "required": ["allowed"],
141 }
142 adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
143
144 result = adapter.validate_python({"allowed": "yes"})
145 assert result.allowed == "yes"
146 for impossible_value in (None, "some string", 123):
147 with pytest.raises(ValidationError):
148 adapter.validate_python({"allowed": "yes", "impossible": impossible_value})
149
150
151 @pytest.mark.schema
152 def test_schema_converter_unsatisfiable_definition_keeps_ref_rejecting():
153 schema = {
154 "$defs": {"Impossible": {"allOf": [False, {"type": "string"}]}},
155 "$ref": "#/$defs/Impossible",
156 }
157 adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
158
159 for value in (None, "some string", 123, {}, []):
160 with pytest.raises(ValidationError):
161 adapter.validate_python(value)
162
163
164 @pytest.mark.schema
165 def test_schema_converter_unsatisfiable_property_works_in_shared_model():
166 schema = {
167 "type": "object",
168 "title": "ToolArguments",
169 "properties": {
170 "required_value": {"type": "string"},
171 "impossible": {"allOf": [False, {"type": "string"}]},
172 },
173 "required": ["required_value"],
174 }
175 model = json_schema_to_model(schema)
176
177 assert model(required_value="yes").required_value == "yes"
178 for impossible_value in (None, "some string", 123):
179 with pytest.raises(ValidationError):
180 model(required_value="yes", impossible=impossible_value)
181
182
183 @pytest.mark.schema
184 def test_schema_converter_required_unsatisfiable_property_has_no_valid_value():
185 schema = {
186 "type": "object",
187 "title": "RequiredImpossibleArgument",
188 "properties": {
189 "impossible": {"allOf": [False, {"type": "string"}]},
190 },
191 "required": ["impossible"],
192 }
193 model = json_schema_to_model(schema)
194
195 with pytest.raises(ValidationError):
196 model()
197 for impossible_value in (None, "some string", 123):
198 with pytest.raises(ValidationError):
199 model(impossible=impossible_value)
200
201
202 @pytest.mark.parametrize(
203 "converter",
204 [json_schema_to_model, json_schema_to_pydantic_type],
205 ids=["model", "pydantic-type"],
206 )
207 def test_unsatisfiable_declared_property_keeps_root_dynamic_policy(converter):
208 schema = {
209 "type": "object",
210 "properties": {"impossible": {"allOf": [False, {"type": "string"}]}},
211 "patternProperties": {"^count_": {"type": "integer"}},
212 }
213 adapter = TypeAdapter(converter(schema))
214
215 result = adapter.validate_python({"count_a": 1})
216 assert adapter.dump_python(
217 result,
218 mode="json",
219 by_alias=True,
220 exclude_none=True,
221 ) == {"count_a": 1}
222 with pytest.raises(ValidationError):
223 adapter.validate_python({"count_a": "1"})
224 with pytest.raises(ValidationError):
225 adapter.validate_python({"impossible": None})
226
227
228 def test_dynamic_default_materialization_preserves_plain_python_values():
229 model = json_schema_to_model(
230 {
231 "type": "object",
232 "patternProperties": {
233 "^item_": {
234 "type": "object",
235 "properties": {
236 "implicit": {
237 "anyOf": [{"type": "string"}, {"type": "null"}],
238 },
239 "defaulted": {"type": "integer", "default": 1},
240 },
241 },
242 },
243 "additionalProperties": False,
244 }
245 )
246
247 result = model.model_validate({"item_a": {}})
248
249 assert result.model_extra == {"item_a": {"defaulted": 1}}
250 assert isinstance(result.model_extra["item_a"], dict)
251 assert getattr(result, "item_a") == {"defaulted": 1}
252
253
254 @pytest.mark.parametrize(
255 "invalid_object_schema,error_type,error_match",
256 [
257 (
258 {
259 "type": "object",
260 "patternProperties": {
261 "^value_": {"$ref": "https://example.com/schema.json"}
262 },
263 },
264 ValueError,
265 "must be a local JSON Pointer",
266 ),
267 (
268 {
269 "type": "object",
270 "patternProperties": {"^value_": {"$ref": "#/$defs/missing"}},
271 },
272 ValueError,
273 "Unresolvable dynamic-key schema reference",
274 ),
275 (
276 {
277 "type": "object",
278 "patternProperties": {"^value_": {"$ref": "#anchor"}},
279 },
280 ValueError,
281 "must be a local JSON Pointer",
282 ),
283 (
284 {
285 "type": "object",
286 "patternProperties": {"[": {"type": "string"}},
287 },
288 ValueError,
289 "Invalid patternProperties regular expression",
290 ),
291 (
292 {
293 "type": "object",
294 "patternProperties": {"^value_": {"type": "not-a-json-schema-type"}},
295 },
296 SchemaError,
297 "not valid under any of the given schemas",
298 ),
299 (
300 {
301 "type": "object",
302 "additionalProperties": {"$ref": "https://example.com/schema.json"},
303 },
304 ValueError,
305 "must be a local JSON Pointer",
306 ),
307 (
308 {
309 "type": "object",
310 "additionalProperties": {"$ref": "#/$defs/missing"},
311 },
312 ValueError,
313 "Unresolvable dynamic-key schema reference",
314 ),
315 (
316 {
317 "type": "object",
318 "additionalProperties": {"$ref": "#anchor"},
319 },
320 ValueError,
321 "must be a local JSON Pointer",
322 ),
323 (
324 {
325 "type": "object",
326 "additionalProperties": {"type": "not-a-json-schema-type"},
327 },
328 SchemaError,
329 "not valid under any of the given schemas",
330 ),
331 ],
332 ids=[
333 "pattern-external-ref",
334 "pattern-missing-local-ref",
335 "pattern-anchored-ref",
336 "invalid-pattern",
337 "pattern-invalid-draft7",
338 "additional-external-ref",
339 "additional-missing-local-ref",
340 "additional-anchored-ref",
341 "additional-invalid-draft7",
342 ],
343 )
344 @pytest.mark.parametrize(
345 "converter",
346 [json_schema_to_model, json_schema_to_pydantic_type],
347 ids=["model", "pydantic-type"],
348 )
349 @pytest.mark.parametrize("nested", [False, True], ids=["root", "nested"])
350 def test_invalid_dynamic_object_schema_fails_closed_across_public_entry_points(
351 invalid_object_schema,
352 error_type,
353 error_match,
354 converter,
355 nested,
356 ):
357 schema = invalid_object_schema
358 if nested:
359 schema = {
360 "type": "object",
361 "properties": {"payload": invalid_object_schema},
362 "required": ["payload"],
363 }
364
365 with pytest.raises(error_type, match=error_match):
366 converter(schema)
367
368
369 _INVALID_DYNAMIC_POLICY = {
370 "type": "object",
371 "patternProperties": {"[": {"type": "string"}},
372 }
373
374
375 @pytest.mark.parametrize(
376 "schema",
377 [
378 {"type": "object", "properties": {"payload": _INVALID_DYNAMIC_POLICY}},
379 {"type": "array", "items": _INVALID_DYNAMIC_POLICY},
380 {"type": "array", "items": [{}, _INVALID_DYNAMIC_POLICY]},
381 {"allOf": [_INVALID_DYNAMIC_POLICY]},
382 {"anyOf": [_INVALID_DYNAMIC_POLICY]},
383 {"oneOf": [_INVALID_DYNAMIC_POLICY]},
384 {"not": _INVALID_DYNAMIC_POLICY},
385 {"if": _INVALID_DYNAMIC_POLICY},
386 {"then": _INVALID_DYNAMIC_POLICY},
387 {"else": _INVALID_DYNAMIC_POLICY},
388 {"type": "array", "contains": _INVALID_DYNAMIC_POLICY},
389 {"type": "object", "propertyNames": _INVALID_DYNAMIC_POLICY},
390 {
391 "type": "array",
392 "items": [{}],
393 "additionalItems": _INVALID_DYNAMIC_POLICY,
394 },
395 {
396 "type": "object",
397 "dependencies": {"flag": _INVALID_DYNAMIC_POLICY},
398 },
399 {
400 "$defs": {"Invalid": _INVALID_DYNAMIC_POLICY},
401 "$ref": "#/$defs/Invalid",
402 },
403 {
404 "definitions": {"Invalid": _INVALID_DYNAMIC_POLICY},
405 "$ref": "#/definitions/Invalid",
406 },
407 ],
408 ids=[
409 "property",
410 "array-items",
411 "tuple-items",
412 "all-of",
413 "any-of",
414 "one-of",
415 "not",
416 "if",
417 "then",
418 "else",
419 "contains",
420 "property-names",
421 "additional-items",
422 "dependency-schema",
423 "defs-reference",
424 "legacy-definitions-reference",
425 ],
426 )
427 def test_invalid_dynamic_policy_is_found_in_reachable_draft7_schema_positions(schema):
428 with pytest.raises(ValueError, match="Invalid patternProperties"):
429 json_schema_to_pydantic_type(schema)
430
431
432 def _recursive_dynamic_schema(dynamic_keyword, nested):
433 recursive_ref = "#/$defs/Recursive" if nested else "#"
434
435 def make_recursive_object():
436 dynamic_schema = {
437 "allOf": [{"$ref": recursive_ref}],
438 "default": {},
439 }
440 recursive_object = {"type": "object"}
441 if dynamic_keyword == "patternProperties":
442 recursive_object[dynamic_keyword] = {"^child_": dynamic_schema}
443 else:
444 recursive_object[dynamic_keyword] = dynamic_schema
445 return recursive_object
446
447 recursive_object = make_recursive_object()
448 recursive_input = {"child_one": {}}
449
450 if not nested:
451 return recursive_object, recursive_input
452
453 return (
454 {
455 "$defs": {"Recursive": recursive_object},
456 "type": "object",
457 "properties": {"payload": make_recursive_object()},
458 "required": ["payload"],
459 },
460 {"payload": recursive_input},
461 )
462
463
464 @pytest.mark.parametrize(
465 "dynamic_keyword",
466 ["patternProperties", "additionalProperties"],
467 ids=["pattern-properties", "additional-properties"],
468 )
469 @pytest.mark.parametrize(
470 "converter",
471 [json_schema_to_model, json_schema_to_pydantic_type],
472 ids=["model", "pydantic-type"],
473 )
474 @pytest.mark.parametrize("nested", [False, True], ids=["root", "nested"])
475 def test_recursive_dynamic_schema_with_default_does_not_reenter_policy_validation(
476 dynamic_keyword,
477 converter,
478 nested,
479 ):
480 schema, input_value = _recursive_dynamic_schema(dynamic_keyword, nested)
481 Draft7Validator.check_schema(schema)
482
483 annotation = converter(schema)
484 adapter = TypeAdapter(annotation)
485 result = adapter.validate_python(input_value)
486
487 assert adapter.dump_python(result, mode="json", by_alias=True) == input_value
488
489 invalid_value = {"child_one": "not-an-object"}
490 if nested:
491 invalid_value = {"payload": invalid_value}
492 with pytest.raises(ValidationError):
493 adapter.validate_python(invalid_value)
494
495
496 @pytest.mark.parametrize(
497 "case,index",
498 [
499 (case, index)
500 for case in load_object_cases()
501 for index in range(len(case.instances))
502 ],
503 ids=[
504 f"{case.id}[{index}]"
505 for case in load_object_cases()
506 for index in range(len(case.instances))
507 ],
508 )
509 def test_shared_object_corpus_through_json_schema_to_pydantic_type(case, index):
510 """`json_schema_to_pydantic_type` must agree with `json_schema_to_model`.
511
512 The two entry points return different things — a Pydantic type versus a
513 `BaseModel` subclass — so the type is exercised through a `TypeAdapter` and
514 compared as a JSON-shaped value.
515 """
516 instance = case.instances[index]
517 adapter = TypeAdapter(json_schema_to_pydantic_type(case.schema_))
518
519 if not instance.accepted_for("python"):
520 with pytest.raises(ValidationError):
521 adapter.validate_python(instance.input)
522 return
523
524 result = adapter.validate_python(instance.input)
525 if instance.python is not None and instance.python.has_output:
526 assert (
527 adapter.dump_python(result, mode="json", by_alias=True)
528 == instance.python.output
529 )