Skip to content

Commit adb7c67

Browse files
committed
We CannotDetermineSpecification for $schema kyes that aren't strs
(E.g. if something has such a key but isn't a JSON Schema).
1 parent 80c9781 commit adb7c67

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

docs/changes.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
v0.32.1
6+
-------
7+
8+
* 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).
9+
510
v0.32.0
611
-------
712

referencing/_core.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@ def _detect_or_error(contents: D) -> Specification[D]:
4242
raise exceptions.CannotDetermineSpecification(contents)
4343

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

4848
from referencing.jsonschema import specification_with
4949

50-
return specification_with(
51-
jsonschema_dialect_id, # type: ignore[reportUnknownArgumentType]
52-
)
50+
return specification_with(jsonschema_dialect_id)
5351

5452

5553
def _detect_or_default(

referencing/tests/test_core.py

+4
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,10 @@ def test_detect_with_no_discernible_information(self):
975975
with pytest.raises(exceptions.CannotDetermineSpecification):
976976
Specification.detect({"foo": "bar"})
977977

978+
def test_detect_with_non_URI_schema(self):
979+
with pytest.raises(exceptions.CannotDetermineSpecification):
980+
Specification.detect({"$schema": 37})
981+
978982
def test_detect_with_no_discernible_information_and_default(self):
979983
specification = Specification.OPAQUE.detect({"foo": "bar"})
980984
assert specification is Specification.OPAQUE

0 commit comments

Comments
 (0)