Skip to content

Commit 5fa2593

Browse files
kmichel-aivenshinnar
authored andcommitted
Fix unpredictable untyped simplification
When the type is not set, a dict is canonicalized by adding all types, then trying to join them to remove the unnecessary values. Because `_join` is not commutative or associative, the order of types matters and cause variations in the canonicalized output. If there are issues in some `_join` that cause errors, then the tests become flaky, for instance the exception below only happens when enumerating Jtypes start with `number` and is followed by `integer`. (Any other order will first create a `JSONanyOf` that will only attempt a real join on the `anyOf` alternatives if the types are identical which won't call the failing code because `number` != `integer`). ``` Traceback (most recent call last): File "/home/runner/work/jsonsubschema/jsonsubschema/test/test_numeric.py", line 649, in test_all_all_3 self.assertFalse(isSubschema(s2, s1)) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/api.py", line 56, in isSubschema s1, s2 = prepare_operands(s1, s2) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/api.py", line 46, in prepare_operands canonicalize_schema(s2)) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 35, in canonicalize_schema canonical_schema = canonicalize_dict(obj) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 84, in canonicalize_dict return canonicalize_connectors(d) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 218, in canonicalize_connectors allofs.append(canonicalize_dict({c: d[c]})) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 84, in canonicalize_dict return canonicalize_connectors(d) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 211, in canonicalize_connectors simplified = simplify_schema_and_embed_checkers(d) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 369, in simplify_schema_and_embed_checkers allofs = [simplify_schema_and_embed_checkers(i) for i in s["allOf"]] File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 369, in <listcomp> allofs = [simplify_schema_and_embed_checkers(i) for i in s["allOf"]] File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_canonicalization.py", line 366, in simplify_schema_and_embed_checkers return boolToConstructor.get("anyOf")({"anyOf": anyofs}) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_checkers.py", line 1481, in JSONanyOfFactory ret = ret.join(i) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_checkers.py", line 133, in join ret = self._join(s) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_checkers.py", line 686, in _join return _joinNumber(self, s) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_checkers.py", line 672, in _joinNumber gcd = utils.gcd(s1.multipleOf, s2.multipleOf) File "/home/runner/work/jsonsubschema/jsonsubschema/jsonsubschema/_utils.py", line 269, in gcd return fractions.gcd(x, y) AttributeError: module 'fractions' has no attribute 'gcd' ```
1 parent 595d925 commit 5fa2593

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

jsonsubschema/_canonicalization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def canonicalize_dict(d, outer_key=None):
9191
elif utils.is_list(t):
9292
return canonicalize_list_of_types(d)
9393
else:
94-
d["type"] = definitions.Jtypes
94+
d["type"] = sorted(definitions.Jtypes)
9595
return canonicalize_list_of_types(d)
9696

9797

0 commit comments

Comments
 (0)