Skip to content

Percent-encoded fragment identifiers in $ref are not resolved in JSON Schema files #281

@splo

Description

@splo

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

  1. 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" }
      }
    }
  2. Open the file in VS Code.
  3. 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));
  	};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions