Skip to content

Commit

Permalink
We CannotDetermineSpecification for $schema kyes that aren't strs
Browse files Browse the repository at this point in the history
(E.g. if something has such a key but isn't a JSON Schema).
  • Loading branch information
Julian committed Jan 5, 2024
1 parent 80c9781 commit adb7c67
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v0.32.1
-------

* Make ``Specification.detect`` raise a ``CannotDetermineSpecification`` error even if passed a mapping with a ``$schema`` key that doesn't match JSON Schema dialect semantics (e.g. a non-string).

v0.32.0
-------

Expand Down
6 changes: 2 additions & 4 deletions referencing/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ def _detect_or_error(contents: D) -> Specification[D]:
raise exceptions.CannotDetermineSpecification(contents)

jsonschema_dialect_id = contents.get("$schema") # type: ignore[reportUnknownMemberType]
if jsonschema_dialect_id is None:
if not isinstance(jsonschema_dialect_id, str):
raise exceptions.CannotDetermineSpecification(contents)

from referencing.jsonschema import specification_with

return specification_with(
jsonschema_dialect_id, # type: ignore[reportUnknownArgumentType]
)
return specification_with(jsonschema_dialect_id)


def _detect_or_default(
Expand Down
4 changes: 4 additions & 0 deletions referencing/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,10 @@ def test_detect_with_no_discernible_information(self):
with pytest.raises(exceptions.CannotDetermineSpecification):
Specification.detect({"foo": "bar"})

def test_detect_with_non_URI_schema(self):
with pytest.raises(exceptions.CannotDetermineSpecification):
Specification.detect({"$schema": 37})

def test_detect_with_no_discernible_information_and_default(self):
specification = Specification.OPAQUE.detect({"foo": "bar"})
assert specification is Specification.OPAQUE
Expand Down

0 comments on commit adb7c67

Please sign in to comment.