Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 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 typing as t |
| 9 | |
| 10 | import pytest |
| 11 | from pydantic import BaseModel, TypeAdapter, ValidationError |
| 12 | from pydantic.fields import PydanticUndefined |
| 13 | |
| 14 | from composio.utils.shared import ( |
| 15 | get_signature_format_from_schema_params, |
| 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 | from tests.fixtures.json_schema_conversion_corpus import find_case, load_object_cases |
| 23 | |
| 24 | |
| 25 | class TestJsonSchemaToPydanticField: |
| 26 | """Test cases for json_schema_to_pydantic_field function.""" |
| 27 | |
| 28 | @pytest.mark.unit |
| 29 | @pytest.mark.schema |
| 30 | def test_simple_required_field(self): |
| 31 | """Test that a field in the required list is marked as required.""" |
| 32 | name = "test_field" |
| 33 | json_schema = { |
| 34 | "type": "string", |
| 35 | "description": "A test field", |
| 36 | "title": "Test Field", |
| 37 | } |
| 38 | required = ["test_field"] |
| 39 | |
| 40 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 41 | name, json_schema, required |
| 42 | ) |
| 43 | |
| 44 | assert field_name == "test_field" |
| 45 | assert field_type is str |
| 46 | assert field_info.default is PydanticUndefined # Required field marker |
| 47 | |
| 48 | def test_simple_optional_field(self): |
| 49 | """Test that a field not in the required list is marked as optional.""" |
| 50 | name = "optional_field" |
| 51 | json_schema = { |
| 52 | "type": "string", |
| 53 | "description": "An optional field", |
| 54 | "title": "Optional Field", |
| 55 | "default": "default_value", |
| 56 | } |
| 57 | required = [] |
| 58 | |
| 59 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 60 | name, json_schema, required |
| 61 | ) |
| 62 | |
| 63 | assert field_name == "optional_field" |
| 64 | assert field_type is str |
| 65 | assert field_info.default == "default_value" |
| 66 | |
| 67 | @pytest.mark.unit |
| 68 | @pytest.mark.schema |
| 69 | def test_nested_object_with_internal_required_not_propagated(self): |
| 70 | """ |
| 71 | CRITICAL TEST: Ensure nested object's internal required array |
| 72 | does NOT make the parent object required. |
| 73 | |
| 74 | This tests the specific bug that was fixed. |
| 75 | """ |
| 76 | name = "nested_object" |
| 77 | json_schema = { |
| 78 | "type": "object", |
| 79 | "title": "NestedObject", |
| 80 | "properties": {"inner_field": {"type": "string", "title": "Inner Field"}}, |
| 81 | "required": ["inner_field"], # This should NOT make nested_object required |
| 82 | } |
| 83 | required = [] # nested_object is not in parent's required list |
| 84 | |
| 85 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 86 | name, json_schema, required |
| 87 | ) |
| 88 | |
| 89 | assert field_name == "nested_object" |
| 90 | assert field_info.default is not PydanticUndefined # Should NOT be required |
| 91 | assert field_info.default is None # Should have default value |
| 92 | |
| 93 | def test_nested_object_explicitly_required(self): |
| 94 | """Test that a nested object can be explicitly required via parent's required list.""" |
| 95 | name = "nested_object" |
| 96 | json_schema = { |
| 97 | "type": "object", |
| 98 | "title": "NestedObject", |
| 99 | "properties": {"inner_field": {"type": "string", "title": "Inner Field"}}, |
| 100 | "required": ["inner_field"], |
| 101 | } |
| 102 | required = ["nested_object"] # Explicitly in parent's required list |
| 103 | |
| 104 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 105 | name, json_schema, required |
| 106 | ) |
| 107 | |
| 108 | assert field_name == "nested_object" |
| 109 | assert field_info.default is PydanticUndefined # Should be required |
| 110 | |
| 111 | def test_reserved_field_name_handling(self): |
| 112 | """Test that reserved Pydantic field names are properly aliased.""" |
| 113 | name = "validate" # Reserved name |
| 114 | json_schema = { |
| 115 | "type": "string", |
| 116 | "description": "A field with reserved name", |
| 117 | "title": "Validate", |
| 118 | } |
| 119 | required = [] |
| 120 | |
| 121 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 122 | name, json_schema, required |
| 123 | ) |
| 124 | |
| 125 | assert field_name == "validate_" # Should be renamed |
| 126 | assert field_info.alias == "validate" # Should preserve original name |
| 127 | |
| 128 | def test_reserved_required_field_name_preserves_required_status(self): |
| 129 | """Test that reserved field names keep the original alias and required status.""" |
| 130 | name = "validate" |
| 131 | json_schema = { |
| 132 | "type": "string", |
| 133 | "description": "A required field with reserved name", |
| 134 | "title": "Validate", |
| 135 | } |
| 136 | required = ["validate"] |
| 137 | |
| 138 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 139 | name, json_schema, required |
| 140 | ) |
| 141 | |
| 142 | assert field_name == "validate_" |
| 143 | assert field_type is str |
| 144 | assert field_info.alias == "validate" |
| 145 | assert field_info.default is PydanticUndefined |
| 146 | |
| 147 | def test_field_with_examples(self): |
| 148 | """Test that examples are properly preserved in field info.""" |
| 149 | name = "example_field" |
| 150 | json_schema = { |
| 151 | "type": "string", |
| 152 | "description": "A field with examples", |
| 153 | "title": "Example Field", |
| 154 | "examples": ["example1", "example2"], |
| 155 | } |
| 156 | required = [] |
| 157 | |
| 158 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 159 | name, json_schema, required |
| 160 | ) |
| 161 | |
| 162 | assert field_name == "example_field" |
| 163 | assert field_info.examples == ["example1", "example2"] |
| 164 | |
| 165 | def test_oneof_field_description_merging(self): |
| 166 | """Test that oneOf schemas have their descriptions properly merged.""" |
| 167 | name = "oneof_field" |
| 168 | json_schema = { |
| 169 | "oneOf": [ |
| 170 | {"type": "string", "description": "String option"}, |
| 171 | {"type": "integer", "description": "Integer option"}, |
| 172 | ] |
| 173 | } |
| 174 | required = [] |
| 175 | |
| 176 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 177 | name, json_schema, required |
| 178 | ) |
| 179 | |
| 180 | expected_desc = "Any of the following options(separated by |): String option | Integer option" |
| 181 | assert field_info.description == expected_desc |
| 182 | |
| 183 | def test_skip_default_parameter(self): |
| 184 | """Test that skip_default parameter works correctly.""" |
| 185 | name = "test_field" |
| 186 | json_schema = {"type": "string", "default": "should_be_skipped"} |
| 187 | required = [] |
| 188 | |
| 189 | field_name, field_type, field_info = json_schema_to_pydantic_field( |
| 190 | name, json_schema, required, skip_default=True |
| 191 | ) |
| 192 | |
| 193 | # When skip_default=True, field should be required (default=PydanticUndefined) |
| 194 | assert field_info.default is PydanticUndefined |
| 195 | |
| 196 | |
| 197 | class TestJsonSchemaToModel: |
| 198 | """Test cases for json_schema_to_model function.""" |
| 199 | |
| 200 | def test_simple_model_creation(self): |
| 201 | """Test creating a simple Pydantic model from JSON schema.""" |
| 202 | json_schema = { |
| 203 | "title": "SimpleModel", |
| 204 | "type": "object", |
| 205 | "properties": { |
| 206 | "name": {"type": "string", "title": "Name"}, |
| 207 | "age": {"type": "integer", "title": "Age"}, |
| 208 | }, |
| 209 | "required": ["name"], |
| 210 | } |
| 211 | |
| 212 | model_class = json_schema_to_model(json_schema) |
| 213 | |
| 214 | # Test model creation |
| 215 | instance = model_class(name="test") |
| 216 | assert instance.name == "test" |
| 217 | assert instance.age is None |
| 218 | |
| 219 | # Test validation |
| 220 | with pytest.raises(Exception): # Should fail without required field |
| 221 | model_class() |
| 222 | |
| 223 | def test_nested_object_model(self): |
| 224 | """Test creating a model with nested objects.""" |
| 225 | json_schema = { |
| 226 | "title": "ParentModel", |
| 227 | "type": "object", |
| 228 | "properties": { |
| 229 | "basic_field": {"type": "string", "title": "Basic Field"}, |
| 230 | "nested_object": { |
| 231 | "type": "object", |
| 232 | "title": "NestedObject", |
| 233 | "properties": { |
| 234 | "inner_field": {"type": "string", "title": "Inner Field"} |
| 235 | }, |
| 236 | "required": ["inner_field"], |
| 237 | }, |
| 238 | }, |
| 239 | "required": ["basic_field"], |
| 240 | } |
| 241 | |
| 242 | model_class = json_schema_to_model(json_schema) |
| 243 | |
| 244 | # Test that nested_object is optional (not required) |
| 245 | instance = model_class(basic_field="test") |
| 246 | assert instance.basic_field == "test" |
| 247 | assert instance.nested_object is None |
| 248 | |
| 249 | # Test that nested object validation works when provided |
| 250 | instance_with_nested = model_class( |
| 251 | basic_field="test", nested_object={"inner_field": "nested_value"} |
| 252 | ) |
| 253 | assert instance_with_nested.nested_object.inner_field == "nested_value" |
| 254 | |
| 255 | @pytest.mark.unit |
| 256 | @pytest.mark.schema |
| 257 | def test_working_location_properties_bug_scenario(self): |
| 258 | """ |
| 259 | CRITICAL TEST: Reproduce the exact scenario that caused the bug. |
| 260 | |
| 261 | This tests the workingLocationProperties scenario that was incorrectly |
| 262 | marked as required. |
| 263 | """ |
| 264 | json_schema = { |
| 265 | "title": "CreateEventRequest", |
| 266 | "type": "object", |
| 267 | "properties": { |
| 268 | "start_datetime": {"type": "string", "title": "Start Datetime"}, |
| 269 | "workingLocationProperties": { |
| 270 | "type": "object", |
| 271 | "title": "WorkingLocationProperties", |
| 272 | "properties": { |
| 273 | "type": { |
| 274 | "type": "string", |
| 275 | "title": "Type", |
| 276 | "enum": ["homeOffice", "officeLocation", "customLocation"], |
| 277 | }, |
| 278 | "customLocation": { |
| 279 | "type": "object", |
| 280 | "title": "WorkingLocationCustom", |
| 281 | "properties": { |
| 282 | "label": {"type": "string", "title": "Label"} |
| 283 | }, |
| 284 | "required": ["label"], |
| 285 | }, |
| 286 | }, |
| 287 | "required": ["type"], |
| 288 | }, |
| 289 | }, |
| 290 | "required": ["start_datetime"], |
| 291 | } |
| 292 | |
| 293 | model_class = json_schema_to_model(json_schema) |
| 294 | |
| 295 | # Test that workingLocationProperties is NOT required |
| 296 | instance = model_class(start_datetime="2025-01-01T10:00:00") |
| 297 | assert instance.start_datetime == "2025-01-01T10:00:00" |
| 298 | assert instance.workingLocationProperties is None |
| 299 | |
| 300 | # Test that nested validation works when provided |
| 301 | instance_with_working_location = model_class( |
| 302 | start_datetime="2025-01-01T10:00:00", |
| 303 | workingLocationProperties={ |
| 304 | "type": "customLocation", |
| 305 | "customLocation": {"label": "Client Office"}, |
| 306 | }, |
| 307 | ) |
| 308 | assert ( |
| 309 | instance_with_working_location.workingLocationProperties.type |
| 310 | == "customLocation" |
| 311 | ) |
| 312 | |
| 313 | def test_array_type_handling(self): |
| 314 | """Test handling of array types in schema.""" |
| 315 | json_schema = { |
| 316 | "title": "ArrayModel", |
| 317 | "type": "object", |
| 318 | "properties": { |
| 319 | "tags": {"type": "array", "items": {"type": "string"}, "title": "Tags"} |
| 320 | }, |
| 321 | } |
| 322 | |
| 323 | model_class = json_schema_to_model(json_schema) |
| 324 | instance = model_class(tags=["tag1", "tag2"]) |
| 325 | assert instance.tags == ["tag1", "tag2"] |
| 326 | |
| 327 | @pytest.mark.parametrize( |
| 328 | ("title", "expected_model_name"), |
| 329 | [ |
| 330 | ({}, "GeneratedModel"), |
| 331 | ({"title": None}, "GeneratedModel"), |
| 332 | ({"title": ""}, ""), |
| 333 | ], |
| 334 | ) |
| 335 | def test_title_uses_expected_model_name(self, title, expected_model_name): |
| 336 | """Missing or null titles must not reach Pydantic's model factory.""" |
| 337 | json_schema = { |
| 338 | **title, |
| 339 | "type": "object", |
| 340 | "properties": { |
| 341 | "name": {"type": "string"}, |
| 342 | "value": {"type": "integer"}, |
| 343 | }, |
| 344 | "required": ["name"], |
| 345 | } |
| 346 | |
| 347 | model_class = json_schema_to_model(json_schema) |
| 348 | assert model_class.__name__ == expected_model_name |
| 349 | instance = model_class(name="test", value=42) |
| 350 | assert instance.name == "test" |
| 351 | assert instance.value == 42 |
| 352 | |
| 353 | with pytest.raises(ValidationError): |
| 354 | model_class() |
| 355 | |
| 356 | |
| 357 | class TestPydanticModelFromParamSchema: |
| 358 | """Test cases for pydantic_model_from_param_schema function.""" |
| 359 | |
| 360 | def test_simple_param_schema(self): |
| 361 | """Test creating a model from parameter schema format.""" |
| 362 | param_schema = { |
| 363 | "title": "SimpleParam", |
| 364 | "type": "object", |
| 365 | "properties": {"name": {"type": "string", "title": "Name"}}, |
| 366 | "required": ["name"], |
| 367 | } |
| 368 | |
| 369 | model_class = pydantic_model_from_param_schema(param_schema) |
| 370 | |
| 371 | # Should be able to create instance with required field |
| 372 | instance = model_class(name="test") |
| 373 | assert instance.name == "test" |
| 374 | |
| 375 | def test_nested_object_not_making_parent_required(self): |
| 376 | """ |
| 377 | CRITICAL TEST: Ensure nested objects with internal required fields |
| 378 | don't make the parent object required in pydantic_model_from_param_schema. |
| 379 | """ |
| 380 | param_schema = { |
| 381 | "title": "ParentParam", |
| 382 | "type": "object", |
| 383 | "properties": { |
| 384 | "required_field": {"type": "string", "title": "Required Field"}, |
| 385 | "optional_nested": { |
| 386 | "type": "object", |
| 387 | "title": "Optional Nested", |
| 388 | "properties": { |
| 389 | "inner_required": {"type": "string", "title": "Inner Required"} |
| 390 | }, |
| 391 | "required": [ |
| 392 | "inner_required" |
| 393 | ], # Should NOT make optional_nested required |
| 394 | }, |
| 395 | }, |
| 396 | "required": ["required_field"], |
| 397 | } |
| 398 | |
| 399 | model_class = pydantic_model_from_param_schema(param_schema) |
| 400 | |
| 401 | # Should work with just the required field |
| 402 | instance = model_class(required_field="test") |
| 403 | assert instance.required_field == "test" |
| 404 | # optional_nested should be optional (None or default value) |
| 405 | |
| 406 | def test_array_type_param_schema(self): |
| 407 | """Test array type handling in parameter schema.""" |
| 408 | param_schema = { |
| 409 | "title": "ArrayParam", |
| 410 | "type": "array", |
| 411 | "items": {"type": "string", "title": "String Item"}, |
| 412 | } |
| 413 | |
| 414 | result = pydantic_model_from_param_schema(param_schema) |
| 415 | # Should return List[str] type |
| 416 | assert hasattr(result, "__origin__") # Generic type |
| 417 | assert result.__origin__ is list |
| 418 | |
| 419 | def test_missing_title_error(self): |
| 420 | """Test that missing title raises appropriate error.""" |
| 421 | param_schema = { |
| 422 | "type": "object", |
| 423 | "properties": {}, |
| 424 | # Missing "title" |
| 425 | } |
| 426 | |
| 427 | with pytest.raises(ValueError, match="Missing 'title' in param_schema"): |
| 428 | pydantic_model_from_param_schema(param_schema) |
| 429 | |
| 430 | |
| 431 | def _materialized(annotation: t.Any) -> t.Any: |
| 432 | """Strip the exact-validation `Annotated` wrapper to reach the Pydantic type.""" |
| 433 | while t.get_origin(annotation) is t.Annotated: |
| 434 | annotation = t.get_args(annotation)[0] |
| 435 | return annotation |
| 436 | |
| 437 | |
| 438 | class TestJsonSchemaToPydanticType: |
| 439 | """Test cases for json_schema_to_pydantic_type function.""" |
| 440 | |
| 441 | def test_basic_types(self): |
| 442 | """Test conversion of basic JSON schema types to Python types.""" |
| 443 | test_cases = [ |
| 444 | ({"type": "string"}, str), |
| 445 | ({"type": "integer"}, int), |
| 446 | ({"type": "number"}, float), |
| 447 | ({"type": "boolean"}, bool), |
| 448 | ] |
| 449 | |
| 450 | for json_schema, expected_type in test_cases: |
| 451 | result = json_schema_to_pydantic_type(json_schema) |
| 452 | assert result == expected_type |
| 453 | |
| 454 | def test_anyof_null_only_preserves_nullability(self): |
| 455 | """Test that anyOf with only null accepts no non-null value.""" |
| 456 | result = _materialized( |
| 457 | json_schema_to_pydantic_type({"anyOf": [{"type": "null"}]}) |
| 458 | ) |
| 459 | |
| 460 | assert result is type(None) |
| 461 | |
| 462 | def test_array_type(self): |
| 463 | """Test array type conversion.""" |
| 464 | json_schema = {"type": "array", "items": {"type": "string"}} |
| 465 | |
| 466 | result = json_schema_to_pydantic_type(json_schema) |
| 467 | assert hasattr(result, "__origin__") |
| 468 | assert result.__origin__ is list |
| 469 | |
| 470 | def test_object_type_creates_nested_model(self): |
| 471 | """Test that object types create nested Pydantic models.""" |
| 472 | json_schema = { |
| 473 | "type": "object", |
| 474 | "title": "NestedModel", |
| 475 | "properties": {"field": {"type": "string", "title": "Field"}}, |
| 476 | } |
| 477 | |
| 478 | result = json_schema_to_pydantic_type(json_schema) |
| 479 | assert isinstance(result, type) |
| 480 | assert issubclass(result, BaseModel) |
| 481 | |
| 482 | def test_oneof_union_types(self): |
| 483 | """Test oneOf schemas create union types.""" |
| 484 | json_schema = {"oneOf": [{"type": "string"}, {"type": "integer"}]} |
| 485 | |
| 486 | result = json_schema_to_pydantic_type(json_schema) |
| 487 | # Should create a Union type |
| 488 | assert hasattr(result, "__origin__") |
| 489 | |
| 490 | def test_oneof_unlimited_types(self): |
| 491 | """Test oneOf schemas with unlimited number of types (fixes the 3-type limit bug).""" |
| 492 | # Test 4 types (previously would fail) |
| 493 | json_schema_4 = { |
| 494 | "oneOf": [ |
| 495 | {"type": "string"}, |
| 496 | {"type": "integer"}, |
| 497 | {"type": "boolean"}, |
| 498 | {"type": "number"}, |
| 499 | ] |
| 500 | } |
| 501 | result_4 = json_schema_to_pydantic_type(json_schema_4) |
| 502 | union_4 = t.get_args(result_4)[0] |
| 503 | assert t.get_origin(union_4) is t.Union |
| 504 | assert len(t.get_args(union_4)) == 4 |
| 505 | assert str in t.get_args(union_4) |
| 506 | assert int in t.get_args(union_4) |
| 507 | assert bool in t.get_args(union_4) |
| 508 | assert float in t.get_args(union_4) |
| 509 | # `integer` is a subset of `number`, so 42 matches two branches and |
| 510 | # must be rejected by oneOf's exactly-one rule. |
| 511 | with pytest.raises(ValidationError): |
| 512 | TypeAdapter(result_4).validate_python(42) |
| 513 | |
| 514 | # Test 5 types |
| 515 | json_schema_5 = { |
| 516 | "oneOf": [ |
| 517 | {"type": "string"}, |
| 518 | {"type": "integer"}, |
| 519 | {"type": "boolean"}, |
| 520 | {"type": "number"}, |
| 521 | {"type": "array"}, |
| 522 | ] |
| 523 | } |
| 524 | result_5 = json_schema_to_pydantic_type(json_schema_5) |
| 525 | union_5 = t.get_args(result_5)[0] |
| 526 | assert t.get_origin(union_5) is t.Union |
| 527 | assert len(t.get_args(union_5)) == 5 |
| 528 | |
| 529 | # Test 6 types (stress test, avoiding null which expands to Optional[Any]) |
| 530 | json_schema_6 = { |
| 531 | "oneOf": [ |
| 532 | {"type": "string"}, |
| 533 | {"type": "integer"}, |
| 534 | {"type": "boolean"}, |
| 535 | {"type": "number"}, |
| 536 | {"type": "array"}, |
| 537 | {"type": "object"}, |
| 538 | ] |
| 539 | } |
| 540 | result_6 = json_schema_to_pydantic_type(json_schema_6) |
| 541 | union_6 = t.get_args(result_6)[0] |
| 542 | assert t.get_origin(union_6) is t.Union |
| 543 | assert len(t.get_args(union_6)) == 6 |
| 544 | |
| 545 | def test_oneof_single_type(self): |
| 546 | """Test oneOf with single type returns the type directly.""" |
| 547 | json_schema = {"oneOf": [{"type": "string"}]} |
| 548 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 549 | assert result is str |
| 550 | # Single type should not create a Union |
| 551 | assert not hasattr(result, "__origin__") |
| 552 | |
| 553 | def test_oneof_with_complex_types(self): |
| 554 | """Test oneOf with complex types like objects and arrays.""" |
| 555 | json_schema = { |
| 556 | "oneOf": [ |
| 557 | {"type": "string"}, |
| 558 | {"type": "array", "items": {"type": "integer"}}, |
| 559 | { |
| 560 | "type": "object", |
| 561 | "title": "ComplexObject", |
| 562 | "properties": {"field": {"type": "string"}}, |
| 563 | }, |
| 564 | ] |
| 565 | } |
| 566 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 567 | assert hasattr(result, "__origin__") |
| 568 | assert result.__origin__ is t.Union |
| 569 | assert len(result.__args__) == 3 |
| 570 | # Check that we have string, List[int], and a BaseModel subclass |
| 571 | args = result.__args__ |
| 572 | assert str in args |
| 573 | # One should be a List type |
| 574 | list_types = [ |
| 575 | arg for arg in args if hasattr(arg, "__origin__") and arg.__origin__ is list |
| 576 | ] |
| 577 | assert len(list_types) == 1 |
| 578 | # One should be a BaseModel subclass |
| 579 | model_types = [ |
| 580 | arg for arg in args if isinstance(arg, type) and issubclass(arg, BaseModel) |
| 581 | ] |
| 582 | assert len(model_types) >= 1 |
| 583 | |
| 584 | def test_oneof_nested_in_object(self): |
| 585 | """Test oneOf field within an object schema.""" |
| 586 | json_schema = { |
| 587 | "type": "object", |
| 588 | "title": "ObjectWithOneOf", |
| 589 | "properties": { |
| 590 | "flexible_field": { |
| 591 | "oneOf": [ |
| 592 | {"type": "string"}, |
| 593 | {"type": "integer"}, |
| 594 | {"type": "boolean"}, |
| 595 | {"type": "number"}, |
| 596 | ] |
| 597 | }, |
| 598 | "normal_field": {"type": "string"}, |
| 599 | }, |
| 600 | "required": ["flexible_field"], |
| 601 | } |
| 602 | |
| 603 | # Test that the model can be created |
| 604 | model_class = json_schema_to_model(json_schema) |
| 605 | |
| 606 | # Test with different oneOf values |
| 607 | instance1 = model_class(flexible_field="hello", normal_field="world") |
| 608 | instance3 = model_class(flexible_field=True, normal_field="world") |
| 609 | instance4 = model_class(flexible_field=3.14, normal_field="world") |
| 610 | |
| 611 | assert instance1.flexible_field == "hello" |
| 612 | assert instance3.flexible_field is True |
| 613 | assert instance4.flexible_field == 3.14 |
| 614 | |
| 615 | # Draft 7 `oneOf` requires exactly one branch to match. An integer |
| 616 | # satisfies both {"type": "integer"} and {"type": "number"}, so it is |
| 617 | # rejected — the Zod and Effect converters agree. |
| 618 | with pytest.raises(ValidationError): |
| 619 | model_class(flexible_field=42, normal_field="world") |
| 620 | |
| 621 | def test_empty_schema_accepts_any_value(self): |
| 622 | """An empty JSON Schema accepts every value.""" |
| 623 | json_schema = {} # No type specified |
| 624 | |
| 625 | result = json_schema_to_pydantic_type(json_schema) |
| 626 | assert result is t.Any |
| 627 | |
| 628 | def test_unsupported_type_fallback(self): |
| 629 | """Test that unsupported types fall back to string (graceful degradation).""" |
| 630 | json_schema = {"type": "unsupported_type"} |
| 631 | |
| 632 | # The library gracefully falls back to string instead of raising an error |
| 633 | result = json_schema_to_pydantic_type(json_schema) |
| 634 | assert result is str |
| 635 | |
| 636 | def test_anyof_nullable_object(self): |
| 637 | """Test anyOf with object and null types (common for nullable fields).""" |
| 638 | json_schema = { |
| 639 | "anyOf": [{"type": "object", "additionalProperties": {}}, {"type": "null"}] |
| 640 | } |
| 641 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 642 | # Should return Optional[dict], not str (to allow both dict and None) |
| 643 | assert hasattr(result, "__origin__") |
| 644 | assert result.__origin__ is t.Union |
| 645 | # The library returns `dict` (the concrete type) instead of `typing.Dict` |
| 646 | assert dict in result.__args__ or t.Dict in result.__args__ |
| 647 | assert type(None) in result.__args__ |
| 648 | |
| 649 | def test_anyof_nullable_object_with_properties(self): |
| 650 | """Test anyOf with object (with properties) and null types.""" |
| 651 | json_schema = { |
| 652 | "anyOf": [ |
| 653 | { |
| 654 | "type": "object", |
| 655 | "title": "CustomFields", |
| 656 | "properties": {"field1": {"type": "string"}}, |
| 657 | }, |
| 658 | {"type": "null"}, |
| 659 | ] |
| 660 | } |
| 661 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 662 | # Should return Optional[BaseModel subclass] (Union with None) |
| 663 | assert hasattr(result, "__origin__") |
| 664 | assert result.__origin__ is t.Union |
| 665 | # One of the args should be a BaseModel subclass |
| 666 | model_types = [ |
| 667 | arg |
| 668 | for arg in result.__args__ |
| 669 | if isinstance(arg, type) and issubclass(arg, BaseModel) |
| 670 | ] |
| 671 | assert len(model_types) == 1 |
| 672 | # None should also be in the union |
| 673 | assert type(None) in result.__args__ |
| 674 | |
| 675 | def test_anyof_multiple_types(self): |
| 676 | """Test anyOf with multiple non-null types.""" |
| 677 | json_schema = { |
| 678 | "anyOf": [{"type": "string"}, {"type": "integer"}, {"type": "object"}] |
| 679 | } |
| 680 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 681 | assert hasattr(result, "__origin__") |
| 682 | assert result.__origin__ is t.Union |
| 683 | |
| 684 | def test_anyof_single_type(self): |
| 685 | """Test anyOf with single type returns the type directly.""" |
| 686 | json_schema = {"anyOf": [{"type": "string"}]} |
| 687 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 688 | assert result is str |
| 689 | assert not hasattr(result, "__origin__") |
| 690 | |
| 691 | def test_allof_single_option(self): |
| 692 | """A single allOf option preserves its acceptance behavior.""" |
| 693 | json_schema = { |
| 694 | "allOf": [ |
| 695 | { |
| 696 | "type": "object", |
| 697 | "properties": {"name": {"type": "string"}}, |
| 698 | "title": "Test", |
| 699 | } |
| 700 | ] |
| 701 | } |
| 702 | adapter = TypeAdapter(json_schema_to_pydantic_type(json_schema)) |
| 703 | result = adapter.validate_python({"name": "Ada"}) |
| 704 | assert adapter.dump_python(result, mode="json") == {"name": "Ada"} |
| 705 | with pytest.raises(ValidationError): |
| 706 | adapter.validate_python({"name": 42}) |
| 707 | |
| 708 | def test_allof_multiple_options_with_type(self): |
| 709 | """Test allOf with multiple options where one has type.""" |
| 710 | json_schema = { |
| 711 | "allOf": [{"description": "Some description"}, {"type": "string"}] |
| 712 | } |
| 713 | adapter = TypeAdapter(json_schema_to_pydantic_type(json_schema)) |
| 714 | assert adapter.validate_python("value") == "value" |
| 715 | with pytest.raises(ValidationError): |
| 716 | adapter.validate_python(42) |
| 717 | |
| 718 | def test_allof_empty_options(self): |
| 719 | """Test allOf with empty options accepts any value.""" |
| 720 | json_schema = {"allOf": []} |
| 721 | adapter = TypeAdapter(json_schema_to_pydantic_type(json_schema)) |
| 722 | assert adapter.validate_python(42) == 42 |
| 723 | assert adapter.validate_python({"anything": True}) == {"anything": True} |
| 724 | |
| 725 | |
| 726 | class TestJsonSchemaToFieldsDict: |
| 727 | """Test cases for json_schema_to_fields_dict function.""" |
| 728 | |
| 729 | def test_basic_fields_dict(self): |
| 730 | """Test creating fields dictionary from JSON schema.""" |
| 731 | json_schema = { |
| 732 | "properties": { |
| 733 | "name": {"type": "string", "title": "Name"}, |
| 734 | "age": {"type": "integer", "title": "Age"}, |
| 735 | }, |
| 736 | "required": ["name"], |
| 737 | } |
| 738 | |
| 739 | fields_dict = json_schema_to_fields_dict(json_schema) |
| 740 | |
| 741 | assert "name" in fields_dict |
| 742 | assert "age" in fields_dict |
| 743 | |
| 744 | # Check field types and info |
| 745 | name_type, name_field = fields_dict["name"] |
| 746 | age_type, age_field = fields_dict["age"] |
| 747 | |
| 748 | assert name_type is str |
| 749 | assert age_type is int |
| 750 | assert name_field.default is PydanticUndefined # Required |
| 751 | assert age_field.default is None # Optional |
| 752 | |
| 753 | |
| 754 | class TestRegressionScenarios: |
| 755 | """Test cases for specific regression scenarios and edge cases.""" |
| 756 | |
| 757 | def test_deeply_nested_objects_required_propagation(self): |
| 758 | """Test deeply nested objects don't propagate required fields incorrectly.""" |
| 759 | json_schema = { |
| 760 | "title": "DeeplyNested", |
| 761 | "type": "object", |
| 762 | "properties": { |
| 763 | "level1": { |
| 764 | "type": "object", |
| 765 | "title": "Level1", |
| 766 | "properties": { |
| 767 | "level2": { |
| 768 | "type": "object", |
| 769 | "title": "Level2", |
| 770 | "properties": { |
| 771 | "level3": { |
| 772 | "type": "object", |
| 773 | "title": "Level3", |
| 774 | "properties": { |
| 775 | "deep_field": { |
| 776 | "type": "string", |
| 777 | "title": "Deep Field", |
| 778 | } |
| 779 | }, |
| 780 | "required": ["deep_field"], |
| 781 | } |
| 782 | }, |
| 783 | "required": ["level3"], |
| 784 | } |
| 785 | }, |
| 786 | "required": ["level2"], |
| 787 | } |
| 788 | }, |
| 789 | "required": [], # level1 should NOT be required |
| 790 | } |
| 791 | |
| 792 | model_class = json_schema_to_model(json_schema) |
| 793 | |
| 794 | # Should be able to create instance without level1 |
| 795 | instance = model_class() |
| 796 | assert instance.level1 is None |
| 797 | |
| 798 | def test_multiple_nested_objects_same_level(self): |
| 799 | """Test multiple nested objects at same level with different required fields.""" |
| 800 | json_schema = { |
| 801 | "title": "MultipleNested", |
| 802 | "type": "object", |
| 803 | "properties": { |
| 804 | "config1": { |
| 805 | "type": "object", |
| 806 | "title": "Config1", |
| 807 | "properties": {"setting1": {"type": "string", "title": "Setting1"}}, |
| 808 | "required": ["setting1"], |
| 809 | }, |
| 810 | "config2": { |
| 811 | "type": "object", |
| 812 | "title": "Config2", |
| 813 | "properties": {"setting2": {"type": "string", "title": "Setting2"}}, |
| 814 | "required": ["setting2"], |
| 815 | }, |
| 816 | "required_field": {"type": "string", "title": "Required Field"}, |
| 817 | }, |
| 818 | "required": ["required_field"], |
| 819 | } |
| 820 | |
| 821 | model_class = json_schema_to_model(json_schema) |
| 822 | |
| 823 | # Should work with just required_field |
| 824 | instance = model_class(required_field="test") |
| 825 | assert instance.required_field == "test" |
| 826 | assert instance.config1 is None |
| 827 | assert instance.config2 is None |
| 828 | |
| 829 | def test_empty_required_array_handling(self): |
| 830 | """Test handling of empty required arrays.""" |
| 831 | json_schema = { |
| 832 | "title": "EmptyRequired", |
| 833 | "type": "object", |
| 834 | "properties": { |
| 835 | "optional1": {"type": "string", "title": "Optional1"}, |
| 836 | "optional2": {"type": "string", "title": "Optional2"}, |
| 837 | }, |
| 838 | "required": [], |
| 839 | } |
| 840 | |
| 841 | model_class = json_schema_to_model(json_schema) |
| 842 | |
| 843 | # Should work with no fields |
| 844 | instance = model_class() |
| 845 | assert instance.optional1 is None |
| 846 | assert instance.optional2 is None |
| 847 | |
| 848 | def test_missing_required_array_handling(self): |
| 849 | """Test handling when required array is missing entirely.""" |
| 850 | json_schema = { |
| 851 | "title": "NoRequired", |
| 852 | "type": "object", |
| 853 | "properties": { |
| 854 | "field1": {"type": "string", "title": "Field1"}, |
| 855 | "field2": {"type": "string", "title": "Field2"}, |
| 856 | }, |
| 857 | # No "required" key at all |
| 858 | } |
| 859 | |
| 860 | model_class = json_schema_to_model(json_schema) |
| 861 | |
| 862 | # Should work with no fields (all optional) |
| 863 | instance = model_class() |
| 864 | assert instance.field1 is None |
| 865 | assert instance.field2 is None |
| 866 | |
| 867 | |
| 868 | class TestEdgeCases: |
| 869 | """Test cases for edge cases and error conditions.""" |
| 870 | |
| 871 | def test_none_schema_handling(self): |
| 872 | """Test handling of None or empty schemas.""" |
| 873 | with pytest.raises((TypeError, AttributeError, KeyError)): |
| 874 | json_schema_to_model(None) |
| 875 | |
| 876 | def test_malformed_schema_handling(self): |
| 877 | """Test handling of malformed schemas.""" |
| 878 | malformed_schemas = [ |
| 879 | {"type": "object"}, # Missing properties |
| 880 | {"properties": {}}, # Missing type and title |
| 881 | {"title": "Test", "type": "invalid_type"}, # Invalid type |
| 882 | ] |
| 883 | |
| 884 | for schema in malformed_schemas: |
| 885 | # Should either handle gracefully or raise appropriate error |
| 886 | try: |
| 887 | result = json_schema_to_model(schema) |
| 888 | # If it doesn't raise an error, it should at least return something |
| 889 | assert result is not None |
| 890 | except (ValueError, KeyError, TypeError): |
| 891 | # These are acceptable errors for malformed schemas |
| 892 | pass |
| 893 | |
| 894 | def test_circular_reference_protection(self): |
| 895 | """Test that circular references don't cause infinite recursion.""" |
| 896 | # This is a complex scenario that would require special handling |
| 897 | # For now, we just ensure it doesn't crash |
| 898 | json_schema = { |
| 899 | "title": "SelfReference", |
| 900 | "type": "object", |
| 901 | "properties": { |
| 902 | "name": {"type": "string", "title": "Name"}, |
| 903 | "children": { |
| 904 | "type": "array", |
| 905 | "items": {"$ref": "#"}, # Self-reference |
| 906 | }, |
| 907 | }, |
| 908 | } |
| 909 | |
| 910 | # This might not work perfectly but shouldn't crash |
| 911 | try: |
| 912 | model_class = json_schema_to_model(json_schema) |
| 913 | # If successful, test basic functionality |
| 914 | instance = model_class(name="test") |
| 915 | assert instance.name == "test" |
| 916 | except (RecursionError, ValueError): |
| 917 | # Acceptable for now - circular references are complex |
| 918 | pass |
| 919 | |
| 920 | |
| 921 | class TestCrewAICustomFieldsBug: |
| 922 | """Regression tests for PLEN-1177 - CustomFields type error with CrewAI.""" |
| 923 | |
| 924 | @pytest.mark.unit |
| 925 | @pytest.mark.schema |
| 926 | def test_custom_fields_object_type_preserved(self): |
| 927 | """ |
| 928 | Test that CustomFields with anyOf [object, null] returns Dict, not str. |
| 929 | |
| 930 | This is the exact bug scenario from PLEN-1177. |
| 931 | """ |
| 932 | # Schema similar to what Salesforce tools return for CustomFields |
| 933 | json_schema = { |
| 934 | "title": "CreateLeadRequest", |
| 935 | "type": "object", |
| 936 | "properties": { |
| 937 | "Company": {"type": "string", "title": "Company"}, |
| 938 | "LastName": {"type": "string", "title": "LastName"}, |
| 939 | "CustomFields": { |
| 940 | "anyOf": [ |
| 941 | {"type": "object", "additionalProperties": {}}, |
| 942 | {"type": "null"}, |
| 943 | ], |
| 944 | "default": None, |
| 945 | "description": "Dictionary of custom field API names and their values.", |
| 946 | "title": "CustomFields", |
| 947 | }, |
| 948 | }, |
| 949 | "required": ["Company", "LastName"], |
| 950 | } |
| 951 | |
| 952 | model_class = json_schema_to_model(json_schema) |
| 953 | |
| 954 | # The model should accept a dict for CustomFields |
| 955 | instance = model_class( |
| 956 | Company="Test Corp", |
| 957 | LastName="Smith", |
| 958 | CustomFields={"Custom_Field__c": "Value"}, |
| 959 | ) |
| 960 | assert instance.CustomFields == {"Custom_Field__c": "Value"} |
| 961 | |
| 962 | # The model should also accept None |
| 963 | instance_none = model_class( |
| 964 | Company="Test Corp", |
| 965 | LastName="Smith", |
| 966 | CustomFields=None, |
| 967 | ) |
| 968 | assert instance_none.CustomFields is None |
| 969 | |
| 970 | @pytest.mark.unit |
| 971 | @pytest.mark.schema |
| 972 | def test_pydantic_model_json_schema_preserves_object_type(self): |
| 973 | """ |
| 974 | Test that when Pydantic model is converted back to JSON schema, |
| 975 | the object type is preserved (not converted to string). |
| 976 | """ |
| 977 | json_schema = { |
| 978 | "title": "TestModel", |
| 979 | "type": "object", |
| 980 | "properties": { |
| 981 | "custom_dict": { |
| 982 | "anyOf": [{"type": "object"}, {"type": "null"}], |
| 983 | "default": None, |
| 984 | } |
| 985 | }, |
| 986 | } |
| 987 | |
| 988 | model_class = json_schema_to_model(json_schema) |
| 989 | generated_schema = model_class.model_json_schema() |
| 990 | |
| 991 | # The generated schema should have object type, not string |
| 992 | custom_dict_schema = generated_schema["properties"]["custom_dict"] |
| 993 | |
| 994 | # It might be wrapped in anyOf or have direct type |
| 995 | if "anyOf" in custom_dict_schema: |
| 996 | types = [opt.get("type") for opt in custom_dict_schema["anyOf"]] |
| 997 | assert "object" in types |
| 998 | assert "string" not in types |
| 999 | else: |
| 1000 | assert ( |
| 1001 | custom_dict_schema.get("type") == "object" |
| 1002 | or custom_dict_schema.get("additionalProperties") is not None |
| 1003 | ) |
| 1004 | |
| 1005 | @pytest.mark.unit |
| 1006 | @pytest.mark.schema |
| 1007 | def test_anyof_with_nested_custom_fields(self): |
| 1008 | """ |
| 1009 | Test anyOf handling with more complex nested CustomFields scenario. |
| 1010 | """ |
| 1011 | json_schema = { |
| 1012 | "title": "SalesforceRequest", |
| 1013 | "type": "object", |
| 1014 | "properties": { |
| 1015 | "leadData": { |
| 1016 | "type": "object", |
| 1017 | "title": "LeadData", |
| 1018 | "properties": { |
| 1019 | "Name": {"type": "string", "title": "Name"}, |
| 1020 | "CustomFields": { |
| 1021 | "anyOf": [ |
| 1022 | {"type": "object", "additionalProperties": {}}, |
| 1023 | {"type": "null"}, |
| 1024 | ], |
| 1025 | "default": None, |
| 1026 | }, |
| 1027 | }, |
| 1028 | "required": ["Name"], |
| 1029 | } |
| 1030 | }, |
| 1031 | "required": ["leadData"], |
| 1032 | } |
| 1033 | |
| 1034 | model_class = json_schema_to_model(json_schema) |
| 1035 | |
| 1036 | # Test with CustomFields as dict |
| 1037 | instance = model_class( |
| 1038 | leadData={"Name": "John", "CustomFields": {"Industry__c": "Tech"}} |
| 1039 | ) |
| 1040 | assert instance.leadData.CustomFields == {"Industry__c": "Tech"} |
| 1041 | |
| 1042 | # Test with CustomFields as None |
| 1043 | instance_none = model_class(leadData={"Name": "John", "CustomFields": None}) |
| 1044 | assert instance_none.leadData.CustomFields is None |
| 1045 | |
| 1046 | |
| 1047 | class TestBooleanSchemas: |
| 1048 | """Test cases for JSON Schema boolean schema handling (draft-06+).""" |
| 1049 | |
| 1050 | @pytest.mark.unit |
| 1051 | @pytest.mark.schema |
| 1052 | def test_true_schema_in_anyof(self): |
| 1053 | """Test that true boolean schema in anyOf doesn't crash.""" |
| 1054 | json_schema = { |
| 1055 | "anyOf": [ |
| 1056 | {"type": "string"}, |
| 1057 | True, # Boolean schema |
| 1058 | ] |
| 1059 | } |
| 1060 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 1061 | # Should handle gracefully, creating a union including Any |
| 1062 | assert result is not None |
| 1063 | # The result should include Any type from the true schema |
| 1064 | assert hasattr(result, "__origin__") |
| 1065 | assert result.__origin__ is t.Union |
| 1066 | |
| 1067 | @pytest.mark.unit |
| 1068 | @pytest.mark.schema |
| 1069 | def test_false_schema_in_anyof(self): |
| 1070 | """Test that false boolean schema in anyOf doesn't crash.""" |
| 1071 | json_schema = { |
| 1072 | "anyOf": [ |
| 1073 | {"type": "string"}, |
| 1074 | False, # Boolean schema - should be filtered out |
| 1075 | ] |
| 1076 | } |
| 1077 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 1078 | # Should handle gracefully, false schema filtered out leaving just string |
| 1079 | assert result is str |
| 1080 | |
| 1081 | @pytest.mark.unit |
| 1082 | @pytest.mark.schema |
| 1083 | def test_boolean_schema_in_allof_with_type(self): |
| 1084 | """A true allOf branch leaves the typed branch unchanged.""" |
| 1085 | json_schema = { |
| 1086 | "allOf": [ |
| 1087 | {"type": "string"}, |
| 1088 | True, |
| 1089 | ] |
| 1090 | } |
| 1091 | adapter = TypeAdapter(json_schema_to_pydantic_type(json_schema)) |
| 1092 | assert adapter.validate_python("value") == "value" |
| 1093 | with pytest.raises(ValidationError): |
| 1094 | adapter.validate_python(42) |
| 1095 | |
| 1096 | @pytest.mark.unit |
| 1097 | @pytest.mark.schema |
| 1098 | def test_boolean_schema_in_allof_single(self): |
| 1099 | """A single true allOf branch accepts every value.""" |
| 1100 | json_schema = {"allOf": [True]} |
| 1101 | adapter = TypeAdapter(json_schema_to_pydantic_type(json_schema)) |
| 1102 | assert adapter.validate_python(42) == 42 |
| 1103 | assert adapter.validate_python({"anything": True}) == {"anything": True} |
| 1104 | |
| 1105 | @pytest.mark.unit |
| 1106 | @pytest.mark.schema |
| 1107 | def test_boolean_schema_in_oneof(self): |
| 1108 | """A true branch still participates in oneOf's exactly-one rule.""" |
| 1109 | json_schema = { |
| 1110 | "oneOf": [ |
| 1111 | {"type": "string"}, |
| 1112 | True, |
| 1113 | ] |
| 1114 | } |
| 1115 | result = json_schema_to_pydantic_type(json_schema) |
| 1116 | adapter = TypeAdapter(result) |
| 1117 | assert adapter.validate_python(42) == 42 |
| 1118 | with pytest.raises(ValidationError): |
| 1119 | adapter.validate_python("matches both branches") |
| 1120 | |
| 1121 | @pytest.mark.unit |
| 1122 | @pytest.mark.schema |
| 1123 | def test_standalone_true_schema(self): |
| 1124 | """Test that standalone true schema returns Any.""" |
| 1125 | result = json_schema_to_pydantic_type(True) |
| 1126 | assert result is t.Any |
| 1127 | |
| 1128 | @pytest.mark.unit |
| 1129 | @pytest.mark.schema |
| 1130 | def test_standalone_false_schema(self): |
| 1131 | """Test that standalone false schema rejects every value.""" |
| 1132 | adapter = TypeAdapter(json_schema_to_pydantic_type(False)) |
| 1133 | for value in (None, "value", 1, {}, []): |
| 1134 | with pytest.raises(ValidationError): |
| 1135 | adapter.validate_python(value) |
| 1136 | |
| 1137 | @pytest.mark.unit |
| 1138 | @pytest.mark.schema |
| 1139 | def test_only_false_schemas_in_anyof(self): |
| 1140 | """Test anyOf with only false schemas rejects every value.""" |
| 1141 | json_schema = { |
| 1142 | "anyOf": [ |
| 1143 | False, |
| 1144 | False, |
| 1145 | ] |
| 1146 | } |
| 1147 | result = json_schema_to_pydantic_type(json_schema) |
| 1148 | adapter = TypeAdapter(result) |
| 1149 | for value in (None, "some string", 123, {}, []): |
| 1150 | with pytest.raises(ValidationError): |
| 1151 | adapter.validate_python(value) |
| 1152 | |
| 1153 | @pytest.mark.unit |
| 1154 | @pytest.mark.schema |
| 1155 | def test_mixed_boolean_schemas_in_anyof(self): |
| 1156 | """Test anyOf with mixed true and false schemas.""" |
| 1157 | json_schema = { |
| 1158 | "anyOf": [ |
| 1159 | True, |
| 1160 | False, |
| 1161 | {"type": "integer"}, |
| 1162 | ] |
| 1163 | } |
| 1164 | result = _materialized(json_schema_to_pydantic_type(json_schema)) |
| 1165 | # Should create union of Any and int (false filtered out) |
| 1166 | assert result is not None |
| 1167 | assert hasattr(result, "__origin__") |
| 1168 | assert result.__origin__ is t.Union |
| 1169 | |
| 1170 | |
| 1171 | class TestBooleanDefaultCoercion: |
| 1172 | """Regression tests for PLEN-1311 - Boolean default type mismatch in LangchainProvider.""" |
| 1173 | |
| 1174 | @pytest.mark.unit |
| 1175 | @pytest.mark.schema |
| 1176 | def test_anyof_boolean_null_with_boolean_default(self): |
| 1177 | """ |
| 1178 | Test that anyOf [boolean, null] with boolean default preserves types. |
| 1179 | |
| 1180 | Regression test for PLEN-1311: GOOGLEDRIVE_FIND_FILE supportsAllDrives |
| 1181 | field was incorrectly converted to string type with string default. |
| 1182 | """ |
| 1183 | json_schema = { |
| 1184 | "title": "FindFileRequest", |
| 1185 | "type": "object", |
| 1186 | "properties": { |
| 1187 | "supportsAllDrives": { |
| 1188 | "anyOf": [{"type": "boolean"}, {"type": "null"}], |
| 1189 | "default": True, |
| 1190 | "description": "Whether to search all drives.", |
| 1191 | }, |
| 1192 | }, |
| 1193 | } |
| 1194 | |
| 1195 | model_class = json_schema_to_model(json_schema) |
| 1196 | |
| 1197 | # Verify the model accepts boolean values |
| 1198 | instance = model_class(supportsAllDrives=True) |
| 1199 | assert instance.supportsAllDrives is True |
| 1200 | |
| 1201 | instance_false = model_class(supportsAllDrives=False) |
| 1202 | assert instance_false.supportsAllDrives is False |
| 1203 | |
| 1204 | # Verify the generated JSON schema preserves boolean type |
| 1205 | generated_schema = model_class.model_json_schema() |
| 1206 | prop = generated_schema["properties"]["supportsAllDrives"] |
| 1207 | |
| 1208 | # Should NOT be string type |
| 1209 | assert prop.get("type") != "string" |
| 1210 | |
| 1211 | # Default should be boolean True, not string "true" |
| 1212 | assert prop.get("default") is True |
| 1213 | |
| 1214 | @pytest.mark.unit |
| 1215 | @pytest.mark.schema |
| 1216 | def test_anyof_boolean_null_with_string_default_coerced(self): |
| 1217 | """ |
| 1218 | Test that string "true"/"false" defaults are coerced to boolean. |
| 1219 | |
| 1220 | This handles cases where the API returns stringified boolean defaults. |
| 1221 | """ |
| 1222 | json_schema = { |
| 1223 | "title": "FindFileRequest", |
| 1224 | "type": "object", |
| 1225 | "properties": { |
| 1226 | "supportsAllDrives": { |
| 1227 | "anyOf": [{"type": "boolean"}, {"type": "null"}], |
| 1228 | "default": "true", # String, should be coerced to True |
| 1229 | "description": "Whether to search all drives.", |
| 1230 | }, |
| 1231 | }, |
| 1232 | } |
| 1233 | |
| 1234 | model_class = json_schema_to_model(json_schema) |
| 1235 | |
| 1236 | # Verify the model accepts boolean values |
| 1237 | instance = model_class(supportsAllDrives=True) |
| 1238 | assert instance.supportsAllDrives is True |
| 1239 | |
| 1240 | # Verify the generated JSON schema has coerced default |
| 1241 | generated_schema = model_class.model_json_schema() |
| 1242 | prop = generated_schema["properties"]["supportsAllDrives"] |
| 1243 | |
| 1244 | # Should NOT have string type |
| 1245 | assert prop.get("type") != "string" |
| 1246 | |
| 1247 | # Default should be coerced to boolean True |
| 1248 | assert prop.get("default") is True |
| 1249 | |
| 1250 | @pytest.mark.unit |
| 1251 | @pytest.mark.schema |
| 1252 | def test_anyof_boolean_null_with_string_false_default_coerced(self): |
| 1253 | """ |
| 1254 | Test that string "false" default is coerced to boolean False. |
| 1255 | """ |
| 1256 | json_schema = { |
| 1257 | "title": "TestRequest", |
| 1258 | "type": "object", |
| 1259 | "properties": { |
| 1260 | "enabled": { |
| 1261 | "anyOf": [{"type": "boolean"}, {"type": "null"}], |
| 1262 | "default": "false", # String, should be coerced to False |
| 1263 | "description": "Enable feature.", |
| 1264 | }, |
| 1265 | }, |
| 1266 | } |
| 1267 | |
| 1268 | model_class = json_schema_to_model(json_schema) |
| 1269 | generated_schema = model_class.model_json_schema() |
| 1270 | prop = generated_schema["properties"]["enabled"] |
| 1271 | |
| 1272 | # Default should be coerced to boolean False |
| 1273 | assert prop.get("default") is False |
| 1274 | |
| 1275 | @pytest.mark.unit |
| 1276 | @pytest.mark.schema |
| 1277 | def test_integer_with_string_default_coerced(self): |
| 1278 | """Test that string integer defaults are coerced.""" |
| 1279 | json_schema = { |
| 1280 | "title": "TestRequest", |
| 1281 | "type": "object", |
| 1282 | "properties": { |
| 1283 | "page": { |
| 1284 | "type": "integer", |
| 1285 | "default": "1", # String, should be coerced to 1 |
| 1286 | "description": "Page number", |
| 1287 | }, |
| 1288 | }, |
| 1289 | } |
| 1290 | |
| 1291 | model_class = json_schema_to_model(json_schema) |
| 1292 | generated_schema = model_class.model_json_schema() |
| 1293 | prop = generated_schema["properties"]["page"] |
| 1294 | |
| 1295 | # Default should be integer 1, not string "1" |
| 1296 | assert prop.get("default") == 1 |
| 1297 | assert isinstance(prop.get("default"), int) |
| 1298 | |
| 1299 | @pytest.mark.unit |
| 1300 | @pytest.mark.schema |
| 1301 | def test_type_list_with_string_default(self): |
| 1302 | """A list-valued `type` (e.g. ["string", "null"]) must not crash default coercion.""" |
| 1303 | json_schema = { |
| 1304 | "title": "TestRequest", |
| 1305 | "type": "object", |
| 1306 | "properties": { |
| 1307 | "order": { |
| 1308 | "type": ["string", "null"], |
| 1309 | "default": "asc", |
| 1310 | "description": "Sort order", |
| 1311 | }, |
| 1312 | }, |
| 1313 | } |
| 1314 | |
| 1315 | model_class = json_schema_to_model(json_schema) |
| 1316 | generated_schema = model_class.model_json_schema() |
| 1317 | prop = generated_schema["properties"]["order"] |
| 1318 | |
| 1319 | # String is an expected type, so the default stays a string |
| 1320 | assert prop.get("default") == "asc" |
| 1321 | |
| 1322 | @pytest.mark.unit |
| 1323 | @pytest.mark.schema |
| 1324 | def test_type_list_boolean_null_with_string_default_coerced(self): |
| 1325 | """Stringified defaults are coerced when `type` is a list.""" |
| 1326 | json_schema = { |
| 1327 | "title": "TestRequest", |
| 1328 | "type": "object", |
| 1329 | "properties": { |
| 1330 | "enabled": { |
| 1331 | "type": ["boolean", "null"], |
| 1332 | "default": "false", |
| 1333 | "description": "Enable feature.", |
| 1334 | }, |
| 1335 | }, |
| 1336 | } |
| 1337 | |
| 1338 | model_class = json_schema_to_model(json_schema) |
| 1339 | generated_schema = model_class.model_json_schema() |
| 1340 | prop = generated_schema["properties"]["enabled"] |
| 1341 | |
| 1342 | assert prop.get("default") is False |
| 1343 | |
| 1344 | @pytest.mark.unit |
| 1345 | @pytest.mark.schema |
| 1346 | @pytest.mark.parametrize("combiner", ["anyOf", "oneOf", "allOf"]) |
| 1347 | def test_type_list_inside_combiner_with_string_default_coerced(self, combiner): |
| 1348 | """A list-valued `type` inside a combiner branch also drives coercion.""" |
| 1349 | json_schema = { |
| 1350 | "title": "TestRequest", |
| 1351 | "type": "object", |
| 1352 | "properties": { |
| 1353 | "limit": { |
| 1354 | combiner: [{"type": ["integer", "null"]}], |
| 1355 | "default": "5", |
| 1356 | "description": "Page size.", |
| 1357 | }, |
| 1358 | }, |
| 1359 | } |
| 1360 | |
| 1361 | model_class = json_schema_to_model(json_schema) |
| 1362 | generated_schema = model_class.model_json_schema() |
| 1363 | prop = generated_schema["properties"]["limit"] |
| 1364 | |
| 1365 | assert prop.get("default") == 5 |
| 1366 | |
| 1367 | @pytest.mark.unit |
| 1368 | @pytest.mark.schema |
| 1369 | def test_float_with_string_default_coerced(self): |
| 1370 | """Test that string float defaults are coerced.""" |
| 1371 | json_schema = { |
| 1372 | "title": "TestRequest", |
| 1373 | "type": "object", |
| 1374 | "properties": { |
| 1375 | "rate": { |
| 1376 | "type": "number", |
| 1377 | "default": "3.14", # String, should be coerced to 3.14 |
| 1378 | "description": "Rate value", |
| 1379 | }, |
| 1380 | }, |
| 1381 | } |
| 1382 | |
| 1383 | model_class = json_schema_to_model(json_schema) |
| 1384 | generated_schema = model_class.model_json_schema() |
| 1385 | prop = generated_schema["properties"]["rate"] |
| 1386 | |
| 1387 | # Default should be float 3.14, not string "3.14" |
| 1388 | assert prop.get("default") == 3.14 |
| 1389 | assert isinstance(prop.get("default"), float) |
| 1390 | |
| 1391 | @pytest.mark.unit |
| 1392 | @pytest.mark.schema |
| 1393 | def test_boolean_default_not_coerced_when_already_correct(self): |
| 1394 | """Test that boolean defaults that are already correct are not modified.""" |
| 1395 | json_schema = { |
| 1396 | "title": "TestRequest", |
| 1397 | "type": "object", |
| 1398 | "properties": { |
| 1399 | "enabled": { |
| 1400 | "type": "boolean", |
| 1401 | "default": True, # Already boolean |
| 1402 | "description": "Enable feature.", |
| 1403 | }, |
| 1404 | }, |
| 1405 | } |
| 1406 | |
| 1407 | model_class = json_schema_to_model(json_schema) |
| 1408 | generated_schema = model_class.model_json_schema() |
| 1409 | prop = generated_schema["properties"]["enabled"] |
| 1410 | |
| 1411 | assert prop.get("default") is True |
| 1412 | assert isinstance(prop.get("default"), bool) |
| 1413 | |
| 1414 | @pytest.mark.unit |
| 1415 | @pytest.mark.schema |
| 1416 | def test_string_default_not_coerced_for_string_type(self): |
| 1417 | """Test that string defaults for string type fields are preserved.""" |
| 1418 | json_schema = { |
| 1419 | "title": "TestRequest", |
| 1420 | "type": "object", |
| 1421 | "properties": { |
| 1422 | "name": { |
| 1423 | "type": "string", |
| 1424 | "default": "true", # String value, should stay as string |
| 1425 | "description": "Name field", |
| 1426 | }, |
| 1427 | }, |
| 1428 | } |
| 1429 | |
| 1430 | model_class = json_schema_to_model(json_schema) |
| 1431 | generated_schema = model_class.model_json_schema() |
| 1432 | prop = generated_schema["properties"]["name"] |
| 1433 | |
| 1434 | # Default should stay as string "true" |
| 1435 | assert prop.get("default") == "true" |
| 1436 | assert isinstance(prop.get("default"), str) |
| 1437 | |
| 1438 | @pytest.mark.unit |
| 1439 | @pytest.mark.schema |
| 1440 | def test_allof_boolean_with_string_default_coerced(self): |
| 1441 | """Test that allOf with boolean type coerces string default.""" |
| 1442 | json_schema = { |
| 1443 | "title": "TestRequest", |
| 1444 | "type": "object", |
| 1445 | "properties": { |
| 1446 | "flag": { |
| 1447 | "allOf": [{"type": "boolean"}], |
| 1448 | "default": "true", |
| 1449 | "description": "Flag field", |
| 1450 | }, |
| 1451 | }, |
| 1452 | } |
| 1453 | |
| 1454 | model_class = json_schema_to_model(json_schema) |
| 1455 | generated_schema = model_class.model_json_schema() |
| 1456 | prop = generated_schema["properties"]["flag"] |
| 1457 | |
| 1458 | # Default should be coerced to boolean True |
| 1459 | assert prop.get("default") is True |
| 1460 | |
| 1461 | @pytest.mark.unit |
| 1462 | @pytest.mark.schema |
| 1463 | def test_invalid_boolean_string_not_coerced(self): |
| 1464 | """Test that invalid boolean strings are not coerced and return as-is.""" |
| 1465 | json_schema = { |
| 1466 | "title": "TestRequest", |
| 1467 | "type": "object", |
| 1468 | "properties": { |
| 1469 | "flag": { |
| 1470 | "anyOf": [{"type": "boolean"}, {"type": "null"}], |
| 1471 | "default": "invalid", # Not a valid boolean string |
| 1472 | "description": "Flag field", |
| 1473 | }, |
| 1474 | }, |
| 1475 | } |
| 1476 | |
| 1477 | model_class = json_schema_to_model(json_schema) |
| 1478 | generated_schema = model_class.model_json_schema() |
| 1479 | prop = generated_schema["properties"]["flag"] |
| 1480 | |
| 1481 | # Default should remain as string "invalid" since it can't be coerced |
| 1482 | assert prop.get("default") == "invalid" |
| 1483 | |
| 1484 | @pytest.mark.unit |
| 1485 | @pytest.mark.schema |
| 1486 | def test_empty_string_default_not_coerced(self): |
| 1487 | """Test that empty string defaults are not coerced.""" |
| 1488 | json_schema = { |
| 1489 | "title": "TestRequest", |
| 1490 | "type": "object", |
| 1491 | "properties": { |
| 1492 | "value": { |
| 1493 | "anyOf": [{"type": "integer"}, {"type": "null"}], |
| 1494 | "default": "", # Empty string |
| 1495 | "description": "Value field", |
| 1496 | }, |
| 1497 | }, |
| 1498 | } |
| 1499 | |
| 1500 | model_class = json_schema_to_model(json_schema) |
| 1501 | generated_schema = model_class.model_json_schema() |
| 1502 | prop = generated_schema["properties"]["value"] |
| 1503 | |
| 1504 | # Default should remain as empty string since it can't be coerced to int |
| 1505 | assert prop.get("default") == "" |
| 1506 | |
| 1507 | @pytest.mark.unit |
| 1508 | @pytest.mark.schema |
| 1509 | def test_non_string_default_not_coerced(self): |
| 1510 | """Test that non-string defaults (like int, list) are returned as-is.""" |
| 1511 | json_schema = { |
| 1512 | "title": "TestRequest", |
| 1513 | "type": "object", |
| 1514 | "properties": { |
| 1515 | "count": { |
| 1516 | "type": "integer", |
| 1517 | "default": 42, # Already an integer |
| 1518 | "description": "Count field", |
| 1519 | }, |
| 1520 | "items": { |
| 1521 | "type": "array", |
| 1522 | "default": [1, 2, 3], # Already a list |
| 1523 | "description": "Items field", |
| 1524 | }, |
| 1525 | }, |
| 1526 | } |
| 1527 | |
| 1528 | model_class = json_schema_to_model(json_schema) |
| 1529 | generated_schema = model_class.model_json_schema() |
| 1530 | |
| 1531 | count_prop = generated_schema["properties"]["count"] |
| 1532 | assert count_prop.get("default") == 42 |
| 1533 | assert isinstance(count_prop.get("default"), int) |
| 1534 | |
| 1535 | items_prop = generated_schema["properties"]["items"] |
| 1536 | assert items_prop.get("default") == [1, 2, 3] |
| 1537 | assert isinstance(items_prop.get("default"), list) |
| 1538 | |
| 1539 | @pytest.mark.unit |
| 1540 | @pytest.mark.schema |
| 1541 | def test_boolean_coercion_case_insensitive(self): |
| 1542 | """Test that boolean coercion handles various case combinations.""" |
| 1543 | test_cases = [ |
| 1544 | ("TRUE", True), |
| 1545 | ("True", True), |
| 1546 | ("FALSE", False), |
| 1547 | ("False", False), |
| 1548 | ("YES", True), |
| 1549 | ("Yes", True), |
| 1550 | ("NO", False), |
| 1551 | ("No", False), |
| 1552 | ] |
| 1553 | |
| 1554 | for string_value, expected_bool in test_cases: |
| 1555 | json_schema = { |
| 1556 | "title": "TestRequest", |
| 1557 | "type": "object", |
| 1558 | "properties": { |
| 1559 | "flag": { |
| 1560 | "type": "boolean", |
| 1561 | "default": string_value, |
| 1562 | }, |
| 1563 | }, |
| 1564 | } |
| 1565 | |
| 1566 | model_class = json_schema_to_model(json_schema) |
| 1567 | generated_schema = model_class.model_json_schema() |
| 1568 | prop = generated_schema["properties"]["flag"] |
| 1569 | |
| 1570 | assert prop.get("default") is expected_bool, ( |
| 1571 | f"Expected '{string_value}' to coerce to {expected_bool}" |
| 1572 | ) |
| 1573 | |
| 1574 | |
| 1575 | class TestGetSignatureFormatFromSchemaParams: |
| 1576 | """Test cases for get_signature_format_from_schema_params union handling.""" |
| 1577 | |
| 1578 | @staticmethod |
| 1579 | def _parameter(schema): |
| 1580 | params = get_signature_format_from_schema_params(schema) |
| 1581 | assert len(params) == 1 |
| 1582 | return params[0] |
| 1583 | |
| 1584 | @classmethod |
| 1585 | def _annotation(cls, schema): |
| 1586 | return cls._parameter(schema).annotation |
| 1587 | |
| 1588 | @pytest.mark.unit |
| 1589 | @pytest.mark.schema |
| 1590 | def test_oneof_four_members(self): |
| 1591 | """oneOf with 4 options builds a Union instead of raising ValueError.""" |
| 1592 | schema = { |
| 1593 | "properties": { |
| 1594 | "value": { |
| 1595 | "oneOf": [ |
| 1596 | {"type": "string"}, |
| 1597 | {"type": "integer"}, |
| 1598 | {"type": "boolean"}, |
| 1599 | {"type": "number"}, |
| 1600 | ] |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | annotation = self._annotation(schema) |
| 1606 | assert t.get_origin(annotation) is t.Union |
| 1607 | assert set(t.get_args(annotation)) == {str, int, bool, float} |
| 1608 | |
| 1609 | @pytest.mark.unit |
| 1610 | @pytest.mark.schema |
| 1611 | def test_oneof_five_members(self): |
| 1612 | """oneOf with more than four options is also supported (no 3-member cap).""" |
| 1613 | schema = { |
| 1614 | "properties": { |
| 1615 | "value": { |
| 1616 | "oneOf": [ |
| 1617 | {"type": "string"}, |
| 1618 | {"type": "integer"}, |
| 1619 | {"type": "boolean"}, |
| 1620 | {"type": "number"}, |
| 1621 | {"type": "array"}, |
| 1622 | ] |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | annotation = self._annotation(schema) |
| 1628 | assert t.get_origin(annotation) is t.Union |
| 1629 | assert len(t.get_args(annotation)) == 5 |
| 1630 | |
| 1631 | @pytest.mark.unit |
| 1632 | @pytest.mark.schema |
| 1633 | def test_anyof_option_missing_type(self): |
| 1634 | """An anyOf option without a 'type' key maps to Any instead of raising KeyError.""" |
| 1635 | schema = { |
| 1636 | "properties": { |
| 1637 | "value": { |
| 1638 | "anyOf": [ |
| 1639 | {"description": "free-form value"}, |
| 1640 | {"type": "string"}, |
| 1641 | ] |
| 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | annotation = self._annotation(schema) |
| 1647 | assert t.get_origin(annotation) is t.Union |
| 1648 | args = t.get_args(annotation) |
| 1649 | assert t.Any in args |
| 1650 | assert str in args |
| 1651 | |
| 1652 | @pytest.mark.unit |
| 1653 | @pytest.mark.schema |
| 1654 | def test_anyof_all_options_missing_type(self): |
| 1655 | """anyOf where every option lacks a 'type' collapses to a single Any annotation.""" |
| 1656 | schema = { |
| 1657 | "properties": { |
| 1658 | "value": { |
| 1659 | "anyOf": [ |
| 1660 | {"description": "a"}, |
| 1661 | {"description": "b"}, |
| 1662 | ] |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | assert self._annotation(schema) is t.Any |
| 1668 | |
| 1669 | @pytest.mark.unit |
| 1670 | @pytest.mark.schema |
| 1671 | def test_oneof_single_member(self): |
| 1672 | """oneOf with a single option resolves to that type directly (unchanged).""" |
| 1673 | schema = {"properties": {"value": {"oneOf": [{"type": "string"}]}}} |
| 1674 | assert self._annotation(schema) is str |
| 1675 | |
| 1676 | @pytest.mark.unit |
| 1677 | @pytest.mark.schema |
| 1678 | def test_oneof_two_members(self): |
| 1679 | """Two-member oneOf behavior is preserved (Union of the two types).""" |
| 1680 | schema = { |
| 1681 | "properties": { |
| 1682 | "value": {"oneOf": [{"type": "string"}, {"type": "integer"}]} |
| 1683 | } |
| 1684 | } |
| 1685 | assert self._annotation(schema) == t.Union[str, int] |
| 1686 | |
| 1687 | @pytest.mark.unit |
| 1688 | @pytest.mark.schema |
| 1689 | def test_oneof_three_members(self): |
| 1690 | """Three-member oneOf behavior is preserved (Union of the three types).""" |
| 1691 | schema = { |
| 1692 | "properties": { |
| 1693 | "value": { |
| 1694 | "oneOf": [ |
| 1695 | {"type": "string"}, |
| 1696 | {"type": "integer"}, |
| 1697 | {"type": "boolean"}, |
| 1698 | ] |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | assert self._annotation(schema) == t.Union[str, int, bool] |
| 1703 | |
| 1704 | @pytest.mark.unit |
| 1705 | @pytest.mark.schema |
| 1706 | def test_anyof_with_null_member_still_resolves(self): |
| 1707 | """Nullable anyOf [type, null] continues to resolve without raising.""" |
| 1708 | schema = { |
| 1709 | "properties": {"value": {"anyOf": [{"type": "string"}, {"type": "null"}]}} |
| 1710 | } |
| 1711 | |
| 1712 | annotation = self._annotation(schema) |
| 1713 | assert t.get_origin(annotation) is t.Union |
| 1714 | args = t.get_args(annotation) |
| 1715 | assert str in args |
| 1716 | assert type(None) in args |
| 1717 | |
| 1718 | @pytest.mark.unit |
| 1719 | @pytest.mark.schema |
| 1720 | @pytest.mark.parametrize( |
| 1721 | ("schema_type", "expected_members"), |
| 1722 | [ |
| 1723 | (["string", "null"], {str, type(None)}), |
| 1724 | (["integer"], {int}), |
| 1725 | (["string", "integer"], {str, int}), |
| 1726 | (["file", "null"], {t.Any, type(None)}), |
| 1727 | ], |
| 1728 | ) |
| 1729 | def test_type_list_resolves(self, schema_type, expected_members): |
| 1730 | """List-valued JSON Schema types produce an annotation without raising.""" |
| 1731 | annotation = self._annotation({"properties": {"value": {"type": schema_type}}}) |
| 1732 | actual = set(t.get_args(annotation)) if t.get_args(annotation) else {annotation} |
| 1733 | assert expected_members <= actual |
| 1734 | |
| 1735 | @pytest.mark.unit |
| 1736 | @pytest.mark.schema |
| 1737 | @pytest.mark.parametrize( |
| 1738 | ("schema_type", "expected_annotation", "expected_default"), |
| 1739 | [ |
| 1740 | (["integer"], int, 0), |
| 1741 | (["boolean"], bool, False), |
| 1742 | (["array"], t.List, []), |
| 1743 | ], |
| 1744 | ) |
| 1745 | def test_single_type_list_uses_scalar_fallback( |
| 1746 | self, schema_type, expected_annotation, expected_default |
| 1747 | ): |
| 1748 | """A one-item type list keeps its scalar type's implicit default.""" |
| 1749 | parameter = self._parameter({"properties": {"value": {"type": schema_type}}}) |
| 1750 | |
| 1751 | assert parameter.annotation is expected_annotation |
| 1752 | assert parameter.default == expected_default |
| 1753 | |
| 1754 | @pytest.mark.unit |
| 1755 | @pytest.mark.schema |
| 1756 | def test_single_type_list_preserves_explicit_default(self): |
| 1757 | """An explicit default overrides the fallback for a one-item type list.""" |
| 1758 | parameter = self._parameter( |
| 1759 | {"properties": {"value": {"type": ["integer"], "default": 42}}} |
| 1760 | ) |
| 1761 | |
| 1762 | assert parameter.default == 42 |
| 1763 | |
| 1764 | @pytest.mark.unit |
| 1765 | @pytest.mark.schema |
| 1766 | def test_multi_type_list_keeps_union_fallback(self): |
| 1767 | """A multi-type list remains a union with the existing empty-string fallback.""" |
| 1768 | parameter = self._parameter( |
| 1769 | {"properties": {"value": {"type": ["integer", "null"]}}} |
| 1770 | ) |
| 1771 | |
| 1772 | assert {int, type(None)} <= set(t.get_args(parameter.annotation)) |
| 1773 | assert parameter.default == "" |
| 1774 | |
| 1775 | @pytest.mark.unit |
| 1776 | @pytest.mark.schema |
| 1777 | def test_combiner_option_with_type_list_resolves(self): |
| 1778 | """List-valued types inside combiners do not reach a scalar dict lookup.""" |
| 1779 | schema = { |
| 1780 | "properties": { |
| 1781 | "value": { |
| 1782 | "anyOf": [ |
| 1783 | {"type": ["string", "null"]}, |
| 1784 | {"type": "integer"}, |
| 1785 | ] |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | annotation = self._annotation(schema) |
| 1791 | assert {str, int, type(None)} <= set(t.get_args(annotation)) |
| 1792 | |
| 1793 | |
| 1794 | if __name__ == "__main__": |
| 1795 | pytest.main([__file__, "-v"]) |
| 1796 | |
| 1797 | |
| 1798 | class TestSharedObjectCorpusThroughJsonSchemaToModel: |
| 1799 | """`json_schema_to_model` must satisfy the shared cross-SDK object contract. |
| 1800 | |
| 1801 | This is the provider-facing entry point (`args_schema`), so a model that |
| 1802 | accepts an argument but discards it is as much a defect as one that rejects |
| 1803 | a valid payload. |
| 1804 | """ |
| 1805 | |
| 1806 | @pytest.mark.parametrize( |
| 1807 | "case,index", |
| 1808 | [ |
| 1809 | (case, index) |
| 1810 | for case in load_object_cases() |
| 1811 | for index in range(len(case.instances)) |
| 1812 | ], |
| 1813 | ids=[ |
| 1814 | f"{case.id}[{index}]" |
| 1815 | for case in load_object_cases() |
| 1816 | for index in range(len(case.instances)) |
| 1817 | ], |
| 1818 | ) |
| 1819 | def test_case(self, case, index: int) -> None: |
| 1820 | instance = case.instances[index] |
| 1821 | model = json_schema_to_model(case.schema_) |
| 1822 | |
| 1823 | if not instance.accepted_for("python"): |
| 1824 | with pytest.raises(ValidationError): |
| 1825 | model.model_validate(instance.input) |
| 1826 | return |
| 1827 | |
| 1828 | result = model.model_validate(instance.input) |
| 1829 | if instance.python is not None and instance.python.has_output: |
| 1830 | assert ( |
| 1831 | result.model_dump(mode="json", by_alias=True) == instance.python.output |
| 1832 | ) |
| 1833 | |
| 1834 | def test_free_form_content_is_readable_as_attributes(self) -> None: |
| 1835 | """Preserved dynamic keys must survive `getattr`, not just `model_dump`.""" |
| 1836 | case = find_case("root-free-form-absent-properties") |
| 1837 | model = json_schema_to_model(case.schema_) |
| 1838 | |
| 1839 | result = model.model_validate(case.instances[0].input) |
| 1840 | |
| 1841 | assert getattr(result, "anything") == {"a": 1} |
| 1842 | assert getattr(result, "other") == "x" |
| 1843 | |
| 1844 | @pytest.mark.parametrize( |
| 1845 | ("case_id", "expected"), |
| 1846 | [ |
| 1847 | ("named-properties-strict-by-default", {"additionalProperties": False}), |
| 1848 | ( |
| 1849 | "root-additional-properties-schema-valued", |
| 1850 | {"additionalProperties": {"type": "number"}}, |
| 1851 | ), |
| 1852 | ( |
| 1853 | "pattern-only-object", |
| 1854 | {"patternProperties": {"^s_": {"type": "string"}}}, |
| 1855 | ), |
| 1856 | ], |
| 1857 | ) |
| 1858 | def test_model_json_schema_preserves_root_object_policy( |
| 1859 | self, |
| 1860 | case_id: str, |
| 1861 | expected: t.Dict[str, t.Any], |
| 1862 | ) -> None: |
| 1863 | model = json_schema_to_model(find_case(case_id).schema_) |
| 1864 | advertised = model.model_json_schema() |
| 1865 | |
| 1866 | for key, value in expected.items(): |
| 1867 | assert advertised[key] == value |
| 1868 | |
| 1869 | def test_dynamic_schema_resolves_local_json_pointer(self) -> None: |
| 1870 | schema = { |
| 1871 | "$defs": {"positive": {"type": "integer", "minimum": 1}}, |
| 1872 | "type": "object", |
| 1873 | "patternProperties": {"^count_": {"$ref": "#/$defs/positive"}}, |
| 1874 | } |
| 1875 | model = json_schema_to_model(schema) |
| 1876 | |
| 1877 | assert model.model_validate({"count_a": 1}).model_dump() == {"count_a": 1} |
| 1878 | with pytest.raises(ValidationError): |
| 1879 | model.model_validate({"count_a": 0}) |
| 1880 | |
| 1881 | def test_nested_dynamic_schema_resolves_against_document_root(self) -> None: |
| 1882 | schema = { |
| 1883 | "$defs": {"positive": {"type": "integer", "minimum": 1}}, |
| 1884 | "type": "object", |
| 1885 | "properties": { |
| 1886 | "payload": { |
| 1887 | "type": "object", |
| 1888 | "patternProperties": {"^count_": {"$ref": "#/$defs/positive"}}, |
| 1889 | "additionalProperties": False, |
| 1890 | } |
| 1891 | }, |
| 1892 | "required": ["payload"], |
| 1893 | } |
| 1894 | model = json_schema_to_model(schema) |
| 1895 | |
| 1896 | result = model.model_validate({"payload": {"count_a": 1}}) |
| 1897 | assert result.model_dump() == {"payload": {"count_a": 1}} |
| 1898 | with pytest.raises(ValidationError): |
| 1899 | model.model_validate({"payload": {"count_a": 0}}) |
| 1900 | |
| 1901 | def test_dynamic_schema_materialization_cannot_reject_valid_input(self) -> None: |
| 1902 | schema = { |
| 1903 | "type": "object", |
| 1904 | "patternProperties": {"^value_": {"enum": [1], "default": 1}}, |
| 1905 | } |
| 1906 | model = json_schema_to_model(schema) |
| 1907 | |
| 1908 | assert model.model_validate({"value_a": 1}).model_dump() == {"value_a": 1} |
| 1909 | |
| 1910 | @pytest.mark.parametrize( |
| 1911 | "reference", |
| 1912 | ["#/$defs/missing", "https://example.com/schema.json"], |
| 1913 | ) |
| 1914 | def test_dynamic_schema_rejects_unresolvable_reference( |
| 1915 | self, |
| 1916 | reference: str, |
| 1917 | ) -> None: |
| 1918 | schema = { |
| 1919 | "type": "object", |
| 1920 | "patternProperties": {"^value_": {"$ref": reference}}, |
| 1921 | } |
| 1922 | |
| 1923 | with pytest.raises(ValueError, match="schema reference"): |
| 1924 | json_schema_to_model(schema) |
| 1925 | |
| 1926 | def test_dynamic_schema_rejects_invalid_pattern(self) -> None: |
| 1927 | schema = { |
| 1928 | "type": "object", |
| 1929 | "patternProperties": {"[": {"type": "string"}}, |
| 1930 | } |
| 1931 | |
| 1932 | with pytest.raises(ValueError, match="Invalid patternProperties"): |
| 1933 | json_schema_to_model(schema) |
| 1934 | |
| 1935 | def test_dynamic_schema_checks_references_inside_local_target(self) -> None: |
| 1936 | schema = { |
| 1937 | "$defs": {"nested": {"$ref": "https://example.com/external-schema.json"}}, |
| 1938 | "type": "object", |
| 1939 | "patternProperties": {"^value_": {"$ref": "#/$defs/nested"}}, |
| 1940 | } |
| 1941 | |
| 1942 | with pytest.raises(ValueError, match="must be a local JSON Pointer"): |
| 1943 | json_schema_to_model(schema) |
| 1944 | |
| 1945 | @pytest.mark.parametrize( |
| 1946 | "keyword,value,accepted", |
| 1947 | [ |
| 1948 | ( |
| 1949 | "default", |
| 1950 | {"$ref": "https://example.com/schema.json"}, |
| 1951 | {"$ref": "kept"}, |
| 1952 | ), |
| 1953 | ("const", {"$ref": "other.json#/thing"}, {"$ref": "other.json#/thing"}), |
| 1954 | ("enum", [{"$ref": "#/$defs/missing"}], {"$ref": "#/$defs/missing"}), |
| 1955 | ("examples", [{"$ref": "#anchor"}], {"$ref": "kept"}), |
| 1956 | ], |
| 1957 | ) |
| 1958 | def test_dynamic_schema_ignores_reference_shaped_instance_data( |
| 1959 | self, |
| 1960 | keyword: str, |
| 1961 | value: t.Any, |
| 1962 | accepted: t.Dict[str, t.Any], |
| 1963 | ) -> None: |
| 1964 | """`$ref`-shaped payloads are data, not references, and must not raise.""" |
| 1965 | schema = { |
| 1966 | "type": "object", |
| 1967 | "patternProperties": {"^value_": {"type": "object", keyword: value}}, |
| 1968 | } |
| 1969 | |
| 1970 | model = json_schema_to_model(schema) |
| 1971 | |
| 1972 | assert model.model_validate({"value_a": accepted}).model_dump() == { |
| 1973 | "value_a": accepted |
| 1974 | } |
| 1975 | |
| 1976 | @pytest.mark.parametrize( |
| 1977 | "dynamic_schema", |
| 1978 | [ |
| 1979 | {"type": "array", "items": {"$ref": "https://example.com/schema.json"}}, |
| 1980 | {"allOf": [{"$ref": "https://example.com/schema.json"}]}, |
| 1981 | { |
| 1982 | "type": "object", |
| 1983 | "properties": {"inner": {"$ref": "https://example.com/schema.json"}}, |
| 1984 | }, |
| 1985 | { |
| 1986 | "type": "object", |
| 1987 | "additionalProperties": { |
| 1988 | "$ref": "https://example.com/schema.json", |
| 1989 | }, |
| 1990 | }, |
| 1991 | ], |
| 1992 | ) |
| 1993 | def test_dynamic_schema_checks_references_in_nested_schema_positions( |
| 1994 | self, |
| 1995 | dynamic_schema: t.Dict[str, t.Any], |
| 1996 | ) -> None: |
| 1997 | schema = { |
| 1998 | "type": "object", |
| 1999 | "patternProperties": {"^value_": dynamic_schema}, |
| 2000 | } |
| 2001 | |
| 2002 | with pytest.raises(ValueError, match="must be a local JSON Pointer"): |
| 2003 | json_schema_to_model(schema) |