From 532f57bb29e396f573a3295b4bc8cb452df2ff97 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Wed, 5 Jun 2024 10:21:21 +0200 Subject: [PATCH 1/2] Fix join of enum with string The join of a string restricted by a pattern with an unrestricted string is the unrestricted string, because it contains every possible string. --- jsonsubschema/_checkers.py | 4 ---- test/test_string.py | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jsonsubschema/_checkers.py b/jsonsubschema/_checkers.py index d2033a1..f25a7e4 100644 --- a/jsonsubschema/_checkers.py +++ b/jsonsubschema/_checkers.py @@ -359,10 +359,6 @@ def _joinString(s1, s2): if s1_new_pattern and s2_new_pattern: ret["pattern"] = "^" + s1_new_pattern + \ "$|^" + s2_new_pattern + "$" - elif s1_new_pattern: - ret["pattern"] = "^" + s1_new_pattern + "$" - elif s2_new_pattern: - ret["pattern"] = "^" + s2_new_pattern + "$" return JSONTypeString(ret) else: return JSONanyOf({"anyOf": [s1, s2]}) diff --git a/test/test_string.py b/test/test_string.py index b5e1337..ae6b285 100644 --- a/test/test_string.py +++ b/test/test_string.py @@ -212,6 +212,11 @@ def test_enum3(self): with self.subTest(): self.assertFalse(isSubschema(s2, s1)) + def test_enum4(self): + s1 = {"anyOf": [{"enum": ["a", "b", "c"]}, {"type": "string"}]} + s2 = {"type": "string"} + self.assertTrue(isEquivalent(s1, s2)) + def test_not_enum1(self): s1 = {"type": "string", "not": {"enum": ["a"]}} s2 = {"type": "string"} From 2b4105ad01b2e3d2aed0c9a323c21cb5d32a44e6 Mon Sep 17 00:00:00 2001 From: Kevin Michel Date: Wed, 5 Jun 2024 16:45:26 +0200 Subject: [PATCH 2/2] Fix false negative in regex subset If s2 is a regex and s1 is empty or None, then s1 is equivalent to all the strings, but s2 can also be a regex equivalent to all the strings (for instance `.*|[abc]`). In that case, the two regex are equivalent and subsets of each other. --- jsonsubschema/_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonsubschema/_utils.py b/jsonsubschema/_utils.py index f423ae3..7c4d672 100644 --- a/jsonsubschema/_utils.py +++ b/jsonsubschema/_utils.py @@ -199,7 +199,7 @@ def regex_isSubset(s1, s2): elif s1: return True elif s2: - return False + return parse(s2).equivalent(parse(".*")) # def regex_isProperSubset(s1, s2):