Description
When a JSON Schema file contains a $ref with a percent-encoded fragment identifier (for example, "$ref": "#/$defs/foo%20bar"), the VS Code JSON language service fails to resolve the reference. As a result, navigation (e.g., clicking the reference) does not work, and validation may not resolve the schema section.
This is contrary to the JSON Schema specification (Section 8.2), which requires schemas to be identified by URI, which in turn requires the identifiers to be percent-decoded when resolving references (RFC 3986 - 2.4 When to Encode or Decode).
If the property name contains reserved characters but the reference is not percent-encoded (e.g., "$ref": "#/$defs/foo:bar"), VS Code does provide a clickable link and resolves the reference. I'm not sure if this is technically correct per the spec, but it works.
Details
Steps to Reproduce
- Create a JSON Schema file with a
$ref whose fragment contains percent-encoded characters:
{
"$defs": {
"foo:bar": { "type": "string" }
},
"type": "object",
"properties": {
"example": { "$ref": "#/$defs/foo%3Abar" }
}
}
- Open the file in VS Code.
- Try to navigate to the referenced definition (mouse over the reference).
Expected Behavior
The $ref should resolve to the correct definition of foo:bar and navigation should work.
Actual Behavior
The reference is not resolved, and navigation does not work.
Suggested Fix
Decode fragment identifiers using decodeURIComponent before matching against schema property names. I think this is handled by findSchemaById in jsonSchemaService.ts#resolveSchemaContent:
const findSchemaById = (schema: JSONSchema, handle: SchemaHandle, id: string) => {
if (!handle.anchors) {
handle.anchors = collectAnchors(schema);
}
- return handle.anchors.get(id);
+ return handle.anchors.get(decodeURIComponent(id));
};
Description
When a JSON Schema file contains a
$refwith a percent-encoded fragment identifier (for example,"$ref": "#/$defs/foo%20bar"), the VS Code JSON language service fails to resolve the reference. As a result, navigation (e.g., clicking the reference) does not work, and validation may not resolve the schema section.This is contrary to the JSON Schema specification (Section 8.2), which requires schemas to be identified by URI, which in turn requires the identifiers to be percent-decoded when resolving references (RFC 3986 - 2.4 When to Encode or Decode).
If the property name contains reserved characters but the reference is not percent-encoded (e.g.,
"$ref": "#/$defs/foo:bar"), VS Code does provide a clickable link and resolves the reference. I'm not sure if this is technically correct per the spec, but it works.Details
Steps to Reproduce
$refwhose fragment contains percent-encoded characters:{ "$defs": { "foo:bar": { "type": "string" } }, "type": "object", "properties": { "example": { "$ref": "#/$defs/foo%3Abar" } } }Expected Behavior
The
$refshould resolve to the correct definition offoo:barand navigation should work.Actual Behavior
The reference is not resolved, and navigation does not work.
Suggested Fix
Decode fragment identifiers using
decodeURIComponentbefore matching against schema property names. I think this is handled byfindSchemaByIdinjsonSchemaService.ts#resolveSchemaContent:const findSchemaById = (schema: JSONSchema, handle: SchemaHandle, id: string) => { if (!handle.anchors) { handle.anchors = collectAnchors(schema); } - return handle.anchors.get(id); + return handle.anchors.get(decodeURIComponent(id)); };