@devries-cameron20 / composio Mirror · read-only / commits / d256568cb
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 22, 19:30 UTC

Format unformated files

haxzie committed Sep 29, 2025 at 16:50 UTC d256568cb5b08fccf1056f1a20a3abd2eed82dd5
2 files changed +77 -153
python/composio/utils/shared.py
+1 -6
@@ -79,7 +79,6 @@ def json_schema_to_pydantic_type(
79 return nested_model
80 return t.Dict
81
82 -
82 pytype = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_)
83 if pytype is not None:
84 return pytype
@@ -124,11 +123,7 @@ def json_schema_to_pydantic_field(
123 "alias": alias,
124 }
125 if not skip_default:
127 - field["default"] = (
128 - ...
129 - if name in required
130 - else default
131 - )
126 + field["default"] = ... if name in required else default
127
128 return (
129 name,
python/tests/test_schema_parser.py
+76 -147
@@ -5,7 +5,6 @@ This module tests the core schema parsing functionality in composio.utils.shared
5 particularly focusing on the required field propagation bug that was fixed.
6 """
7
8 -
8 import pytest
9 from pydantic import BaseModel
10 from pydantic.fields import PydanticUndefined
@@ -30,7 +29,7 @@ class TestJsonSchemaToPydanticField:
29 json_schema = {
30 "type": "string",
31 "description": "A test field",
33 - "title": "Test Field"
32 + "title": "Test Field",
33 }
34 required = ["test_field"]
35
@@ -49,7 +48,7 @@ class TestJsonSchemaToPydanticField:
48 "type": "string",
49 "description": "An optional field",
50 "title": "Optional Field",
52 - "default": "default_value"
51 + "default": "default_value",
52 }
53 required = []
54
@@ -67,20 +66,15 @@ class TestJsonSchemaToPydanticField:
66 """
67 CRITICAL TEST: Ensure nested object's internal required array
68 does NOT make the parent object required.
70 -
69 +
70 This tests the specific bug that was fixed.
71 """
72 name = "nested_object"
73 json_schema = {
74 "type": "object",
75 "title": "NestedObject",
77 - "properties": {
78 - "inner_field": {
79 - "type": "string",
80 - "title": "Inner Field"
81 - }
82 - },
83 - "required": ["inner_field"] # This should NOT make nested_object required
76 + "properties": {"inner_field": {"type": "string", "title": "Inner Field"}},
77 + "required": ["inner_field"], # This should NOT make nested_object required
78 }
79 required = [] # nested_object is not in parent's required list
80
@@ -98,13 +92,8 @@ class TestJsonSchemaToPydanticField:
92 json_schema = {
93 "type": "object",
94 "title": "NestedObject",
101 - "properties": {
102 - "inner_field": {
103 - "type": "string",
104 - "title": "Inner Field"
105 - }
106 - },
107 - "required": ["inner_field"]
95 + "properties": {"inner_field": {"type": "string", "title": "Inner Field"}},
96 + "required": ["inner_field"],
97 }
98 required = ["nested_object"] # Explicitly in parent's required list
99
@@ -121,7 +110,7 @@ class TestJsonSchemaToPydanticField:
110 json_schema = {
111 "type": "string",
112 "description": "A field with reserved name",
124 - "title": "Validate"
113 + "title": "Validate",
114 }
115 required = []
116
@@ -139,7 +128,7 @@ class TestJsonSchemaToPydanticField:
128 "type": "string",
129 "description": "A field with examples",
130 "title": "Example Field",
142 - "examples": ["example1", "example2"]
131 + "examples": ["example1", "example2"],
132 }
133 required = []
134
@@ -156,7 +145,7 @@ class TestJsonSchemaToPydanticField:
145 json_schema = {
146 "oneOf": [
147 {"type": "string", "description": "String option"},
159 - {"type": "integer", "description": "Integer option"}
148 + {"type": "integer", "description": "Integer option"},
149 ]
150 }
151 required = []
@@ -171,10 +160,7 @@ class TestJsonSchemaToPydanticField:
160 def test_skip_default_parameter(self):
161 """Test that skip_default parameter works correctly."""
162 name = "test_field"
174 - json_schema = {
175 - "type": "string",
176 - "default": "should_be_skipped"
177 - }
163 + json_schema = {"type": "string", "default": "should_be_skipped"}
164 required = []
165
166 field_name, field_type, field_info = json_schema_to_pydantic_field(
@@ -194,16 +180,10 @@ class TestJsonSchemaToModel:
180 "title": "SimpleModel",
181 "type": "object",
182 "properties": {
197 - "name": {
198 - "type": "string",
199 - "title": "Name"
200 - },
201 - "age": {
202 - "type": "integer",
203 - "title": "Age"
204 - }
183 + "name": {"type": "string", "title": "Name"},
184 + "age": {"type": "integer", "title": "Age"},
185 },
206 - "required": ["name"]
186 + "required": ["name"],
187 }
188
189 model_class = json_schema_to_model(json_schema)
@@ -223,23 +203,17 @@ class TestJsonSchemaToModel:
203 "title": "ParentModel",
204 "type": "object",
205 "properties": {
226 - "basic_field": {
227 - "type": "string",
228 - "title": "Basic Field"
229 - },
206 + "basic_field": {"type": "string", "title": "Basic Field"},
207 "nested_object": {
208 "type": "object",
209 "title": "NestedObject",
210 "properties": {
234 - "inner_field": {
235 - "type": "string",
236 - "title": "Inner Field"
237 - }
211 + "inner_field": {"type": "string", "title": "Inner Field"}
212 },
239 - "required": ["inner_field"]
240 - }
213 + "required": ["inner_field"],
214 + },
215 },
242 - "required": ["basic_field"]
216 + "required": ["basic_field"],
217 }
218
219 model_class = json_schema_to_model(json_schema)
@@ -251,8 +225,7 @@ class TestJsonSchemaToModel:
225
226 # Test that nested object validation works when provided
227 instance_with_nested = model_class(
254 - basic_field="test",
255 - nested_object={"inner_field": "nested_value"}
228 + basic_field="test", nested_object={"inner_field": "nested_value"}
229 )
230 assert instance_with_nested.nested_object.inner_field == "nested_value"
231
@@ -261,7 +234,7 @@ class TestJsonSchemaToModel:
234 def test_working_location_properties_bug_scenario(self):
235 """
236 CRITICAL TEST: Reproduce the exact scenario that caused the bug.
264 -
237 +
238 This tests the workingLocationProperties scenario that was incorrectly
239 marked as required.
240 """
@@ -269,10 +242,7 @@ class TestJsonSchemaToModel:
242 "title": "CreateEventRequest",
243 "type": "object",
244 "properties": {
272 - "start_datetime": {
273 - "type": "string",
274 - "title": "Start Datetime"
275 - },
245 + "start_datetime": {"type": "string", "title": "Start Datetime"},
246 "workingLocationProperties": {
247 "type": "object",
248 "title": "WorkingLocationProperties",
@@ -280,24 +250,21 @@ class TestJsonSchemaToModel:
250 "type": {
251 "type": "string",
252 "title": "Type",
283 - "enum": ["homeOffice", "officeLocation", "customLocation"]
253 + "enum": ["homeOffice", "officeLocation", "customLocation"],
254 },
255 "customLocation": {
256 "type": "object",
257 "title": "WorkingLocationCustom",
258 "properties": {
289 - "label": {
290 - "type": "string",
291 - "title": "Label"
292 - }
259 + "label": {"type": "string", "title": "Label"}
260 },
294 - "required": ["label"]
295 - }
261 + "required": ["label"],
262 + },
263 },
297 - "required": ["type"]
298 - }
264 + "required": ["type"],
265 + },
266 },
300 - "required": ["start_datetime"]
267 + "required": ["start_datetime"],
268 }
269
270 model_class = json_schema_to_model(json_schema)
@@ -312,10 +279,13 @@ class TestJsonSchemaToModel:
279 start_datetime="2025-01-01T10:00:00",
280 workingLocationProperties={
281 "type": "customLocation",
315 - "customLocation": {"label": "Client Office"}
316 - }
282 + "customLocation": {"label": "Client Office"},
283 + },
284 + )
285 + assert (
286 + instance_with_working_location.workingLocationProperties.type
287 + == "customLocation"
288 )
318 - assert instance_with_working_location.workingLocationProperties.type == "customLocation"
289
290 def test_array_type_handling(self):
291 """Test handling of array types in schema."""
@@ -323,14 +293,8 @@ class TestJsonSchemaToModel:
293 "title": "ArrayModel",
294 "type": "object",
295 "properties": {
326 - "tags": {
327 - "type": "array",
328 - "items": {
329 - "type": "string"
330 - },
331 - "title": "Tags"
332 - }
333 - }
296 + "tags": {"type": "array", "items": {"type": "string"}, "title": "Tags"}
297 + },
298 }
299
300 model_class = json_schema_to_model(json_schema)
@@ -346,17 +310,12 @@ class TestPydanticModelFromParamSchema:
310 param_schema = {
311 "title": "SimpleParam",
312 "type": "object",
349 - "properties": {
350 - "name": {
351 - "type": "string",
352 - "title": "Name"
353 - }
354 - },
355 - "required": ["name"]
313 + "properties": {"name": {"type": "string", "title": "Name"}},
314 + "required": ["name"],
315 }
316
317 model_class = pydantic_model_from_param_schema(param_schema)
359 -
318 +
319 # Should be able to create instance with required field
320 instance = model_class(name="test")
321 assert instance.name == "test"
@@ -370,23 +329,19 @@ class TestPydanticModelFromParamSchema:
329 "title": "ParentParam",
330 "type": "object",
331 "properties": {
373 - "required_field": {
374 - "type": "string",
375 - "title": "Required Field"
376 - },
332 + "required_field": {"type": "string", "title": "Required Field"},
333 "optional_nested": {
334 "type": "object",
335 "title": "Optional Nested",
336 "properties": {
381 - "inner_required": {
382 - "type": "string",
383 - "title": "Inner Required"
384 - }
337 + "inner_required": {"type": "string", "title": "Inner Required"}
338 },
386 - "required": ["inner_required"] # Should NOT make optional_nested required
387 - }
339 + "required": [
340 + "inner_required"
341 + ], # Should NOT make optional_nested required
342 + },
343 },
389 - "required": ["required_field"]
344 + "required": ["required_field"],
345 }
346
347 model_class = pydantic_model_from_param_schema(param_schema)
@@ -401,22 +356,19 @@ class TestPydanticModelFromParamSchema:
356 param_schema = {
357 "title": "ArrayParam",
358 "type": "array",
404 - "items": {
405 - "type": "string",
406 - "title": "String Item"
407 - }
359 + "items": {"type": "string", "title": "String Item"},
360 }
361
362 result = pydantic_model_from_param_schema(param_schema)
363 # Should return List[str] type
412 - assert hasattr(result, '__origin__') # Generic type
364 + assert hasattr(result, "__origin__") # Generic type
365 assert result.__origin__ is list
366
367 def test_missing_title_error(self):
368 """Test that missing title raises appropriate error."""
369 param_schema = {
370 "type": "object",
419 - "properties": {}
371 + "properties": {},
372 # Missing "title"
373 }
374
@@ -442,13 +394,10 @@ class TestJsonSchemaToPydanticType:
394
395 def test_array_type(self):
396 """Test array type conversion."""
445 - json_schema = {
446 - "type": "array",
447 - "items": {"type": "string"}
448 - }
397 + json_schema = {"type": "array", "items": {"type": "string"}}
398
399 result = json_schema_to_pydantic_type(json_schema)
451 - assert hasattr(result, '__origin__')
400 + assert hasattr(result, "__origin__")
401 assert result.__origin__ is list
402
403 def test_object_type_creates_nested_model(self):
@@ -456,9 +405,7 @@ class TestJsonSchemaToPydanticType:
405 json_schema = {
406 "type": "object",
407 "title": "NestedModel",
459 - "properties": {
460 - "field": {"type": "string", "title": "Field"}
461 - }
408 + "properties": {"field": {"type": "string", "title": "Field"}},
409 }
410
411 result = json_schema_to_pydantic_type(json_schema)
@@ -467,16 +414,11 @@ class TestJsonSchemaToPydanticType:
414
415 def test_oneof_union_types(self):
416 """Test oneOf schemas create union types."""
470 - json_schema = {
471 - "oneOf": [
472 - {"type": "string"},
473 - {"type": "integer"}
474 - ]
475 - }
417 + json_schema = {"oneOf": [{"type": "string"}, {"type": "integer"}]}
418
419 result = json_schema_to_pydantic_type(json_schema)
420 # Should create a Union type
479 - assert hasattr(result, '__origin__')
421 + assert hasattr(result, "__origin__")
422
423 def test_fallback_to_string(self):
424 """Test that missing type defaults to string."""
@@ -500,16 +442,10 @@ class TestJsonSchemaToFieldsDict:
442 """Test creating fields dictionary from JSON schema."""
443 json_schema = {
444 "properties": {
503 - "name": {
504 - "type": "string",
505 - "title": "Name"
506 - },
507 - "age": {
508 - "type": "integer",
509 - "title": "Age"
510 - }
445 + "name": {"type": "string", "title": "Name"},
446 + "age": {"type": "integer", "title": "Age"},
447 },
512 - "required": ["name"]
448 + "required": ["name"],
449 }
450
451 fields_dict = json_schema_to_fields_dict(json_schema)
@@ -550,19 +486,19 @@ class TestRegressionScenarios:
486 "properties": {
487 "deep_field": {
488 "type": "string",
553 - "title": "Deep Field"
489 + "title": "Deep Field",
490 }
491 },
556 - "required": ["deep_field"]
492 + "required": ["deep_field"],
493 }
494 },
559 - "required": ["level3"]
495 + "required": ["level3"],
496 }
497 },
562 - "required": ["level2"]
498 + "required": ["level2"],
499 }
500 },
565 - "required": [] # level1 should NOT be required
501 + "required": [], # level1 should NOT be required
502 }
503
504 model_class = json_schema_to_model(json_schema)
@@ -580,25 +516,18 @@ class TestRegressionScenarios:
516 "config1": {
517 "type": "object",
518 "title": "Config1",
583 - "properties": {
584 - "setting1": {"type": "string", "title": "Setting1"}
585 - },
586 - "required": ["setting1"]
519 + "properties": {"setting1": {"type": "string", "title": "Setting1"}},
520 + "required": ["setting1"],
521 },
522 "config2": {
523 "type": "object",
524 "title": "Config2",
591 - "properties": {
592 - "setting2": {"type": "string", "title": "Setting2"}
593 - },
594 - "required": ["setting2"]
525 + "properties": {"setting2": {"type": "string", "title": "Setting2"}},
526 + "required": ["setting2"],
527 },
596 - "required_field": {
597 - "type": "string",
598 - "title": "Required Field"
599 - }
528 + "required_field": {"type": "string", "title": "Required Field"},
529 },
601 - "required": ["required_field"]
530 + "required": ["required_field"],
531 }
532
533 model_class = json_schema_to_model(json_schema)
@@ -616,9 +545,9 @@ class TestRegressionScenarios:
545 "type": "object",
546 "properties": {
547 "optional1": {"type": "string", "title": "Optional1"},
619 - "optional2": {"type": "string", "title": "Optional2"}
548 + "optional2": {"type": "string", "title": "Optional2"},
549 },
621 - "required": []
550 + "required": [],
551 }
552
553 model_class = json_schema_to_model(json_schema)
@@ -635,8 +564,8 @@ class TestRegressionScenarios:
564 "type": "object",
565 "properties": {
566 "field1": {"type": "string", "title": "Field1"},
638 - "field2": {"type": "string", "title": "Field2"}
639 - }
567 + "field2": {"type": "string", "title": "Field2"},
568 + },
569 # No "required" key at all
570 }
571
@@ -685,9 +614,9 @@ class TestEdgeCases:
614 "name": {"type": "string", "title": "Name"},
615 "children": {
616 "type": "array",
688 - "items": {"$ref": "#"} # Self-reference
689 - }
690 - }
617 + "items": {"$ref": "#"}, # Self-reference
618 + },
619 + },
620 }
621
622 # This might not work perfectly but shouldn't crash