microsoft / microsoft/vscode-json-languageservice

Resolving a root-level $ref destroys the "definitions" that other $refs in the same document point to

Open
#355 0 comments 0 reactions 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

Resolving a $ref merges the referenced section into the target node with a plain per-key assignment. If the referenced section itself has a definitions key, the document's root definitions is overwritten, destroying pointer targets that other $refs in the same document still need.

Because the schema is mutated in place, the outcome depends on resolution order: a perfectly valid #/definitions/… reference becomes unresolvable.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/entry",
  "definitions": {
    "entry": {
      "type": "object",
      "properties": { "p": { "$ref": "#/definitions/shared" } },
      "definitions": { "unused": { "type": "null" } }
    },
    "shared": { "type": "string" }
  }
}

$ref '/definitions/shared' in 'file:///.../schema.json' can not be resolved.


Another issue, I'm not sure if it's relevant, is with the $ref path itself:

{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"$ref": "#/definitions/entry",
	"definitions": {
		"entry": {
			"type": "object",
			"properties": {
				"p": {
					"$ref": "#/definitions/entry/definitions/testDef"
				}
			},
			"definitions": {
				"testDef": {
					"type": "null"
				}
			}
		}
	}
}

#/definitions/entry/definitions/testDef won't resolve, when it's technically correct
#/definitions/testDef DOES, but it's actually incorrect, as # always point to the root, and "testDef" is NOT in /definitions

Reproduces in 5.7.2, 6.0.0-next.1, 6.0.0-next.2 and 6.0.0-next.3.
It's not a regression but a long-standing behaviour.
Also reproduces in VS Code (json-language-features 10.0.0).

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 by reproducing the two JSON Schema examples against the $ref resolution entry point in the TypeScript language service. Trace how resolving a root-level reference mutates definitions and how fragment paths are interpreted. Done means root-level and nested definitions remain resolvable in resolution-order-independent regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.