|
1 | 1 | from unittest import TestCase
|
2 | 2 | import textwrap
|
3 | 3 |
|
| 4 | +import jsonpath_ng |
| 5 | + |
4 | 6 | from jsonschema import exceptions
|
5 | 7 | from jsonschema.validators import _LATEST_VERSION
|
6 | 8 |
|
@@ -700,3 +702,58 @@ class TestHashable(TestCase):
|
700 | 702 | def test_hashable(self):
|
701 | 703 | {exceptions.ValidationError("")}
|
702 | 704 | {exceptions.SchemaError("")}
|
| 705 | + |
| 706 | + |
| 707 | +class TestJsonPathRendering(TestCase): |
| 708 | + def validate_json_path_rendering(self, property_name, expected_path): |
| 709 | + error = exceptions.ValidationError( |
| 710 | + path=[property_name], |
| 711 | + message="1", |
| 712 | + validator="foo", |
| 713 | + instance="i1", |
| 714 | + ) |
| 715 | + |
| 716 | + rendered_json_path = error.json_path |
| 717 | + self.assertEqual(rendered_json_path, expected_path) |
| 718 | + |
| 719 | + re_parsed_name = jsonpath_ng.parse(rendered_json_path).right.fields[0] |
| 720 | + self.assertEqual(re_parsed_name, property_name) |
| 721 | + |
| 722 | + def test_basic(self): |
| 723 | + self.validate_json_path_rendering("x", "$.x") |
| 724 | + |
| 725 | + def test_empty(self): |
| 726 | + self.validate_json_path_rendering("", "$['']") |
| 727 | + |
| 728 | + def test_number(self): |
| 729 | + self.validate_json_path_rendering("1", "$['1']") |
| 730 | + |
| 731 | + def test_period(self): |
| 732 | + self.validate_json_path_rendering(".", "$['.']") |
| 733 | + |
| 734 | + def test_single_quote(self): |
| 735 | + self.validate_json_path_rendering("'", r"$['\'']") |
| 736 | + |
| 737 | + def test_space(self): |
| 738 | + self.validate_json_path_rendering(" ", "$[' ']") |
| 739 | + |
| 740 | + def test_backslash(self): |
| 741 | + self.validate_json_path_rendering("\\", r"$['\\']") |
| 742 | + |
| 743 | + def test_backslash_single_quote(self): |
| 744 | + self.validate_json_path_rendering(r"\'", r"$['\\\'']") |
| 745 | + |
| 746 | + def test_underscore(self): |
| 747 | + self.validate_json_path_rendering("_", r"$['_']") |
| 748 | + |
| 749 | + def test_double_quote(self): |
| 750 | + self.validate_json_path_rendering('"', """$['"']""") |
| 751 | + |
| 752 | + def test_hyphen(self): |
| 753 | + self.validate_json_path_rendering("-", "$['-']") |
| 754 | + |
| 755 | + def test_json_path_injection(self): |
| 756 | + self.validate_json_path_rendering("a[0]", "$['a[0]']") |
| 757 | + |
| 758 | + def test_open_bracket(self): |
| 759 | + self.validate_json_path_rendering("[", "$['[']") |
0 commit comments