|
3 | 3 | from typing import Any |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +from pydantic import BaseModel |
6 | 7 |
|
7 | 8 | from uipath_langchain.agent.exceptions import AgentStartupError, AgentStartupErrorCode |
8 | 9 | from uipath_langchain.agent.react.jsonschema_pydantic_converter import ( |
| 10 | + _RESERVED_FIELD_NAMES, |
| 11 | + _rename_reserved_properties, |
9 | 12 | create_model, |
10 | 13 | has_underscore_fields, |
11 | 14 | ) |
@@ -309,3 +312,224 @@ def test_nested_underscore_field(self) -> None: |
309 | 312 | assert exc_info.value.error_info.code == AgentStartupError.full_code( |
310 | 313 | AgentStartupErrorCode.UNDERSCORE_SCHEMA |
311 | 314 | ) |
| 315 | + |
| 316 | + |
| 317 | +class TestRenameReservedProperties: |
| 318 | + """Tests for _rename_reserved_properties schema pre-processing.""" |
| 319 | + |
| 320 | + def test_renames_schema_field(self) -> None: |
| 321 | + schema = { |
| 322 | + "type": "object", |
| 323 | + "properties": { |
| 324 | + "schema": {"type": "string"}, |
| 325 | + "name": {"type": "string"}, |
| 326 | + }, |
| 327 | + "required": ["schema", "name"], |
| 328 | + } |
| 329 | + modified, renames = _rename_reserved_properties(schema) |
| 330 | + |
| 331 | + assert "schema_" in modified["properties"] |
| 332 | + assert "schema" not in modified["properties"] |
| 333 | + assert "name" in modified["properties"] |
| 334 | + assert renames == {"schema_": "schema"} |
| 335 | + assert modified["required"] == ["schema_", "name"] |
| 336 | + |
| 337 | + def test_renames_multiple_reserved_fields(self) -> None: |
| 338 | + schema = { |
| 339 | + "type": "object", |
| 340 | + "properties": { |
| 341 | + "schema": {"type": "string"}, |
| 342 | + "copy": {"type": "string"}, |
| 343 | + "validate": {"type": "string"}, |
| 344 | + "name": {"type": "string"}, |
| 345 | + }, |
| 346 | + } |
| 347 | + modified, renames = _rename_reserved_properties(schema) |
| 348 | + |
| 349 | + assert "schema_" in modified["properties"] |
| 350 | + assert "copy_" in modified["properties"] |
| 351 | + assert "validate_" in modified["properties"] |
| 352 | + assert "name" in modified["properties"] |
| 353 | + assert len(renames) == 3 |
| 354 | + |
| 355 | + def test_handles_collision_with_existing_field(self) -> None: |
| 356 | + """When 'schema_' already exists, 'schema' should become 'schema__'.""" |
| 357 | + schema = { |
| 358 | + "type": "object", |
| 359 | + "properties": { |
| 360 | + "schema": {"type": "string"}, |
| 361 | + "schema_": {"type": "string"}, |
| 362 | + }, |
| 363 | + } |
| 364 | + modified, renames = _rename_reserved_properties(schema) |
| 365 | + |
| 366 | + assert "schema__" in modified["properties"] |
| 367 | + assert "schema_" in modified["properties"] |
| 368 | + assert renames["schema__"] == "schema" |
| 369 | + |
| 370 | + def test_renames_in_defs(self) -> None: |
| 371 | + schema = { |
| 372 | + "type": "object", |
| 373 | + "properties": {"name": {"type": "string"}}, |
| 374 | + "$defs": { |
| 375 | + "Inner": { |
| 376 | + "type": "object", |
| 377 | + "properties": { |
| 378 | + "schema": {"type": "string"}, |
| 379 | + }, |
| 380 | + "required": ["schema"], |
| 381 | + }, |
| 382 | + }, |
| 383 | + } |
| 384 | + modified, renames = _rename_reserved_properties(schema) |
| 385 | + |
| 386 | + inner = modified["$defs"]["Inner"] |
| 387 | + assert "schema_" in inner["properties"] |
| 388 | + assert inner["required"] == ["schema_"] |
| 389 | + |
| 390 | + def test_does_not_modify_original_schema(self) -> None: |
| 391 | + schema = { |
| 392 | + "type": "object", |
| 393 | + "properties": {"schema": {"type": "string"}}, |
| 394 | + } |
| 395 | + _rename_reserved_properties(schema) |
| 396 | + |
| 397 | + assert "schema" in schema["properties"] |
| 398 | + |
| 399 | + def test_no_renames_for_normal_fields(self) -> None: |
| 400 | + schema = { |
| 401 | + "type": "object", |
| 402 | + "properties": { |
| 403 | + "name": {"type": "string"}, |
| 404 | + "age": {"type": "integer"}, |
| 405 | + }, |
| 406 | + } |
| 407 | + modified, renames = _rename_reserved_properties(schema) |
| 408 | + |
| 409 | + assert renames == {} |
| 410 | + assert modified["properties"] == schema["properties"] |
| 411 | + |
| 412 | + |
| 413 | +class TestCreateModelWithReservedFields: |
| 414 | + """Tests for create_model handling of reserved field names.""" |
| 415 | + |
| 416 | + def test_schema_field_creates_valid_model(self) -> None: |
| 417 | + schema = { |
| 418 | + "title": "Input", |
| 419 | + "type": "object", |
| 420 | + "properties": { |
| 421 | + "schema": {"type": "string"}, |
| 422 | + "name": {"type": "string"}, |
| 423 | + }, |
| 424 | + } |
| 425 | + model = create_model(schema) |
| 426 | + |
| 427 | + assert issubclass(model, BaseModel) |
| 428 | + # BaseModel methods should still work |
| 429 | + assert callable(model.model_json_schema) |
| 430 | + assert callable(model.model_validate) |
| 431 | + |
| 432 | + def test_model_validate_accepts_original_names(self) -> None: |
| 433 | + schema = { |
| 434 | + "title": "Input", |
| 435 | + "type": "object", |
| 436 | + "properties": { |
| 437 | + "schema": {"type": "string"}, |
| 438 | + "name": {"type": "string"}, |
| 439 | + }, |
| 440 | + } |
| 441 | + model = create_model(schema) |
| 442 | + |
| 443 | + instance = model.model_validate({"schema": "test_val", "name": "alice"}) |
| 444 | + assert instance is not None |
| 445 | + |
| 446 | + def test_model_dump_outputs_original_names(self) -> None: |
| 447 | + schema = { |
| 448 | + "title": "Input", |
| 449 | + "type": "object", |
| 450 | + "properties": { |
| 451 | + "schema": {"type": "string"}, |
| 452 | + "name": {"type": "string"}, |
| 453 | + }, |
| 454 | + } |
| 455 | + model = create_model(schema) |
| 456 | + |
| 457 | + instance = model.model_validate({"schema": "test_val", "name": "alice"}) |
| 458 | + dumped = instance.model_dump() |
| 459 | + |
| 460 | + assert dumped == {"schema": "test_val", "name": "alice"} |
| 461 | + |
| 462 | + def test_model_dump_json_mode_outputs_original_names(self) -> None: |
| 463 | + schema = { |
| 464 | + "title": "Input", |
| 465 | + "type": "object", |
| 466 | + "properties": { |
| 467 | + "schema": {"type": "string"}, |
| 468 | + }, |
| 469 | + } |
| 470 | + model = create_model(schema) |
| 471 | + |
| 472 | + instance = model.model_validate({"schema": "val"}) |
| 473 | + dumped = instance.model_dump(mode="json") |
| 474 | + |
| 475 | + assert dumped == {"schema": "val"} |
| 476 | + |
| 477 | + def test_model_json_schema_shows_original_names(self) -> None: |
| 478 | + schema = { |
| 479 | + "title": "Input", |
| 480 | + "type": "object", |
| 481 | + "properties": { |
| 482 | + "schema": {"type": "string"}, |
| 483 | + "copy": {"type": "integer"}, |
| 484 | + }, |
| 485 | + } |
| 486 | + model = create_model(schema) |
| 487 | + |
| 488 | + json_schema = model.model_json_schema() |
| 489 | + assert "schema" in json_schema["properties"] |
| 490 | + assert "copy" in json_schema["properties"] |
| 491 | + assert "schema_" not in json_schema["properties"] |
| 492 | + assert "copy_" not in json_schema["properties"] |
| 493 | + |
| 494 | + def test_multiple_reserved_fields(self) -> None: |
| 495 | + schema = { |
| 496 | + "title": "Input", |
| 497 | + "type": "object", |
| 498 | + "properties": { |
| 499 | + "schema": {"type": "string"}, |
| 500 | + "copy": {"type": "string"}, |
| 501 | + "validate": {"type": "string"}, |
| 502 | + "name": {"type": "string"}, |
| 503 | + }, |
| 504 | + } |
| 505 | + model = create_model(schema) |
| 506 | + |
| 507 | + instance = model.model_validate( |
| 508 | + {"schema": "s", "copy": "c", "validate": "v", "name": "n"} |
| 509 | + ) |
| 510 | + dumped = instance.model_dump() |
| 511 | + |
| 512 | + assert dumped == {"schema": "s", "copy": "c", "validate": "v", "name": "n"} |
| 513 | + |
| 514 | + def test_model_fields_field_does_not_shadow(self) -> None: |
| 515 | + """'model_fields' is in Pydantic's protected namespace — must not crash.""" |
| 516 | + schema = { |
| 517 | + "title": "Input", |
| 518 | + "type": "object", |
| 519 | + "properties": { |
| 520 | + "model_fields": {"type": "string"}, |
| 521 | + "name": {"type": "string"}, |
| 522 | + }, |
| 523 | + } |
| 524 | + model = create_model(schema) |
| 525 | + |
| 526 | + # model.model_fields should still be the Pydantic descriptor, not a field value |
| 527 | + assert isinstance(model.model_fields, dict) |
| 528 | + instance = model.model_validate({"model_fields": "test", "name": "n"}) |
| 529 | + assert instance.model_dump() == {"model_fields": "test", "name": "n"} |
| 530 | + |
| 531 | + def test_reserved_field_names_constant_contains_known_problematic_names( |
| 532 | + self, |
| 533 | + ) -> None: |
| 534 | + known_problematic = {"schema", "copy", "validate", "dict", "json", "construct"} |
| 535 | + assert known_problematic.issubset(_RESERVED_FIELD_NAMES) |
0 commit comments