Resolved schema caching not working when $id is set to retrieval URI

Open Beginner friendly
#356 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start in src/services/jsonSchemaService.ts, at resolveSchemaContent and registerEmbeddedSchemas, then add the supplied regression test for a schema whose $id matches its retrieval URI. Run the JSON schema service tests and confirm the schema resolves once while the second getResolvedSchema call uses the cached result.

Written by the indexing model from the issue text.

Description

In JSONSchemaService.resolveSchemaContent, there is a function called registerEmbeddedSchemas, which is supposed to find any sub-schemas with an $id property, and register them to the service. However, when traversing the schema, it also visits the root. If $id is set there to the same URI as the one that is currently being resolved, it will be overridden with setSchemaContent, which also clears its resolvedSchema promise.

https://github.com/microsoft/vscode-json-languageservice/blob/f5985875dcded9a2f76c4c2a89e5ad3fbb39766f/src/services/jsonSchemaService.ts#L1224-L1263

This means there is effectively no caching for schemas that set their $id to their retrieval URI, which is probably most of them.

Here's a test case to reproduce:

test('Schema with $id matching its own URI should not cause double resolution', async function () {
	const schemaUri = 'https://exmaple.com/schema';

	const schema: JSONSchema = {
		$schema: 'https://json-schema.org/draft/2019-09/schema',
		$id: schemaUri,
		type: 'object'
	};

	const service = new SchemaService.JSONSchemaService(newMockRequestService(), workspaceContext);

	service.setSchemaContributions({ schemas: { [schemaUri]: schema } });

	let resolveSchemaContentCount = 0;

	// Intercept resolveSchemaContent to count resolution calls
	const originalResolveSchemaContent = (service as any).resolveSchemaContent.bind(service);
	(service as any).resolveSchemaContent = function (
		schemaToResolve: SchemaService.UnresolvedSchema,
		handle: SchemaService.ISchemaHandle
	) {
		resolveSchemaContentCount++;
		return originalResolveSchemaContent(schemaToResolve, handle);
	};

	// First call - resolves the schema
	const result1 = await service.getResolvedSchema(schemaUri);
	assert.ok(result1, 'First resolution should succeed');
	assert.strictEqual(result1?.schema.type, 'object');

	// Second call - should return cached result
	const result2 = await service.getResolvedSchema(schemaUri);
	assert.ok(result2, 'Second call should return cached result');
	assert.strictEqual(result2?.schema.type, 'object');

	assert.strictEqual(
		resolveSchemaContentCount,
		1,
		`Expected single resolution, but got ${resolveSchemaContentCount}. Schema with $id matching its URI should not invalidate its own resolution cache.`
	);
});
Dominant language
TypeScript
Stars
326
Forks
145
Avg merge
22h 10m
Merged PRs (30d)
9

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.

More from microsoft/vscode-json-languageservice

All issues in microsoft/vscode-json-languageservice

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.