Skip to content

Commit 6318c8f

Browse files
committed
Move the empty fragment stripping to Resource.id
Fine, given we are anyways doing this for Registry.contents, this feels slightly better, as otherwise someone wouldn't anyhow have a way to create some specification where empty fragments are meaningful but still call contents(). So, let's try this.
1 parent bc24395 commit 6318c8f

File tree

4 files changed

+12
-22
lines changed

4 files changed

+12
-22
lines changed

referencing/_core.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ def id(self) -> URI | None:
142142
"""
143143
Retrieve this resource's (specification-specific) identifier.
144144
"""
145-
return self._specification.id_of(self.contents)
145+
id = self._specification.id_of(self.contents)
146+
if id is None:
147+
return
148+
return id.rstrip("#")
146149

147150
def subresources(self) -> Iterable[Resource[D]]:
148151
"""

referencing/jsonschema.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ class UnknownDialect(Exception):
3636
def _dollar_id(contents: Schema) -> URI | None:
3737
if isinstance(contents, bool):
3838
return
39-
id = contents.get("$id")
40-
if id is not None:
41-
return id.rstrip("#")
39+
return contents.get("$id")
4240

4341

4442
def _legacy_dollar_id(contents: Schema) -> URI | None:
4543
if isinstance(contents, bool) or "$ref" in contents:
4644
return
4745
id = contents.get("$id")
4846
if id is not None and not id.startswith("#"):
49-
return id.rstrip("#")
47+
return id
5048

5149

5250
def _legacy_id(contents: ObjectSchema) -> URI | None:
5351
if "$ref" in contents:
5452
return
5553
id = contents.get("id")
5654
if id is not None and not id.startswith("#"):
57-
return id.rstrip("#")
55+
return id
5856

5957

6058
def _anchor(

referencing/tests/test_core.py

+5
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,11 @@ def test_id_delegates_to_specification(self):
610610
)
611611
assert resource.id() == "urn:fixedID"
612612

613+
def test_id_strips_empty_fragment(self):
614+
uri = "http://example.com/"
615+
root = ID_AND_CHILDREN.create_resource({"ID": uri + "#"})
616+
assert root.id() == uri
617+
613618
def test_subresources_delegates_to_specification(self):
614619
resource = ID_AND_CHILDREN.create_resource({"children": [{}, 12]})
615620
assert list(resource.subresources()) == [

referencing/tests/test_jsonschema.py

-16
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,6 @@ def test_id_of_mapping(id, specification):
6565
assert specification.id_of({id: uri}) == uri
6666

6767

68-
@pytest.mark.parametrize(
69-
"id, specification",
70-
[
71-
("$id", referencing.jsonschema.DRAFT202012),
72-
("$id", referencing.jsonschema.DRAFT201909),
73-
("$id", referencing.jsonschema.DRAFT7),
74-
("$id", referencing.jsonschema.DRAFT6),
75-
("id", referencing.jsonschema.DRAFT4),
76-
("id", referencing.jsonschema.DRAFT3),
77-
],
78-
)
79-
def test_id_of_empty_fragment(id, specification):
80-
uri = "http://example.com/some-schema"
81-
assert specification.id_of({id: uri + "#"}) == uri
82-
83-
8468
@pytest.mark.parametrize(
8569
"specification",
8670
[

0 commit comments

Comments
 (0)