Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for unevaluated with dynamic reference #696

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/draft2019-09/unevaluatedItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,50 @@
}
]
},
{
"description": "unevaluatedItems with $recursiveRef",
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/extended-tree",

"$recursiveAnchor": true,

"$ref": "/tree",
"items": [
true,
true,
{ "type": "string" }
],

"$defs": {
"tree": {
"$id": "/tree",
"$recursiveAnchor": true,

"type": "array",
"items": [
{ "type": "number" },
{
"$recursiveRef": "#",
"unevaluatedItems": false
}
]
}
}
},
"tests": [
{
"description": "with no unevaluated items",
"data": [1, [2, [], "b"], "a"],
"valid": true
},
{
"description": "with unevaluated items",
"data": [1, [2, [], "b", "too many"], "a"],
"valid": false
}
]
},
{
"description": "unevaluatedItems can't see inside cousins",
"schema": {
Expand Down
57 changes: 57 additions & 0 deletions tests/draft2019-09/unevaluatedProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,63 @@
}
]
},
{
"description": "unevaluatedProperties with $recursiveRef",
"schema": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/extended-tree",

"$recursiveAnchor": true,

"$ref": "/tree",
"properties": {
"name": { "type": "string" }
},

"$defs": {
"tree": {
"$id": "/tree",
"$recursiveAnchor": true,

"type": "object",
"properties": {
"node": true,
"branches": {
"$recursiveRef": "#",
"unevaluatedProperties": false
}
},
"required": ["node"]
}
}
},
"tests": [
{
"description": "with no unevaluated properties",
"data": {
"name": "a",
"node": 1,
"branches": {
"name": "b",
"node": 2
}
},
"valid": true
},
{
"description": "with unevaluated properties",
"data": {
"name": "a",
"node": 1,
"branches": {
"foo": "b",
"node": 2
}
},
"valid": false
}
]
},
{
"description": "unevaluatedProperties can't see inside cousins",
"schema": {
Expand Down
48 changes: 48 additions & 0 deletions tests/draft2020-12/unevaluatedItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,54 @@
}
]
},
{
"description": "unevaluatedItems with $dynamicRef",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/derived",

"$ref": "/baseSchema",

"$defs": {
"derived": {
"$dynamicAnchor": "addons",
"prefixItems": [
true,
{ "type": "string" }
]
},
"baseSchema": {
"$id": "/baseSchema",

"type": "array",
"prefixItems": [
{ "type": "string" }
],
"$dynamicRef": "#addons",
"unevaluatedItems": false,

"$defs": {
"defaultAddons": {
"$comment": "Needed to satisfy the bookending requirement",
"$dynamicAnchor": "addons"
}
}
}
}
},
"tests": [
{
"description": "with no unevaluated items",
"data": ["foo", "bar"],
"valid": true
},
{
"description": "with unevaluated items",
"data": ["foo", "bar", "baz"],
"valid": false
}
]
},
{
"description": "unevaluatedItems can't see inside cousins",
"schema": {
Expand Down
54 changes: 54 additions & 0 deletions tests/draft2020-12/unevaluatedProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,60 @@
}
]
},
{
"description": "unevaluatedProperties with $dynamicRef",
"schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/derived",

"$ref": "/baseSchema",

"$defs": {
"derived": {
"$dynamicAnchor": "addons",
"properties": {
"bar": { "type": "string" }
}
},
"baseSchema": {
"$id": "/baseSchema",

"type": "object",
"properties": {
"foo": { "type": "string" }
},
"$dynamicRef": "#addons",
"unevaluatedProperties": false,
Copy link
Contributor

@skryukov skryukov Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of keywords might affect order of execution in implementations, so should we put unevaluatedProperties first, to cover that as well? (that might be a separate issue, since there are a lot of test cases here that have unevaluatedProperties last in the list)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have other tests that ensure key order is not a factor in evaluation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for $dynamicRef and $ref though. My implementation passed this test case but just by luck 😅

Moving "$ref": "#/$defs/bar" down in "unevaluatedItems with $ref", for example, fails.

It's also really important to test references bc they are not explicitly listed in the spec section: https://json-schema.org/draft/2020-12/json-schema-core#name-unevaluatedproperties

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for $dynamicRef and $ref though.

I don't think it's practical to check order of keywords for every combination of keywords. We have tests that ensure order of keywords is irrelevant. It's expected that this is applied across an implementation, not just for those tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also really important to test references bc they are not explicitly listed in the spec section: https://json-schema.org/draft/2020-12/json-schema-core#name-unevaluatedproperties

The spec does address references:

"This includes but is not limited to the in-place applicators defined in this document."

References are in-place applicators because they evaluate the instance against the schema they're referencing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that it's impractical to test ordering for every combination of keywords, I'm of the opinion that anything that exposed a real bug in an implementation belongs in the test suite. I'll move unevaluated* to be the first keyword and include a note explaining why.


"$defs": {
"defaultAddons": {
"$comment": "Needed to satisfy the bookending requirement",
"$dynamicAnchor": "addons"
}
}
}
}
},
"tests": [
{
"description": "with no unevaluated properties",
"data": {
"foo": "foo",
"bar": "bar"
},
"valid": true
},
{
"description": "with unevaluated properties",
"data": {
"foo": "foo",
"bar": "bar",
"baz": "baz"
},
"valid": false
}
]
},
{
"description": "unevaluatedProperties can't see inside cousins",
"schema": {
Expand Down