Skip to content

Commit 2c3bf67

Browse files
committed
Pass the draft 4 tests.
1 parent 9771693 commit 2c3bf67

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

referencing/jsonschema.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _dollar_id(contents: Schema) -> URI | None:
3535
return contents.get("$id")
3636

3737

38-
def _dollar_id_pre2019(contents: Schema) -> URI | None:
38+
def _legacy_dollar_id(contents: Schema) -> URI | None:
3939
if isinstance(contents, bool) or "$ref" in contents:
4040
return
4141
id = contents.get("$id")
@@ -44,11 +44,15 @@ def _dollar_id_pre2019(contents: Schema) -> URI | None:
4444

4545

4646
def _legacy_id(contents: ObjectSchema) -> URI | None:
47-
return contents.get("id")
47+
if "$ref" in contents:
48+
return
49+
id = contents.get("id")
50+
if id is not None and not id.startswith("#"):
51+
return id
4852

4953

50-
def _legacy_anchor_in_id(
51-
specification: Specification[ObjectSchema],
54+
def _legacy_anchor_in_dollar_id(
55+
specification: Specification[Schema],
5256
contents: Schema,
5357
) -> Iterable[Anchor[ObjectSchema]]:
5458
if isinstance(contents, bool):
@@ -64,6 +68,21 @@ def _legacy_anchor_in_id(
6468
]
6569

6670

71+
def _legacy_anchor_in_id(
72+
specification: Specification[ObjectSchema],
73+
contents: ObjectSchema,
74+
) -> Iterable[Anchor[ObjectSchema]]:
75+
id = contents.get("id", "")
76+
if not id.startswith("#"):
77+
return []
78+
return [
79+
Anchor(
80+
name=id[1:],
81+
resource=specification.create_resource(contents),
82+
),
83+
]
84+
85+
6786
def _subresources_of(
6887
in_value: Set[str] = frozenset(),
6988
in_subvalues: Set[str] = frozenset(),
@@ -106,29 +125,33 @@ def subresources_of(contents: Schema) -> Iterable[ObjectSchema]:
106125
)
107126
DRAFT7 = Specification(
108127
name="draft-07",
109-
id_of=_dollar_id_pre2019,
128+
id_of=_legacy_dollar_id,
110129
subresources_of=_subresources_of(
111130
in_value={"if", "then", "else", "not"},
112131
in_subarray={"allOf", "anyOf", "oneOf"},
113132
in_subvalues={"definitions", "properties"},
114133
),
115-
anchors_in=_legacy_anchor_in_id,
134+
anchors_in=_legacy_anchor_in_dollar_id,
116135
)
117136
DRAFT6 = Specification(
118137
name="draft-06",
119-
id_of=_dollar_id_pre2019,
138+
id_of=_legacy_dollar_id,
120139
subresources_of=_subresources_of(
121140
in_value={"not"},
122141
in_subarray={"allOf", "anyOf", "oneOf"},
123142
in_subvalues={"definitions", "properties"},
124143
),
125-
anchors_in=_legacy_anchor_in_id,
144+
anchors_in=_legacy_anchor_in_dollar_id,
126145
)
127146
DRAFT4 = Specification(
128147
name="draft-04",
129148
id_of=_legacy_id,
130-
subresources_of=lambda contents: [],
131-
anchors_in=lambda specification, contents: [],
149+
subresources_of=_subresources_of(
150+
in_value={"not"},
151+
in_subarray={"allOf", "anyOf", "oneOf"},
152+
in_subvalues={"definitions", "properties"},
153+
),
154+
anchors_in=_legacy_anchor_in_id,
132155
)
133156
DRAFT3 = Specification(
134157
name="draft-03",

0 commit comments

Comments
 (0)