Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/hypothesis_jsonschema/_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
HypothesisRefResolutionError,
Schema,
canonicalish,
merged,
)


Expand Down Expand Up @@ -57,15 +56,9 @@ def resolve_all_refs(
)

if "$ref" in schema:
s = dict(schema)
ref = s.pop("$ref")
with resolver.resolving(ref) as got:
m = merged([s, resolve_all_refs(got, resolver=resolver)])
if m is None: # pragma: no cover
msg = f"$ref:{ref!r} had incompatible base schema {s!r}"
raise HypothesisRefResolutionError(msg)
assert "$ref" not in m
return m
# All supported drafts (04, 06, 07) ignore siblings of $ref.
with resolver.resolving(schema["$ref"]) as got:
return resolve_all_refs(got, resolver=resolver)
assert "$ref" not in schema

for key in SCHEMA_KEYS:
Expand Down
1 change: 1 addition & 0 deletions tests/corpus-suite-schemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,7 @@
"propertyNames": true
},
"draft7/ref overrides any sibling keywords": {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"reffed": {
"type": "array"
Expand Down
40 changes: 40 additions & 0 deletions tests/test_from_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,46 @@ def test_can_generate_with_explicit_schema_version(_):
pass


@pytest.mark.parametrize("draft", ["04", "06", "07"])
@pytest.mark.parametrize("definition_key", ["definitions", "$defs"])
@settings(deadline=None)
@given(data=st.data())
def test_ref_siblings_do_not_resolve_against_a_subschema(data, draft, definition_key):
# GH-97: items is a sibling of $ref, and must not be resolved or applied.
ref = f"#/{definition_key}/foo"
schema = {
"$schema": f"http://json-schema.org/draft-{draft}/schema#",
definition_key: {
"foo": {
"properties": {"foo": {"type": "string"}},
"additionalProperties": False,
},
},
"type": "object",
"properties": {
"bar": {
"$ref": ref,
"type": ["object", "array"],
"items": {"type": "object", "$ref": ref},
},
},
}
value = data.draw(from_schema(schema))
jsonschema.validate(value, schema)


@pytest.mark.parametrize("draft", ["04", "06", "07"])
@given(data=st.data())
def test_ref_ignores_conflicting_sibling_constraints(data, draft):
schema = {
"$schema": f"http://json-schema.org/draft-{draft}/schema#",
"definitions": {"value": {"enum": ["hello"]}},
"$ref": "#/definitions/value",
"type": "integer",
}
assert data.draw(from_schema(schema)) == "hello"


INVALID_SCHEMAS = {
# Empty list for requires, which is invalid
"Release Drafter configuration file",
Expand Down