Skip to content

Commit 4cf45b9

Browse files
committed
Feedback: Use jsonpath-ng to re-parse the rendered JSON path
1 parent 2680f6a commit 4cf45b9

File tree

2 files changed

+33
-94
lines changed

2 files changed

+33
-94
lines changed

jsonschema/tests/test_exceptions.py

Lines changed: 32 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from unittest import TestCase
22
import textwrap
33

4+
import jsonpath_ng
5+
46
from jsonschema import exceptions
57
from jsonschema.validators import _LATEST_VERSION
68

@@ -703,119 +705,55 @@ def test_hashable(self):
703705

704706

705707
class TestJsonPathRendering(TestCase):
706-
def test_str(self):
707-
e = exceptions.ValidationError(
708-
path=["x"],
708+
def validate_json_path_rendering(self, property_name, expected_path):
709+
error = exceptions.ValidationError(
710+
path=[property_name],
709711
message="1",
710712
validator="foo",
711713
instance="i1",
712714
)
713-
self.assertEqual(e.json_path, "$.x")
714715

715-
def test_empty_str(self):
716-
e = exceptions.ValidationError(
717-
path=[""],
718-
message="1",
719-
validator="foo",
720-
instance="i1",
721-
)
722-
self.assertEqual(e.json_path, "$['']")
716+
rendered_json_path = error.json_path
717+
self.assertEqual(rendered_json_path, expected_path)
723718

724-
def test_numeric_str(self):
725-
e = exceptions.ValidationError(
726-
path=["1"],
727-
message="1",
728-
validator="foo",
729-
instance="i1",
730-
)
731-
self.assertEqual(e.json_path, "$['1']")
719+
re_parsed_name = jsonpath_ng.parse(rendered_json_path).right.fields[0]
720+
self.assertEqual(re_parsed_name, property_name)
732721

733-
def test_period_str(self):
734-
e = exceptions.ValidationError(
735-
path=["."],
736-
message="1",
737-
validator="foo",
738-
instance="i1",
739-
)
740-
self.assertEqual(e.json_path, "$['.']")
722+
def test_basic(self):
723+
self.validate_json_path_rendering("x", "$.x")
741724

742-
def test_single_quote_str(self):
743-
e = exceptions.ValidationError(
744-
path=["'"],
745-
message="1",
746-
validator="foo",
747-
instance="i1",
748-
)
749-
self.assertEqual(e.json_path, r"$['\'']")
725+
def test_empty(self):
726+
self.validate_json_path_rendering("", "$['']")
750727

751-
def test_space_str(self):
752-
e = exceptions.ValidationError(
753-
path=[" "],
754-
message="1",
755-
validator="foo",
756-
instance="i1",
757-
)
758-
self.assertEqual(e.json_path, "$[' ']")
728+
def test_number(self):
729+
self.validate_json_path_rendering("1", "$['1']")
759730

760-
def test_backslash_str(self):
761-
e = exceptions.ValidationError(
762-
path=["\\"],
763-
message="1",
764-
validator="foo",
765-
instance="i1",
766-
)
767-
self.assertEqual(e.json_path, r"$['\\']")
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"$['\\']")
768742

769743
def test_backslash_single_quote(self):
770-
e = exceptions.ValidationError(
771-
path=[r"\'"],
772-
message="1",
773-
validator="foo",
774-
instance="i1",
775-
)
776-
self.assertEqual(e.json_path, r"$['\\\'']")
744+
self.validate_json_path_rendering(r"\'", r"$['\\\'']")
777745

778746
def test_underscore(self):
779-
e = exceptions.ValidationError(
780-
path=["_"],
781-
message="1",
782-
validator="foo",
783-
instance="i1",
784-
)
785-
self.assertEqual(e.json_path, r"$['_']")
747+
self.validate_json_path_rendering("_", r"$['_']")
786748

787749
def test_double_quote(self):
788-
e = exceptions.ValidationError(
789-
path=['"'],
790-
message="1",
791-
validator="foo",
792-
instance="i1",
793-
)
794-
self.assertEqual(e.json_path, """$['"']""")
750+
self.validate_json_path_rendering('"', """$['"']""")
795751

796752
def test_hyphen(self):
797-
e = exceptions.ValidationError(
798-
path=["-"],
799-
message="1",
800-
validator="foo",
801-
instance="i1",
802-
)
803-
self.assertEqual(e.json_path, "$['-']")
753+
self.validate_json_path_rendering("-", "$['-']")
804754

805755
def test_json_path_injection(self):
806-
e = exceptions.ValidationError(
807-
path=["a[0]"],
808-
message="1",
809-
validator="foo",
810-
instance="i1",
811-
)
812-
self.assertEqual(e.json_path, "$['a[0]']")
756+
self.validate_json_path_rendering("a[0]", "$['a[0]']")
813757

814758
def test_open_bracket(self):
815-
e = exceptions.ValidationError(
816-
path=["["],
817-
message="1",
818-
validator="foo",
819-
instance="i1",
820-
)
821-
self.assertEqual(e.json_path, "$['[']")
759+
self.validate_json_path_rendering("[", "$['[']")

noxfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def tests(session, installable):
6161
env = dict(JSON_SCHEMA_TEST_SUITE=str(ROOT / "json"))
6262

6363
session.install("virtue", installable)
64+
session.install("jsonpath-ng", installable)
6465

6566
if session.posargs and session.posargs[0] == "coverage":
6667
if len(session.posargs) > 1 and session.posargs[1] == "github":

0 commit comments

Comments
 (0)