From 19b82a6f1bd07c74fd36026571bbb5776fd4d6cc Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Wed, 8 Mar 2023 16:17:26 -0500 Subject: [PATCH] Don't blow up for boolean additionalProperties on draft 4. --- referencing/jsonschema.py | 51 ++++++++++++++++++++++++++++++++++++--- suite | 2 +- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/referencing/jsonschema.py b/referencing/jsonschema.py index 18d14dd..a05b35b 100644 --- a/referencing/jsonschema.py +++ b/referencing/jsonschema.py @@ -224,6 +224,50 @@ def subresources_of(contents: Schema) -> Iterable[ObjectSchema]: return subresources_of +def _subresources_of_with_crazy_aP_items_dependencies( + in_value: Set[str] = frozenset(), + in_subvalues: Set[str] = frozenset(), + in_subarray: Set[str] = frozenset(), +): + """ + Specifically handle even older drafts where there are some funky keywords. + """ + + def subresources_of(contents: Schema) -> Iterable[ObjectSchema]: + if isinstance(contents, bool): + return + for each in in_value: + if each in contents: + yield contents[each] + for each in in_subarray: + if each in contents: + yield from contents[each] + for each in in_subvalues: + if each in contents: + yield from contents[each].values() + + items = contents.get("items") + if items is not None: + if isinstance(items, Sequence): + yield from items + else: + yield items + dependencies = contents.get("dependencies") + if dependencies is not None: + values = iter(dependencies.values()) + value = next(values, None) + if isinstance(value, Mapping): + yield value + yield from values + + for each in "additionalItems", "additionalProperties": + value = contents.get(each) + if isinstance(value, Mapping): + yield value + + return subresources_of + + def _maybe_in_subresource( in_value: Set[str] = frozenset(), in_subvalues: Set[str] = frozenset(), @@ -468,8 +512,8 @@ def maybe_in_subresource( DRAFT4 = Specification( name="draft-04", id_of=_legacy_id, - subresources_of=_subresources_of_with_crazy_items_dependencies( - in_value={"additionalItems", "additionalProperties", "not"}, + subresources_of=_subresources_of_with_crazy_aP_items_dependencies( + in_value={"not"}, in_subarray={"allOf", "anyOf", "oneOf"}, in_subvalues={"definitions", "patternProperties", "properties"}, ), @@ -484,8 +528,7 @@ def maybe_in_subresource( DRAFT3 = Specification( name="draft-03", id_of=_legacy_id, - subresources_of=_subresources_of_with_crazy_items_dependencies( - in_value={"additionalItems", "additionalProperties"}, + subresources_of=_subresources_of_with_crazy_aP_items_dependencies( in_subarray={"extends"}, in_subvalues={"definitions", "patternProperties", "properties"}, ), diff --git a/suite b/suite index 58a1e53..dcf245a 160000 --- a/suite +++ b/suite @@ -1 +1 @@ -Subproject commit 58a1e53f15a2b5ed62e51c696d2879aed2d9ca78 +Subproject commit dcf245a6360941d2ccb30d16c0c7cb8886aac637