Skip to content

Commit 5fc001e

Browse files
committed
Cap unique array size
This mostly unlocks a few ways to tell if the schema is empty later on.
1 parent 6c2508e commit 5fc001e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,14 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
359359
if schema["contains"] == TRUTHY:
360360
schema.pop("contains")
361361
schema["minItems"] = max(schema.get("minItems", 1), 1)
362-
# TODO: upper_bound_instances on the items, and use that to set maxItems
363-
# constraint for unique arrays. Not worth handling list-items though...
362+
if (
363+
"array" in type_
364+
and "uniqueItems" in schema
365+
and isinstance(schema.get("items", []), dict)
366+
):
367+
item_count = upper_bound_instances(schema["items"])
368+
if math.isfinite(item_count):
369+
schema["maxItems"] = min(item_count, schema.get("maxItems", math.inf))
364370
if "array" in type_ and schema.get("minItems", 0) > schema.get(
365371
"maxItems", math.inf
366372
):

tests/test_canonicalise.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ def test_canonicalises_to_empty(schema):
234234
"contains": {"type": "number", "multipleOf": 0.75},
235235
},
236236
),
237+
(
238+
{"type": "array", "items": {"const": 1}, "uniqueItems": True},
239+
{
240+
"type": "array",
241+
"items": {"const": 1},
242+
"uniqueItems": True,
243+
"maxItems": 1,
244+
},
245+
),
237246
],
238247
)
239248
def test_canonicalises_to_expected(schema, expected):

0 commit comments

Comments
 (0)