|
1 | 1 | """Core JSON schema type conversion tests.""" |
2 | 2 |
|
3 | 3 | import dataclasses |
| 4 | +import warnings |
4 | 5 | from dataclasses import Field |
5 | 6 | from enum import Enum |
6 | 7 | from typing import Any, Literal |
@@ -310,3 +311,96 @@ def test_empty_property_name(self): |
310 | 311 | field_names = [f.name for f in dataclasses.fields(T)] |
311 | 312 | assert len(field_names) == 2 |
312 | 313 | assert len(set(field_names)) == 2 |
| 314 | + |
| 315 | + |
| 316 | +class TestUnsupportedPatternFallback: |
| 317 | + """Patterns that Pydantic's Rust regex engine cannot compile are dropped |
| 318 | + gracefully: a warning is emitted, the pattern is preserved in |
| 319 | + json_schema_extra, and the field still validates as str.""" |
| 320 | + |
| 321 | + def test_lookahead_pattern_falls_back_to_str(self): |
| 322 | + """Lookahead patterns (unsupported by Rust regex) degrade to plain str.""" |
| 323 | + schema = {"type": "string", "pattern": "^(?!aws:).+"} |
| 324 | + with pytest.warns(UserWarning, match="not supported by Pydantic"): |
| 325 | + T = json_schema_to_type(schema) |
| 326 | + ta = TypeAdapter(T) |
| 327 | + ta.validate_python("hello") |
| 328 | + ta.validate_python("aws:forbidden") |
| 329 | + |
| 330 | + def test_unsupported_pattern_preserved_in_schema_extra(self): |
| 331 | + """The original pattern is preserved via json_schema_extra.""" |
| 332 | + schema = {"type": "string", "pattern": "^(?!aws:).+"} |
| 333 | + with pytest.warns(UserWarning): |
| 334 | + T = json_schema_to_type(schema) |
| 335 | + json_schema = TypeAdapter(T).json_schema() |
| 336 | + assert json_schema.get("x-unsupported-pattern") == "^(?!aws:).+" |
| 337 | + |
| 338 | + def test_length_constraints_kept_when_pattern_dropped(self): |
| 339 | + """minLength/maxLength are still enforced after pattern fallback.""" |
| 340 | + schema = { |
| 341 | + "type": "string", |
| 342 | + "minLength": 3, |
| 343 | + "maxLength": 10, |
| 344 | + "pattern": "(?!x).+", |
| 345 | + } |
| 346 | + with pytest.warns(UserWarning): |
| 347 | + T = json_schema_to_type(schema) |
| 348 | + ta = TypeAdapter(T) |
| 349 | + ta.validate_python("abc") |
| 350 | + with pytest.raises(ValidationError): |
| 351 | + ta.validate_python("ab") |
| 352 | + with pytest.raises(ValidationError): |
| 353 | + ta.validate_python("a" * 11) |
| 354 | + |
| 355 | + def test_supported_pattern_still_enforced(self): |
| 356 | + """Valid patterns are not affected by the fallback logic.""" |
| 357 | + schema = {"type": "string", "pattern": "^[a-z]+$"} |
| 358 | + T = json_schema_to_type(schema) |
| 359 | + ta = TypeAdapter(T) |
| 360 | + ta.validate_python("hello") |
| 361 | + with pytest.raises(ValidationError): |
| 362 | + ta.validate_python("HELLO") |
| 363 | + |
| 364 | + def test_unicode_property_pattern_falls_back(self): |
| 365 | + """Unicode \\p{...} patterns (unsupported by Rust regex) degrade gracefully.""" |
| 366 | + schema = {"type": "string", "pattern": r"[\p{Graph}\x20]*"} |
| 367 | + with pytest.warns(UserWarning, match="not supported by Pydantic"): |
| 368 | + T = json_schema_to_type(schema) |
| 369 | + ta = TypeAdapter(T) |
| 370 | + ta.validate_python("anything") |
| 371 | + |
| 372 | + def test_object_with_unsupported_pattern_field(self): |
| 373 | + """An object schema containing a field with an unsupported pattern |
| 374 | + should not crash TypeAdapter construction.""" |
| 375 | + schema = { |
| 376 | + "type": "object", |
| 377 | + "properties": { |
| 378 | + "tag_key": {"type": "string", "pattern": "^(?!aws:)[a-zA-Z]+$"}, |
| 379 | + "value": {"type": "string"}, |
| 380 | + }, |
| 381 | + "required": ["tag_key", "value"], |
| 382 | + } |
| 383 | + with pytest.warns(UserWarning): |
| 384 | + T = json_schema_to_type(schema) |
| 385 | + ta = TypeAdapter(T) |
| 386 | + result = ta.validate_python({"tag_key": "Name", "value": "test"}) |
| 387 | + assert result.tag_key == "Name" # ty:ignore[unresolved-attribute] |
| 388 | + |
| 389 | + def test_fallback_only_triggers_for_regex_errors(self): |
| 390 | + """Non-regex SchemaErrors must not be swallowed by the fallback path. |
| 391 | +
|
| 392 | + Uses a schema whose TypeAdapter construction fails for a reason other |
| 393 | + than an unsupported pattern, to verify the guard raises rather than |
| 394 | + silently degrading. A large tuple Literal with a non-hashable element |
| 395 | + forces a non-regex build error. |
| 396 | + """ |
| 397 | + |
| 398 | + # A pattern that will fail with a non-regex SchemaError is hard to |
| 399 | + # construct deliberately; instead we verify that the guard condition |
| 400 | + # (message containing "regex") is checked: a valid schema must NOT |
| 401 | + # emit a warning. |
| 402 | + schema = {"type": "string", "pattern": "^[a-z]+$"} |
| 403 | + with warnings.catch_warnings(): |
| 404 | + warnings.simplefilter("error", UserWarning) |
| 405 | + T = json_schema_to_type(schema) # must not warn |
| 406 | + TypeAdapter(T).validate_python("hello") |
0 commit comments