Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Fix tests
haxzie committed
Sep 29, 2025 at 16:23 UTC
5ff9d16b29e16b7b2da3e2eea83c4c1b616c3cb2
5 files changed
+847
-20
python/composio/core/models/mcp.py
+2
-2
@@ -9,7 +9,7 @@ from __future__ import annotations
9
import typing as t
10
11
import typing_extensions as te
12
-from composio_client import omit
12
+from composio_client import Omit
13
from composio_client.types.mcp.custom_create_response import CustomCreateResponse
14
15
from composio.client import HttpClient
@@ -183,7 +183,7 @@ class MCP(Resource):
183
auth_config_ids.append(toolkit_config["auth_config_id"])
184
185
# Use the allowed_tools parameter instead of individual toolkit configs
186
- custom_tools = allowed_tools if allowed_tools is not None else omit
186
+ custom_tools = allowed_tools if allowed_tools is not None else Omit
187
188
# Use the custom MCP create endpoint
189
response = self._client.mcp.custom.create(
python/composio/utils/shared.py
+20
-18
@@ -50,6 +50,26 @@ def json_schema_to_pydantic_type(
50
:param json_schema: The JSON schema to convert.
51
:return: A Pydantic type.
52
"""
53
+ # Handle oneOf schemas first
54
+ if "oneOf" in json_schema:
55
+ one_of_options = json_schema["oneOf"]
56
+ pydantic_types: t.List[t.Type] = [ # type: ignore
57
+ json_schema_to_pydantic_type(option) for option in one_of_options
58
+ ]
59
+ if len(pydantic_types) == 1:
60
+ return pydantic_types[0]
61
+ if len(pydantic_types) == 2:
62
+ return t.Union[
63
+ t.cast(t.Type, pydantic_types[0]), t.cast(t.Type, pydantic_types[1])
64
+ ]
65
+ if len(pydantic_types) == 3:
66
+ return t.Union[
67
+ t.cast(t.Type, pydantic_types[0]),
68
+ t.cast(t.Type, pydantic_types[1]),
69
+ t.cast(t.Type, pydantic_types[2]),
70
+ ]
71
+ raise ValueError("Invalid 'oneOf' schema")
72
+
73
# Add fallback type - string
74
if "type" not in json_schema:
75
json_schema["type"] = "string"
@@ -68,24 +88,6 @@ def json_schema_to_pydantic_type(
88
return nested_model
89
return t.Dict
90
71
- if type_ is None and "oneOf" in json_schema:
72
- one_of_options = json_schema["oneOf"]
73
- pydantic_types: t.List[t.Type] = [ # type: ignore
74
- json_schema_to_pydantic_type(option) for option in one_of_options
75
- ]
76
- if len(pydantic_types) == 1:
77
- return pydantic_types[0]
78
- if len(pydantic_types) == 2:
79
- return t.Union[
80
- t.cast(t.Type, pydantic_types[0]), t.cast(t.Type, pydantic_types[1])
81
- ]
82
- if len(pydantic_types) == 3:
83
- return t.Union[
84
- t.cast(t.Type, pydantic_types[0]),
85
- t.cast(t.Type, pydantic_types[1]),
86
- t.cast(t.Type, pydantic_types[2]),
87
- ]
88
- raise ValueError("Invalid 'oneOf' schema")
91
92
pytype = PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_)
93
if pytype is not None:
python/pytest.ini
new
+11
@@ -0,0 +1,11 @@
1
+[pytest]
2
+testpaths = tests
3
+python_files = test_*.py
4
+python_classes = Test*
5
+python_functions = test_*
6
+addopts = -v --tb=short
7
+markers =
8
+ slow: marks tests as slow (deselect with '-m "not slow"')
9
+ integration: marks tests as integration tests
10
+ unit: marks tests as unit tests
11
+ schema: marks tests as schema-related tests
python/tests/test_schema_parser.py
new
+707
@@ -0,0 +1,707 @@
1
+"""
2
+Comprehensive test suite for JSON schema to Pydantic conversion functions.
3
+
4
+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
+import json
9
+import typing as t
10
+
11
+import pytest
12
+from pydantic import BaseModel
13
+from pydantic.fields import PydanticUndefined
14
+
15
+from composio.utils.shared import (
16
+ json_schema_to_fields_dict,
17
+ json_schema_to_model,
18
+ json_schema_to_pydantic_field,
19
+ json_schema_to_pydantic_type,
20
+ pydantic_model_from_param_schema,
21
+)
22
+
23
+
24
+class TestJsonSchemaToPydanticField:
25
+ """Test cases for json_schema_to_pydantic_field function."""
26
+
27
+ @pytest.mark.unit
28
+ @pytest.mark.schema
29
+ def test_simple_required_field(self):
30
+ """Test that a field in the required list is marked as required."""
31
+ name = "test_field"
32
+ json_schema = {
33
+ "type": "string",
34
+ "description": "A test field",
35
+ "title": "Test Field"
36
+ }
37
+ required = ["test_field"]
38
+
39
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
40
+ name, json_schema, required
41
+ )
42
+
43
+ assert field_name == "test_field"
44
+ assert field_type == str
45
+ assert field_info.default is PydanticUndefined # Required field marker
46
+
47
+ def test_simple_optional_field(self):
48
+ """Test that a field not in the required list is marked as optional."""
49
+ name = "optional_field"
50
+ json_schema = {
51
+ "type": "string",
52
+ "description": "An optional field",
53
+ "title": "Optional Field",
54
+ "default": "default_value"
55
+ }
56
+ required = []
57
+
58
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
59
+ name, json_schema, required
60
+ )
61
+
62
+ assert field_name == "optional_field"
63
+ assert field_type == str
64
+ assert field_info.default == "default_value"
65
+
66
+ @pytest.mark.unit
67
+ @pytest.mark.schema
68
+ def test_nested_object_with_internal_required_not_propagated(self):
69
+ """
70
+ CRITICAL TEST: Ensure nested object's internal required array
71
+ does NOT make the parent object required.
72
+
73
+ This tests the specific bug that was fixed.
74
+ """
75
+ name = "nested_object"
76
+ json_schema = {
77
+ "type": "object",
78
+ "title": "NestedObject",
79
+ "properties": {
80
+ "inner_field": {
81
+ "type": "string",
82
+ "title": "Inner Field"
83
+ }
84
+ },
85
+ "required": ["inner_field"] # This should NOT make nested_object required
86
+ }
87
+ required = [] # nested_object is not in parent's required list
88
+
89
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
90
+ name, json_schema, required
91
+ )
92
+
93
+ assert field_name == "nested_object"
94
+ assert field_info.default is not PydanticUndefined # Should NOT be required
95
+ assert field_info.default is None # Should have default value
96
+
97
+ def test_nested_object_explicitly_required(self):
98
+ """Test that a nested object can be explicitly required via parent's required list."""
99
+ name = "nested_object"
100
+ json_schema = {
101
+ "type": "object",
102
+ "title": "NestedObject",
103
+ "properties": {
104
+ "inner_field": {
105
+ "type": "string",
106
+ "title": "Inner Field"
107
+ }
108
+ },
109
+ "required": ["inner_field"]
110
+ }
111
+ required = ["nested_object"] # Explicitly in parent's required list
112
+
113
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
114
+ name, json_schema, required
115
+ )
116
+
117
+ assert field_name == "nested_object"
118
+ assert field_info.default is PydanticUndefined # Should be required
119
+
120
+ def test_reserved_field_name_handling(self):
121
+ """Test that reserved Pydantic field names are properly aliased."""
122
+ name = "validate" # Reserved name
123
+ json_schema = {
124
+ "type": "string",
125
+ "description": "A field with reserved name",
126
+ "title": "Validate"
127
+ }
128
+ required = []
129
+
130
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
131
+ name, json_schema, required
132
+ )
133
+
134
+ assert field_name == "validate_" # Should be renamed
135
+ assert field_info.alias == "validate_" # Should have alias
136
+
137
+ def test_field_with_examples(self):
138
+ """Test that examples are properly preserved in field info."""
139
+ name = "example_field"
140
+ json_schema = {
141
+ "type": "string",
142
+ "description": "A field with examples",
143
+ "title": "Example Field",
144
+ "examples": ["example1", "example2"]
145
+ }
146
+ required = []
147
+
148
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
149
+ name, json_schema, required
150
+ )
151
+
152
+ assert field_name == "example_field"
153
+ assert field_info.examples == ["example1", "example2"]
154
+
155
+ def test_oneof_field_description_merging(self):
156
+ """Test that oneOf schemas have their descriptions properly merged."""
157
+ name = "oneof_field"
158
+ json_schema = {
159
+ "oneOf": [
160
+ {"type": "string", "description": "String option"},
161
+ {"type": "integer", "description": "Integer option"}
162
+ ]
163
+ }
164
+ required = []
165
+
166
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
167
+ name, json_schema, required
168
+ )
169
+
170
+ expected_desc = "Any of the following options(separated by |): String option | Integer option"
171
+ assert field_info.description == expected_desc
172
+
173
+ def test_skip_default_parameter(self):
174
+ """Test that skip_default parameter works correctly."""
175
+ name = "test_field"
176
+ json_schema = {
177
+ "type": "string",
178
+ "default": "should_be_skipped"
179
+ }
180
+ required = []
181
+
182
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
183
+ name, json_schema, required, skip_default=True
184
+ )
185
+
186
+ # When skip_default=True, field should be required (default=PydanticUndefined)
187
+ assert field_info.default is PydanticUndefined
188
+
189
+
190
+class TestJsonSchemaToModel:
191
+ """Test cases for json_schema_to_model function."""
192
+
193
+ def test_simple_model_creation(self):
194
+ """Test creating a simple Pydantic model from JSON schema."""
195
+ json_schema = {
196
+ "title": "SimpleModel",
197
+ "type": "object",
198
+ "properties": {
199
+ "name": {
200
+ "type": "string",
201
+ "title": "Name"
202
+ },
203
+ "age": {
204
+ "type": "integer",
205
+ "title": "Age"
206
+ }
207
+ },
208
+ "required": ["name"]
209
+ }
210
+
211
+ model_class = json_schema_to_model(json_schema)
212
+
213
+ # Test model creation
214
+ instance = model_class(name="test")
215
+ assert instance.name == "test"
216
+ assert instance.age is None
217
+
218
+ # Test validation
219
+ with pytest.raises(Exception): # Should fail without required field
220
+ model_class()
221
+
222
+ def test_nested_object_model(self):
223
+ """Test creating a model with nested objects."""
224
+ json_schema = {
225
+ "title": "ParentModel",
226
+ "type": "object",
227
+ "properties": {
228
+ "basic_field": {
229
+ "type": "string",
230
+ "title": "Basic Field"
231
+ },
232
+ "nested_object": {
233
+ "type": "object",
234
+ "title": "NestedObject",
235
+ "properties": {
236
+ "inner_field": {
237
+ "type": "string",
238
+ "title": "Inner Field"
239
+ }
240
+ },
241
+ "required": ["inner_field"]
242
+ }
243
+ },
244
+ "required": ["basic_field"]
245
+ }
246
+
247
+ model_class = json_schema_to_model(json_schema)
248
+
249
+ # Test that nested_object is optional (not required)
250
+ instance = model_class(basic_field="test")
251
+ assert instance.basic_field == "test"
252
+ assert instance.nested_object is None
253
+
254
+ # Test that nested object validation works when provided
255
+ instance_with_nested = model_class(
256
+ basic_field="test",
257
+ nested_object={"inner_field": "nested_value"}
258
+ )
259
+ assert instance_with_nested.nested_object.inner_field == "nested_value"
260
+
261
+ @pytest.mark.unit
262
+ @pytest.mark.schema
263
+ def test_working_location_properties_bug_scenario(self):
264
+ """
265
+ CRITICAL TEST: Reproduce the exact scenario that caused the bug.
266
+
267
+ This tests the workingLocationProperties scenario that was incorrectly
268
+ marked as required.
269
+ """
270
+ json_schema = {
271
+ "title": "CreateEventRequest",
272
+ "type": "object",
273
+ "properties": {
274
+ "start_datetime": {
275
+ "type": "string",
276
+ "title": "Start Datetime"
277
+ },
278
+ "workingLocationProperties": {
279
+ "type": "object",
280
+ "title": "WorkingLocationProperties",
281
+ "properties": {
282
+ "type": {
283
+ "type": "string",
284
+ "title": "Type",
285
+ "enum": ["homeOffice", "officeLocation", "customLocation"]
286
+ },
287
+ "customLocation": {
288
+ "type": "object",
289
+ "title": "WorkingLocationCustom",
290
+ "properties": {
291
+ "label": {
292
+ "type": "string",
293
+ "title": "Label"
294
+ }
295
+ },
296
+ "required": ["label"]
297
+ }
298
+ },
299
+ "required": ["type"]
300
+ }
301
+ },
302
+ "required": ["start_datetime"]
303
+ }
304
+
305
+ model_class = json_schema_to_model(json_schema)
306
+
307
+ # Test that workingLocationProperties is NOT required
308
+ instance = model_class(start_datetime="2025-01-01T10:00:00")
309
+ assert instance.start_datetime == "2025-01-01T10:00:00"
310
+ assert instance.workingLocationProperties is None
311
+
312
+ # Test that nested validation works when provided
313
+ instance_with_working_location = model_class(
314
+ start_datetime="2025-01-01T10:00:00",
315
+ workingLocationProperties={
316
+ "type": "customLocation",
317
+ "customLocation": {"label": "Client Office"}
318
+ }
319
+ )
320
+ assert instance_with_working_location.workingLocationProperties.type == "customLocation"
321
+
322
+ def test_array_type_handling(self):
323
+ """Test handling of array types in schema."""
324
+ json_schema = {
325
+ "title": "ArrayModel",
326
+ "type": "object",
327
+ "properties": {
328
+ "tags": {
329
+ "type": "array",
330
+ "items": {
331
+ "type": "string"
332
+ },
333
+ "title": "Tags"
334
+ }
335
+ }
336
+ }
337
+
338
+ model_class = json_schema_to_model(json_schema)
339
+ instance = model_class(tags=["tag1", "tag2"])
340
+ assert instance.tags == ["tag1", "tag2"]
341
+
342
+
343
+class TestPydanticModelFromParamSchema:
344
+ """Test cases for pydantic_model_from_param_schema function."""
345
+
346
+ def test_simple_param_schema(self):
347
+ """Test creating a model from parameter schema format."""
348
+ param_schema = {
349
+ "title": "SimpleParam",
350
+ "type": "object",
351
+ "properties": {
352
+ "name": {
353
+ "type": "string",
354
+ "title": "Name"
355
+ }
356
+ },
357
+ "required": ["name"]
358
+ }
359
+
360
+ model_class = pydantic_model_from_param_schema(param_schema)
361
+
362
+ # Should be able to create instance with required field
363
+ instance = model_class(name="test")
364
+ assert instance.name == "test"
365
+
366
+ def test_nested_object_not_making_parent_required(self):
367
+ """
368
+ CRITICAL TEST: Ensure nested objects with internal required fields
369
+ don't make the parent object required in pydantic_model_from_param_schema.
370
+ """
371
+ param_schema = {
372
+ "title": "ParentParam",
373
+ "type": "object",
374
+ "properties": {
375
+ "required_field": {
376
+ "type": "string",
377
+ "title": "Required Field"
378
+ },
379
+ "optional_nested": {
380
+ "type": "object",
381
+ "title": "Optional Nested",
382
+ "properties": {
383
+ "inner_required": {
384
+ "type": "string",
385
+ "title": "Inner Required"
386
+ }
387
+ },
388
+ "required": ["inner_required"] # Should NOT make optional_nested required
389
+ }
390
+ },
391
+ "required": ["required_field"]
392
+ }
393
+
394
+ model_class = pydantic_model_from_param_schema(param_schema)
395
+
396
+ # Should work with just the required field
397
+ instance = model_class(required_field="test")
398
+ assert instance.required_field == "test"
399
+ # optional_nested should be optional (None or default value)
400
+
401
+ def test_array_type_param_schema(self):
402
+ """Test array type handling in parameter schema."""
403
+ param_schema = {
404
+ "title": "ArrayParam",
405
+ "type": "array",
406
+ "items": {
407
+ "type": "string",
408
+ "title": "String Item"
409
+ }
410
+ }
411
+
412
+ result = pydantic_model_from_param_schema(param_schema)
413
+ # Should return List[str] type
414
+ assert hasattr(result, '__origin__') # Generic type
415
+ assert result.__origin__ is list
416
+
417
+ def test_missing_title_error(self):
418
+ """Test that missing title raises appropriate error."""
419
+ param_schema = {
420
+ "type": "object",
421
+ "properties": {}
422
+ # Missing "title"
423
+ }
424
+
425
+ with pytest.raises(ValueError, match="Missing 'title' in param_schema"):
426
+ pydantic_model_from_param_schema(param_schema)
427
+
428
+
429
+class TestJsonSchemaToPydanticType:
430
+ """Test cases for json_schema_to_pydantic_type function."""
431
+
432
+ def test_basic_types(self):
433
+ """Test conversion of basic JSON schema types to Python types."""
434
+ test_cases = [
435
+ ({"type": "string"}, str),
436
+ ({"type": "integer"}, int),
437
+ ({"type": "number"}, float),
438
+ ({"type": "boolean"}, bool),
439
+ ]
440
+
441
+ for json_schema, expected_type in test_cases:
442
+ result = json_schema_to_pydantic_type(json_schema)
443
+ assert result == expected_type
444
+
445
+ def test_array_type(self):
446
+ """Test array type conversion."""
447
+ json_schema = {
448
+ "type": "array",
449
+ "items": {"type": "string"}
450
+ }
451
+
452
+ result = json_schema_to_pydantic_type(json_schema)
453
+ assert hasattr(result, '__origin__')
454
+ assert result.__origin__ is list
455
+
456
+ def test_object_type_creates_nested_model(self):
457
+ """Test that object types create nested Pydantic models."""
458
+ json_schema = {
459
+ "type": "object",
460
+ "title": "NestedModel",
461
+ "properties": {
462
+ "field": {"type": "string", "title": "Field"}
463
+ }
464
+ }
465
+
466
+ result = json_schema_to_pydantic_type(json_schema)
467
+ assert isinstance(result, type)
468
+ assert issubclass(result, BaseModel)
469
+
470
+ def test_oneof_union_types(self):
471
+ """Test oneOf schemas create union types."""
472
+ json_schema = {
473
+ "oneOf": [
474
+ {"type": "string"},
475
+ {"type": "integer"}
476
+ ]
477
+ }
478
+
479
+ result = json_schema_to_pydantic_type(json_schema)
480
+ # Should create a Union type
481
+ assert hasattr(result, '__origin__')
482
+
483
+ def test_fallback_to_string(self):
484
+ """Test that missing type defaults to string."""
485
+ json_schema = {} # No type specified
486
+
487
+ result = json_schema_to_pydantic_type(json_schema)
488
+ assert result == str
489
+
490
+ def test_unsupported_type_error(self):
491
+ """Test that unsupported types raise appropriate error."""
492
+ json_schema = {"type": "unsupported_type"}
493
+
494
+ with pytest.raises(ValueError, match="Unsupported JSON schema type"):
495
+ json_schema_to_pydantic_type(json_schema)
496
+
497
+
498
+class TestJsonSchemaToFieldsDict:
499
+ """Test cases for json_schema_to_fields_dict function."""
500
+
501
+ def test_basic_fields_dict(self):
502
+ """Test creating fields dictionary from JSON schema."""
503
+ json_schema = {
504
+ "properties": {
505
+ "name": {
506
+ "type": "string",
507
+ "title": "Name"
508
+ },
509
+ "age": {
510
+ "type": "integer",
511
+ "title": "Age"
512
+ }
513
+ },
514
+ "required": ["name"]
515
+ }
516
+
517
+ fields_dict = json_schema_to_fields_dict(json_schema)
518
+
519
+ assert "name" in fields_dict
520
+ assert "age" in fields_dict
521
+
522
+ # Check field types and info
523
+ name_type, name_field = fields_dict["name"]
524
+ age_type, age_field = fields_dict["age"]
525
+
526
+ assert name_type == str
527
+ assert age_type == int
528
+ assert name_field.default is PydanticUndefined # Required
529
+ assert age_field.default is None # Optional
530
+
531
+
532
+class TestRegressionScenarios:
533
+ """Test cases for specific regression scenarios and edge cases."""
534
+
535
+ def test_deeply_nested_objects_required_propagation(self):
536
+ """Test deeply nested objects don't propagate required fields incorrectly."""
537
+ json_schema = {
538
+ "title": "DeeplyNested",
539
+ "type": "object",
540
+ "properties": {
541
+ "level1": {
542
+ "type": "object",
543
+ "title": "Level1",
544
+ "properties": {
545
+ "level2": {
546
+ "type": "object",
547
+ "title": "Level2",
548
+ "properties": {
549
+ "level3": {
550
+ "type": "object",
551
+ "title": "Level3",
552
+ "properties": {
553
+ "deep_field": {
554
+ "type": "string",
555
+ "title": "Deep Field"
556
+ }
557
+ },
558
+ "required": ["deep_field"]
559
+ }
560
+ },
561
+ "required": ["level3"]
562
+ }
563
+ },
564
+ "required": ["level2"]
565
+ }
566
+ },
567
+ "required": [] # level1 should NOT be required
568
+ }
569
+
570
+ model_class = json_schema_to_model(json_schema)
571
+
572
+ # Should be able to create instance without level1
573
+ instance = model_class()
574
+ assert instance.level1 is None
575
+
576
+ def test_multiple_nested_objects_same_level(self):
577
+ """Test multiple nested objects at same level with different required fields."""
578
+ json_schema = {
579
+ "title": "MultipleNested",
580
+ "type": "object",
581
+ "properties": {
582
+ "config1": {
583
+ "type": "object",
584
+ "title": "Config1",
585
+ "properties": {
586
+ "setting1": {"type": "string", "title": "Setting1"}
587
+ },
588
+ "required": ["setting1"]
589
+ },
590
+ "config2": {
591
+ "type": "object",
592
+ "title": "Config2",
593
+ "properties": {
594
+ "setting2": {"type": "string", "title": "Setting2"}
595
+ },
596
+ "required": ["setting2"]
597
+ },
598
+ "required_field": {
599
+ "type": "string",
600
+ "title": "Required Field"
601
+ }
602
+ },
603
+ "required": ["required_field"]
604
+ }
605
+
606
+ model_class = json_schema_to_model(json_schema)
607
+
608
+ # Should work with just required_field
609
+ instance = model_class(required_field="test")
610
+ assert instance.required_field == "test"
611
+ assert instance.config1 is None
612
+ assert instance.config2 is None
613
+
614
+ def test_empty_required_array_handling(self):
615
+ """Test handling of empty required arrays."""
616
+ json_schema = {
617
+ "title": "EmptyRequired",
618
+ "type": "object",
619
+ "properties": {
620
+ "optional1": {"type": "string", "title": "Optional1"},
621
+ "optional2": {"type": "string", "title": "Optional2"}
622
+ },
623
+ "required": []
624
+ }
625
+
626
+ model_class = json_schema_to_model(json_schema)
627
+
628
+ # Should work with no fields
629
+ instance = model_class()
630
+ assert instance.optional1 is None
631
+ assert instance.optional2 is None
632
+
633
+ def test_missing_required_array_handling(self):
634
+ """Test handling when required array is missing entirely."""
635
+ json_schema = {
636
+ "title": "NoRequired",
637
+ "type": "object",
638
+ "properties": {
639
+ "field1": {"type": "string", "title": "Field1"},
640
+ "field2": {"type": "string", "title": "Field2"}
641
+ }
642
+ # No "required" key at all
643
+ }
644
+
645
+ model_class = json_schema_to_model(json_schema)
646
+
647
+ # Should work with no fields (all optional)
648
+ instance = model_class()
649
+ assert instance.field1 is None
650
+ assert instance.field2 is None
651
+
652
+
653
+class TestEdgeCases:
654
+ """Test cases for edge cases and error conditions."""
655
+
656
+ def test_none_schema_handling(self):
657
+ """Test handling of None or empty schemas."""
658
+ with pytest.raises((TypeError, AttributeError, KeyError)):
659
+ json_schema_to_model(None)
660
+
661
+ def test_malformed_schema_handling(self):
662
+ """Test handling of malformed schemas."""
663
+ malformed_schemas = [
664
+ {"type": "object"}, # Missing properties
665
+ {"properties": {}}, # Missing type and title
666
+ {"title": "Test", "type": "invalid_type"}, # Invalid type
667
+ ]
668
+
669
+ for schema in malformed_schemas:
670
+ # Should either handle gracefully or raise appropriate error
671
+ try:
672
+ result = json_schema_to_model(schema)
673
+ # If it doesn't raise an error, it should at least return something
674
+ assert result is not None
675
+ except (ValueError, KeyError, TypeError):
676
+ # These are acceptable errors for malformed schemas
677
+ pass
678
+
679
+ def test_circular_reference_protection(self):
680
+ """Test that circular references don't cause infinite recursion."""
681
+ # This is a complex scenario that would require special handling
682
+ # For now, we just ensure it doesn't crash
683
+ json_schema = {
684
+ "title": "SelfReference",
685
+ "type": "object",
686
+ "properties": {
687
+ "name": {"type": "string", "title": "Name"},
688
+ "children": {
689
+ "type": "array",
690
+ "items": {"$ref": "#"} # Self-reference
691
+ }
692
+ }
693
+ }
694
+
695
+ # This might not work perfectly but shouldn't crash
696
+ try:
697
+ model_class = json_schema_to_model(json_schema)
698
+ # If successful, test basic functionality
699
+ instance = model_class(name="test")
700
+ assert instance.name == "test"
701
+ except (RecursionError, ValueError):
702
+ # Acceptable for now - circular references are complex
703
+ pass
704
+
705
+
706
+if __name__ == "__main__":
707
+ pytest.main([__file__, "-v"])
python/tests/test_schema_parser_simple.py
new
+107
@@ -0,0 +1,107 @@
1
+#!/usr/bin/env python3
2
+"""
3
+Simple validation tests for schema parser functions.
4
+"""
5
+
6
+import sys
7
+import os
8
+sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
9
+
10
+from pydantic.fields import PydanticUndefined
11
+from composio.utils.shared import (
12
+ json_schema_to_model,
13
+ json_schema_to_pydantic_field,
14
+)
15
+
16
+
17
+def test_basic_functionality():
18
+ """Basic smoke test for schema parser functions."""
19
+ print("🧪 Testing basic schema parser functionality...")
20
+
21
+ # Test 1: Simple required field
22
+ name = "test_field"
23
+ json_schema = {
24
+ "type": "string",
25
+ "description": "A test field",
26
+ "title": "Test Field"
27
+ }
28
+ required = ["test_field"]
29
+
30
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
31
+ name, json_schema, required
32
+ )
33
+
34
+ assert field_name == "test_field"
35
+ assert field_type == str
36
+ assert field_info.default is PydanticUndefined # Required field marker
37
+ print("✅ Test 1 passed: Simple required field")
38
+
39
+ # Test 2: Optional field with nested required (the bug scenario)
40
+ name = "nested_object"
41
+ json_schema = {
42
+ "type": "object",
43
+ "title": "NestedObject",
44
+ "properties": {
45
+ "inner_field": {
46
+ "type": "string",
47
+ "title": "Inner Field"
48
+ }
49
+ },
50
+ "required": ["inner_field"] # This should NOT make nested_object required
51
+ }
52
+ required = [] # nested_object is not in parent's required list
53
+
54
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
55
+ name, json_schema, required
56
+ )
57
+
58
+ assert field_name == "nested_object"
59
+ assert field_info.default is not PydanticUndefined # Should NOT be required
60
+ print("✅ Test 2 passed: Nested object with internal required not propagated")
61
+
62
+ # Test 3: Full model creation (workingLocationProperties scenario)
63
+ json_schema = {
64
+ "title": "CreateEventRequest",
65
+ "type": "object",
66
+ "properties": {
67
+ "start_datetime": {
68
+ "type": "string",
69
+ "title": "Start Datetime"
70
+ },
71
+ "workingLocationProperties": {
72
+ "type": "object",
73
+ "title": "WorkingLocationProperties",
74
+ "properties": {
75
+ "type": {
76
+ "type": "string",
77
+ "title": "Type"
78
+ }
79
+ },
80
+ "required": ["type"] # Should NOT make workingLocationProperties required
81
+ }
82
+ },
83
+ "required": ["start_datetime"]
84
+ }
85
+
86
+ model_class = json_schema_to_model(json_schema)
87
+
88
+ # Test that workingLocationProperties is NOT required
89
+ instance = model_class(start_datetime="2025-01-01T10:00:00")
90
+ assert instance.start_datetime == "2025-01-01T10:00:00"
91
+ assert instance.workingLocationProperties is None
92
+ print("✅ Test 3 passed: workingLocationProperties bug scenario fixed")
93
+
94
+ print("\n🎉 All tests passed! Schema parser is working correctly.")
95
+ return True
96
+
97
+
98
+if __name__ == "__main__":
99
+ try:
100
+ test_basic_functionality()
101
+ print("\n✅ SUCCESS: All schema parser tests passed!")
102
+ exit(0)
103
+ except Exception as e:
104
+ print(f"\n❌ FAILED: Test failed with error: {e}")
105
+ import traceback
106
+ traceback.print_exc()
107
+ exit(1)