Prefer local sibling schema resolution for relative $ref before remote $id lookup#1186
Conversation
|
It's working okay, but it seems like the schema is cached somehow, so that if the second schema is changed, the changes aren't loaded properly until you close and reopen VS Code. Caching makes sense if it's a remote schema, but not really if it's local. |
|
I think it'd be helpful to add a test with an absolute reference as well, eg.
{
"$id": "https://example.com/schemas/primary.json",
"$ref": "/schemas/secondary.json"
}
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/schemas/secondary.json",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name", "age"]
} |
|
Will this break bundled schemas? Or is that resolution done even earlier. A bundled version of the example would be: {
"$id": "https://example.com/schemas/primary.json",
"type": "object",
"properties": {
"mode": {
"$ref": "secondary.json"
}
},
"required": ["mode"],
"additionalProperties": false,
"$defs": {
"https://example.com/schemas/secondary.json": {
"$id": "https://example.com/schemas/secondary.json",
"type": "string",
"enum": ["dev", "prod"]
}
}
}In this case the embedded schema should be used. |
|
@datho7561 I revisited the 'local schema changes not reloaded' issue with the code and thought of a better approach. When resolving I added test cases for reloading local schema after changes & absolute reference as well. @DorianCzichotzki Bundled schemas should not be affected. They are resolved before any external fetch. In yamlSchemaService, we index embedded resources first then resolve $ref against that index. |
|
It seems like the example in #1186 (comment) doesn't work properly from my testing. |
|
Okay. I reviewed and updated how schema resolution should work when attempting to load schemas from a local file before falling back to remote URIs. It comes in two cases: 1. Relative reference Given: /some/path/schemas/secondary.json YAML file: Here, If that local file is missing, the next attempt is against the $id field ( 2. Absolute reference Given: /some/path/schemas/secondary.json Under standard JSON Schema rules, an absolute JSON Pointer-like path resolves against the base URI, not the local filesystem. Since From there, the relative path Second attempt falls back to https://example.com/schemas/secondary.json. |
…$id ref Signed-off-by: Morgan Chang <shin19991207@gmail.com>
… reloaded' issue Signed-off-by: Morgan Chang <shin19991207@gmail.com>
Signed-off-by: Morgan Chang <shin19991207@gmail.com>
d2dc2a0 to
0973ceb
Compare
datho7561
left a comment
There was a problem hiding this comment.
Seems to be working as described in the spec now. Thanks, Morgan!
What does this PR do?
Resolves relative
$refusing local sibling schema path before remote$idrefWhat issues does this PR fix or reference?
Fixes #1184
Is it tested? How?
Manually and with automated tests.
{ "$id": "https://example.com/schemas/primary.json", "type": "object", "properties": { "mode": { "$ref": "secondary.json" } }, "required": ["mode"], "additionalProperties": false }{ "$id": "https://example.com/schemas/secondary.json", "type": "string", "enum": ["dev", "prod"] }