Skip to content

Commit 0b8f4a0

Browse files
committed
Remove too-tight assertion
1 parent 6115cfe commit 0b8f4a0

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
# Note that in some cases ("dependencies"), the value may be a list of strings.
5252
SCHEMA_OBJECT_KEYS = ("properties", "patternProperties", "dependencies")
5353
ALL_KEYWORDS = tuple(
54-
["$schema", *SCHEMA_KEYS, *SCHEMA_OBJECT_KEYS]
54+
[*SCHEMA_KEYS, *SCHEMA_OBJECT_KEYS]
5555
+ sum((s.split() for _, s in TYPE_SPECIFIC_KEYS), [])
5656
)
5757

@@ -737,15 +737,26 @@ def merged(schemas: List[Any]) -> Optional[Schema]:
737737
out["multipleOf"] = max(x, y)
738738
else:
739739
return None
740-
740+
# TODO: merge `contains` schemas when one is a subset of the other
741+
# TODO: merge `items` schemas or lists-of-schemas
742+
# TODO: merge `not` schemas as {not: anyOf: [not1, not2]}
743+
# TODO: merge if/then/else schemas to the chained form
744+
# or maybe canonicalise them to an anyOf instead?
745+
# TODO: merge dependencies
746+
747+
# This loop handles the remaining cases. Notably, we do not attempt to
748+
# merge distinct values for:
749+
# - `pattern`; computing regex intersection is out of scope
750+
# - `contains`; requires allOf and thus enters an infinite loop
751+
# - `$ref`; if not already resolved we can't do that here
752+
# - `anyOf`; due to product-like explosion in worst case
753+
# - `oneOf`; which we plan to handle as an anyOf-not composition
741754
for k, v in s.items():
742755
if k not in out:
743756
out[k] = v
744757
elif out[k] != v and k in ALL_KEYWORDS:
745758
# If non-validation keys like `title` or `description` don't match,
746759
# that doesn't really matter and we'll just go with first we saw.
747-
known = "$ref $schema anyOf oneOf items if not pattern contains"
748-
assert k in known.split(), (k, out[k], v)
749760
return None
750761
out = canonicalish(out)
751762
if out == FALSEY:

tests/test_canonicalise.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,6 @@ def test_merged(group, result):
338338
assert merged(group) == result
339339

340340

341-
def test_unable_to_merge_differnt_draft_versions():
342-
different_drafts = [
343-
{"$schema": "http://json-schema.org/draft-04/schema#"},
344-
{"$schema": "http://json-schema.org/draft-07/schema#"},
345-
]
346-
assert merged(different_drafts) is None
347-
348-
349341
@settings(suppress_health_check=[HealthCheck.too_slow], deadline=None)
350342
@given(json_schemata())
351343
def test_self_merge_eq_canonicalish(schema):
@@ -418,6 +410,11 @@ def _canonicalises_to_equivalent_fixpoint(data):
418410
jsonschema.validators.validator_for(schema).check_schema(schema)
419411

420412

413+
def test_canonicalise_is_only_valid_for_schemas():
414+
with pytest.raises(InvalidArgument):
415+
canonicalish("not a schema")
416+
417+
421418
# Expose fuzz targets in a form that FuzzBuzz can understand (no dotted names)
422419
fuzz_canonical_json_encoding = test_canonical_json_encoding.hypothesis.fuzz_one_input
423420
fuzz_merge_semantics = test_merge_semantics.hypothesis.fuzz_one_input

0 commit comments

Comments
 (0)