Skip to content

Commit 00895cf

Browse files
committed
Handle nested anyOf
1 parent 5fc001e commit 00895cf

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,19 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
515515
if TRUTHY in schema.get("anyOf", ()):
516516
schema.pop("anyOf", None)
517517
if "anyOf" in schema:
518-
for i, s in enumerate(list(schema["anyOf"])):
518+
i = 0
519+
while i < len(schema["anyOf"]):
520+
s = schema["anyOf"][i]
519521
if set(s) == {"anyOf"}:
520522
schema["anyOf"][i : i + 1] = s["anyOf"]
521-
schema["anyOf"] = sorted(schema["anyOf"], key=encode_canonical_json)
522-
schema["anyOf"] = [s for s in schema["anyOf"] if s != FALSEY]
523+
continue
524+
i += 1
525+
schema["anyOf"] = [
526+
json.loads(s)
527+
for s in sorted(
528+
{encode_canonical_json(a) for a in schema["anyOf"] if a != FALSEY}
529+
)
530+
]
523531
if not schema["anyOf"]:
524532
return FALSEY
525533
if len(schema) == len(schema["anyOf"]) == 1:

tests/test_canonicalise.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def test_canonicalises_to_empty(schema):
196196
{"minItems": 1, "type": "array"},
197197
),
198198
({"anyOf": [{}, {"type": "null"}]}, {}),
199+
({"anyOf": [{"anyOf": [{"anyOf": [{"type": "null"}]}]}]}, {"const": None}),
199200
(
200201
{
201202
"anyOf": [

0 commit comments

Comments
 (0)