Skip to content

Commit 813bf4e

Browse files
test for confusing not-identifiers in enums
A naive implementation would add all $id or $anchors to its list of known resources, without checking if they are actually in subschemas or the value of a keyword like enum, const or examples.
1 parent fc68499 commit 813bf4e

File tree

7 files changed

+408
-0
lines changed

7 files changed

+408
-0
lines changed

tests/draft2019-09/anchor.json

+57
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,62 @@
7777
"valid": false
7878
}
7979
]
80+
},
81+
{
82+
"description": "$anchor inside an enum is not a real identifier",
83+
"comment": "the implementation must not be confused by an $anchor buried in the enum",
84+
"schema": {
85+
"$defs": {
86+
"anchor_in_enum": {
87+
"enum": [
88+
{
89+
"$anchor": "my_anchor",
90+
"type": "null"
91+
}
92+
]
93+
},
94+
"real_identifier_in_schema": {
95+
"$anchor": "my_anchor",
96+
"type": "string"
97+
},
98+
"zzz_anchor_in_const": {
99+
"const": {
100+
"$anchor": "my_anchor",
101+
"type": "null"
102+
}
103+
}
104+
},
105+
"anyOf": [
106+
{ "$ref": "#/$defs/anchor_in_enum" },
107+
{ "$ref": "#my_anchor" }
108+
]
109+
},
110+
"tests": [
111+
{
112+
"description": "exact match to enum, and type matches",
113+
"data": {
114+
"$anchor": "my_anchor",
115+
"type": "null"
116+
},
117+
"valid": true
118+
},
119+
{
120+
"description": "in implementations that strip $anchor, this may match either $def",
121+
"data": {
122+
"type": "null"
123+
},
124+
"valid": false
125+
},
126+
{
127+
"description": "match $ref to $anchor",
128+
"data": "a string to match #/$defs/anchor_in_enum",
129+
"valid": true
130+
},
131+
{
132+
"description": "no match on enum or $ref to $anchor",
133+
"data": 1,
134+
"valid": false
135+
}
136+
]
80137
}
81138
]

tests/draft2019-09/id.json

+57
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,62 @@
202202
"valid": true
203203
}
204204
]
205+
},
206+
{
207+
"description": "$id inside an enum is not a real identifier",
208+
"comment": "the implementation must not be confused by an $id buried in the enum",
209+
"schema": {
210+
"$defs": {
211+
"id_in_enum": {
212+
"enum": [
213+
{
214+
"$id": "https://localhost:1234/my_identifier.json",
215+
"type": "null"
216+
}
217+
]
218+
},
219+
"real_id_in_schema": {
220+
"$id": "https://localhost:1234/my_identifier.json",
221+
"type": "string"
222+
},
223+
"zzz_id_in_const": {
224+
"const": {
225+
"$id": "https://localhost:1234/my_identifier.json",
226+
"type": "null"
227+
}
228+
}
229+
},
230+
"anyOf": [
231+
{ "$ref": "#/$defs/id_in_enum" },
232+
{ "$ref": "https://localhost:1234/my_identifier.json" }
233+
]
234+
},
235+
"tests": [
236+
{
237+
"description": "exact match to enum, and type matches",
238+
"data": {
239+
"$id": "https://localhost:1234/my_identifier.json",
240+
"type": "null"
241+
},
242+
"valid": true
243+
},
244+
{
245+
"description": "in implementations that strip $id, this may match either of the $defs",
246+
"data": {
247+
"type": "null"
248+
},
249+
"valid": false
250+
},
251+
{
252+
"description": "match $ref to $id",
253+
"data": "a string to match #/$defs/id_in_enum",
254+
"valid": true
255+
},
256+
{
257+
"description": "no match on enum or $ref to $id",
258+
"data": 1,
259+
"valid": false
260+
}
261+
]
205262
}
206263
]

tests/draft2020-12/anchor.json

+57
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,62 @@
7777
"valid": false
7878
}
7979
]
80+
},
81+
{
82+
"description": "$anchor inside an enum is not a real identifier",
83+
"comment": "the implementation must not be confused by an $anchor buried in the enum",
84+
"schema": {
85+
"$defs": {
86+
"anchor_in_enum": {
87+
"enum": [
88+
{
89+
"$anchor": "my_anchor",
90+
"type": "null"
91+
}
92+
]
93+
},
94+
"real_identifier_in_schema": {
95+
"$anchor": "my_anchor",
96+
"type": "string"
97+
},
98+
"zzz_anchor_in_const": {
99+
"const": {
100+
"$anchor": "my_anchor",
101+
"type": "null"
102+
}
103+
}
104+
},
105+
"anyOf": [
106+
{ "$ref": "#/$defs/anchor_in_enum" },
107+
{ "$ref": "#my_anchor" }
108+
]
109+
},
110+
"tests": [
111+
{
112+
"description": "exact match to enum, and type matches",
113+
"data": {
114+
"$anchor": "my_anchor",
115+
"type": "null"
116+
},
117+
"valid": true
118+
},
119+
{
120+
"description": "in implementations that strip $anchor, this may match either $def",
121+
"data": {
122+
"type": "null"
123+
},
124+
"valid": false
125+
},
126+
{
127+
"description": "match $ref to $anchor",
128+
"data": "a string to match #/$defs/anchor_in_enum",
129+
"valid": true
130+
},
131+
{
132+
"description": "no match on enum or $ref to $anchor",
133+
"data": 1,
134+
"valid": false
135+
}
136+
]
80137
}
81138
]

tests/draft2020-12/id.json

+57
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,62 @@
204204
"valid": true
205205
}
206206
]
207+
},
208+
{
209+
"description": "$id inside an enum is not a real identifier",
210+
"comment": "the implementation must not be confused by an $id buried in the enum",
211+
"schema": {
212+
"$defs": {
213+
"id_in_enum": {
214+
"enum": [
215+
{
216+
"$id": "https://localhost:1234/my_identifier.json",
217+
"type": "null"
218+
}
219+
]
220+
},
221+
"real_id_in_schema": {
222+
"$id": "https://localhost:1234/my_identifier.json",
223+
"type": "string"
224+
},
225+
"zzz_id_in_const": {
226+
"const": {
227+
"$id": "https://localhost:1234/my_identifier.json",
228+
"type": "null"
229+
}
230+
}
231+
},
232+
"anyOf": [
233+
{ "$ref": "#/$defs/id_in_enum" },
234+
{ "$ref": "https://localhost:1234/my_identifier.json" }
235+
]
236+
},
237+
"tests": [
238+
{
239+
"description": "exact match to enum, and type matches",
240+
"data": {
241+
"$id": "https://localhost:1234/my_identifier.json",
242+
"type": "null"
243+
},
244+
"valid": true
245+
},
246+
{
247+
"description": "in implementations that strip $id, this may match either of the $defs",
248+
"data": {
249+
"type": "null"
250+
},
251+
"valid": false
252+
},
253+
{
254+
"description": "match $ref to $id",
255+
"data": "a string to match #/$defs/id_in_enum",
256+
"valid": true
257+
},
258+
{
259+
"description": "no match on enum or $ref to $id",
260+
"data": 1,
261+
"valid": false
262+
}
263+
]
207264
}
208265
]

tests/draft4/id.json

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[
2+
{
3+
"description": "id inside an enum is not a real identifier",
4+
"comment": "the implementation must not be confused by an id buried in the enum",
5+
"schema": {
6+
"definitions": {
7+
"id_in_enum": {
8+
"enum": [
9+
{
10+
"id": "https://localhost:1234/my_identifier.json",
11+
"type": "null"
12+
}
13+
]
14+
},
15+
"real_id_in_schema": {
16+
"id": "https://localhost:1234/my_identifier.json",
17+
"type": "string"
18+
},
19+
"zzz_id_in_const": {
20+
"const": {
21+
"id": "https://localhost:1234/my_identifier.json",
22+
"type": "null"
23+
}
24+
}
25+
},
26+
"anyOf": [
27+
{ "$ref": "#/definitions/id_in_enum" },
28+
{ "$ref": "https://localhost:1234/my_identifier.json" }
29+
]
30+
},
31+
"tests": [
32+
{
33+
"description": "exact match to enum, and type matches",
34+
"data": {
35+
"id": "https://localhost:1234/my_identifier.json",
36+
"type": "null"
37+
},
38+
"valid": true
39+
},
40+
{
41+
"description": "in implementations that strip id, this may match either of the defs",
42+
"data": {
43+
"type": "null"
44+
},
45+
"valid": false
46+
},
47+
{
48+
"description": "match $ref to id",
49+
"data": "a string to match #/definitions/id_in_enum",
50+
"valid": true
51+
},
52+
{
53+
"description": "no match on enum or $ref to id",
54+
"data": 1,
55+
"valid": false
56+
}
57+
]
58+
}
59+
60+
]

tests/draft6/id.json

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[
2+
{
3+
"description": "id inside an enum is not a real identifier",
4+
"comment": "the implementation must not be confused by an id buried in the enum",
5+
"schema": {
6+
"definitions": {
7+
"id_in_enum": {
8+
"enum": [
9+
{
10+
"id": "https://localhost:1234/my_identifier.json",
11+
"type": "null"
12+
}
13+
]
14+
},
15+
"real_id_in_schema": {
16+
"id": "https://localhost:1234/my_identifier.json",
17+
"type": "string"
18+
},
19+
"zzz_id_in_const": {
20+
"const": {
21+
"id": "https://localhost:1234/my_identifier.json",
22+
"type": "null"
23+
}
24+
}
25+
},
26+
"anyOf": [
27+
{ "$ref": "#/definitions/id_in_enum" },
28+
{ "$ref": "https://localhost:1234/my_identifier.json" }
29+
]
30+
},
31+
"tests": [
32+
{
33+
"description": "exact match to enum, and type matches",
34+
"data": {
35+
"id": "https://localhost:1234/my_identifier.json",
36+
"type": "null"
37+
},
38+
"valid": true
39+
},
40+
{
41+
"description": "in implementations that strip id, this may match either of the defs",
42+
"data": {
43+
"type": "null"
44+
},
45+
"valid": false
46+
},
47+
{
48+
"description": "match $ref to id",
49+
"data": "a string to match #/definitions/id_in_enum",
50+
"valid": true
51+
},
52+
{
53+
"description": "no match on enum or $ref to id",
54+
"data": 1,
55+
"valid": false
56+
}
57+
]
58+
}
59+
60+
]

0 commit comments

Comments
 (0)