Skip to content

Commit 82563f9

Browse files
committed
Resolve before canonicalising
1 parent 0b8f4a0 commit 82563f9

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,18 @@ def resolve_remote(self, uri: str) -> NoReturn:
551551
)
552552

553553

554-
def resolve_all_refs(schema: Schema, *, resolver: LocalResolver = None) -> Schema:
554+
def resolve_all_refs(
555+
schema: Union[bool, Schema], *, resolver: LocalResolver = None
556+
) -> Schema:
555557
"""
556558
Resolve all references in the given schema.
557559
558560
This handles nested definitions, but not recursive definitions.
559561
The latter require special handling to convert to strategies and are much
560562
less common, so we just ignore them (and error out) for now.
561563
"""
564+
if isinstance(schema, bool):
565+
return canonicalish(schema)
562566
if resolver is None:
563567
resolver = LocalResolver.from_schema(deepcopy(schema))
564568
if not isinstance(resolver, jsonschema.RefResolver):
@@ -573,7 +577,7 @@ def resolve_all_refs(schema: Schema, *, resolver: LocalResolver = None) -> Schem
573577
if s == {}:
574578
return resolve_all_refs(got, resolver=resolver)
575579
m = merged([s, got])
576-
if m is None:
580+
if m is None: # pragma: no cover
577581
msg = f"$ref:{ref!r} had incompatible base schema {s!r}"
578582
raise HypothesisRefResolutionError(msg)
579583
return resolve_all_refs(m, resolver=resolver)

src/hypothesis_jsonschema/_from_schema.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def error_raiser() -> NoReturn:
8585

8686

8787
def __from_schema(schema: Union[bool, Schema]) -> st.SearchStrategy[JSONType]:
88+
try:
89+
schema = resolve_all_refs(schema)
90+
except RecursionError:
91+
raise HypothesisRefResolutionError(
92+
f"Could not resolve recursive references in schema={schema!r}"
93+
) from None
8894
schema = canonicalish(schema)
8995
# Boolean objects are special schemata; False rejects all and True accepts all.
9096
if schema == FALSEY:
@@ -97,13 +103,6 @@ def __from_schema(schema: Union[bool, Schema]) -> st.SearchStrategy[JSONType]:
97103
if schema["$schema"] == "http://json-schema.org/draft-03/schema#":
98104
raise InvalidArgument("Draft-03 schemas are not supported")
99105

100-
try:
101-
schema = resolve_all_refs(schema)
102-
except RecursionError:
103-
raise HypothesisRefResolutionError(
104-
f"Could not resolve recursive references in schema={schema!r}"
105-
) from None
106-
107106
# Now we handle as many validation keywords as we can...
108107
# Applying subschemata with boolean logic
109108
if "not" in schema:

tests/test_from_schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def test_invalid_schemas_raise(schema):
110110
"Jenkins X Pipeline YAML configuration files",
111111
"TypeScript compiler configuration file",
112112
"JSON Schema for GraphQL Mesh config file",
113+
"Configuration file for stylelint",
114+
"Travis CI configuration file",
115+
"JSON schema for ESLint configuration files",
116+
# TODO: work out how to use slow marker here
113117
}
114118

115119
with open(Path(__file__).parent / "corpus-schemastore-catalog.json") as f:

0 commit comments

Comments
 (0)