Skip to content

Commit 03b3b43

Browse files
committed
Merge contains schemas
1 parent afc292b commit 03b3b43

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,12 @@ def merged(schemas: List[Any]) -> Optional[Schema]:
751751
out["multipleOf"] = max(x, y)
752752
else:
753753
return None
754-
# TODO: merge `contains` schemas when one is a subset of the other
754+
if "contains" in out and "contains" in s and out["contains"] != s["contains"]:
755+
# If one `contains` schema is a subset of the other, we can discard it.
756+
m = merged([out["contains"], s["contains"]])
757+
if m == out["contains"] or m == s["contains"]:
758+
out["contains"] = m
759+
s.pop("contains")
755760
# TODO: merge `items` schemas or lists-of-schemas
756761
# TODO: merge `not` schemas as {not: anyOf: [not1, not2]}
757762
# TODO: merge if/then/else schemas to the chained form

tests/test_canonicalise.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ def test_canonicalises_to_expected(schema, expected):
335335
"properties": {"ab": {"const": True}},
336336
},
337337
),
338+
(
339+
[
340+
{"type": "array", "contains": {"type": "integer"}},
341+
{"type": "array", "contains": {"type": "number"}},
342+
],
343+
{"type": "array", "contains": {"type": "integer"}, "minItems": 1},
344+
),
338345
]
339346
+ [
340347
([{lo: 0, hi: 9}, {lo: 1, hi: 10}], {lo: 1, hi: 9})

0 commit comments

Comments
 (0)