From 466c0133e7120c7ecff6a86d35ebb3d80fb4ad52 Mon Sep 17 00:00:00 2001 From: Zac-HD Date: Wed, 17 Nov 2021 00:46:32 +1100 Subject: [PATCH] More object canonicalisation Fixes #92 --- src/hypothesis_jsonschema/_canonicalise.py | 6 ++++++ tests/test_canonicalise.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/hypothesis_jsonschema/_canonicalise.py b/src/hypothesis_jsonschema/_canonicalise.py index 98b3d85..74bda2a 100644 --- a/src/hypothesis_jsonschema/_canonicalise.py +++ b/src/hypothesis_jsonschema/_canonicalise.py @@ -391,7 +391,13 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]: ): max_props = schema.get("maxProperties", math.inf) assert isinstance(max_props, (int, float)) + for k, v in list(schema["properties"].items()): + if v == FALSEY: + schema["properties"].pop(k) schema["maxProperties"] = min(max_props, len(schema["properties"])) + if schema.get("maxProperties", math.inf) == 0: + for k in ("properties", "patternProperties", "additionalProperties"): + schema.pop(k, None) if "object" in type_ and schema.get("minProperties", 0) > schema.get( "maxProperties", math.inf ): diff --git a/tests/test_canonicalise.py b/tests/test_canonicalise.py index f3edfd4..6ad0bf3 100644 --- a/tests/test_canonicalise.py +++ b/tests/test_canonicalise.py @@ -305,6 +305,27 @@ def test_canonicalises_to_empty(schema): {"type": "integer", "allOf": [{"multipleOf": 0.5}, {"multipleOf": 1e308}]}, {"type": "integer", "multipleOf": 1e308}, ), + ( + { + "additionalProperties": {"not": {}}, + "properties": {"a": {"not": {}}}, + "type": "object", + }, + {"maxProperties": 0, "type": "object"}, + ), + ( + { + "additionalProperties": {"not": {}}, + "properties": {"a": {"not": {}}, "b": {}}, + "type": "object", + }, + { + "additionalProperties": {"not": {}}, + "properties": {"b": {}}, + "maxProperties": 1, + "type": "object", + }, + ), ], ) def test_canonicalises_to_expected(schema, expected):