microsoft / microsoft/vscode-json-languageservice

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

Open
#281 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
326
Forks
145
Avg merge
22h 10m
Merged PRs (30d)
9

Description

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));
  	};

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/services/jsonSchemaService.ts, at resolveSchemaContent and its findSchemaById helper. Reproduce the percent-encoded $ref example from the issue, then verify that navigation and schema validation resolve the definition with the decoded property name.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.