|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { AjvJsonSchemaValidator } from '../../src/validators/ajvProvider'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Regression tests for #2605: `getValidator()` must not recompile a schema |
| 7 | + * that has no `$id` on every call. Repeated `Client.listTools()` refreshes |
| 8 | + * call `getValidator` with structurally identical schemas; without caching, |
| 9 | + * each call compiles a fresh validator that the AJV engine retains forever, |
| 10 | + * so the heap grows without bound in long-running clients. |
| 11 | + */ |
| 12 | + |
| 13 | +function makeFakeEngine() { |
| 14 | + let compiles = 0; |
| 15 | + let getSchemaCalls = 0; |
| 16 | + const engine = { |
| 17 | + compile: () => { |
| 18 | + compiles += 1; |
| 19 | + return Object.assign(() => true, { errors: undefined }); |
| 20 | + }, |
| 21 | + getSchema: () => { |
| 22 | + getSchemaCalls += 1; |
| 23 | + // eslint-disable-next-line unicorn/no-useless-undefined -- AjvLike.getSchema must return undefined |
| 24 | + return undefined; |
| 25 | + }, |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 27 | + errorsText: (_errors?: any) => '' |
| 28 | + }; |
| 29 | + return { engine, compileCount: () => compiles, getSchemaCount: () => getSchemaCalls }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('AjvJsonSchemaValidator validator caching', () => { |
| 33 | + it('compiles a schema without $id only once across repeated getValidator calls', () => { |
| 34 | + const { engine, compileCount } = makeFakeEngine(); |
| 35 | + const provider = new AjvJsonSchemaValidator(engine); |
| 36 | + const schema = { type: 'object', properties: { a: { type: 'string' } } }; |
| 37 | + |
| 38 | + provider.getValidator(schema); |
| 39 | + provider.getValidator(schema); |
| 40 | + |
| 41 | + expect(compileCount()).toBe(1); |
| 42 | + }); |
| 43 | + |
| 44 | + it('hits the cache for structurally identical schemas with different object identity', () => { |
| 45 | + const { engine, compileCount } = makeFakeEngine(); |
| 46 | + const provider = new AjvJsonSchemaValidator(engine); |
| 47 | + |
| 48 | + provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } }); |
| 49 | + provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } }); |
| 50 | + |
| 51 | + expect(compileCount()).toBe(1); |
| 52 | + }); |
| 53 | + |
| 54 | + it('compiles distinct schemas independently', () => { |
| 55 | + const { engine, compileCount } = makeFakeEngine(); |
| 56 | + const provider = new AjvJsonSchemaValidator(engine); |
| 57 | + |
| 58 | + provider.getValidator({ type: 'string' }); |
| 59 | + provider.getValidator({ type: 'number' }); |
| 60 | + provider.getValidator({ type: 'object' }); |
| 61 | + |
| 62 | + expect(compileCount()).toBe(3); |
| 63 | + }); |
| 64 | + |
| 65 | + it('shares one compilation for schemas that differ only in key order', () => { |
| 66 | + const { engine, compileCount } = makeFakeEngine(); |
| 67 | + const provider = new AjvJsonSchemaValidator(engine); |
| 68 | + |
| 69 | + provider.getValidator({ type: 'object', properties: { a: { type: 'string' } } }); |
| 70 | + provider.getValidator({ properties: { a: { type: 'string' } }, type: 'object' }); |
| 71 | + |
| 72 | + expect(compileCount()).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it('is not poisoned when a caller mutates its schema object in place', () => { |
| 76 | + const { engine, compileCount } = makeFakeEngine(); |
| 77 | + const provider = new AjvJsonSchemaValidator(engine); |
| 78 | + |
| 79 | + const schema: Record<string, unknown> = { |
| 80 | + type: 'object', |
| 81 | + properties: { a: { type: 'string' } } |
| 82 | + }; |
| 83 | + provider.getValidator(schema); |
| 84 | + expect(compileCount()).toBe(1); |
| 85 | + |
| 86 | + // Mutate the same object in place — a fresh content key must trigger a |
| 87 | + // fresh compilation instead of reusing the stale validator for the |
| 88 | + // original content. |
| 89 | + schema.properties = { b: { type: 'number' } }; |
| 90 | + provider.getValidator(schema); |
| 91 | + |
| 92 | + expect(compileCount()).toBe(2); |
| 93 | + }); |
| 94 | + |
| 95 | + it('falls back to compiling without caching when a schema is not serializable', () => { |
| 96 | + const { engine, compileCount } = makeFakeEngine(); |
| 97 | + const provider = new AjvJsonSchemaValidator(engine); |
| 98 | + |
| 99 | + const cyclic: Record<string, unknown> = { type: 'object' }; |
| 100 | + cyclic.self = cyclic; |
| 101 | + |
| 102 | + expect(() => provider.getValidator(cyclic)).not.toThrow(); |
| 103 | + expect(compileCount()).toBe(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('keeps using the $id-based lookup for schemas with an $id', () => { |
| 107 | + const { engine, compileCount, getSchemaCount } = makeFakeEngine(); |
| 108 | + const provider = new AjvJsonSchemaValidator(engine); |
| 109 | + const schema = { $id: 'https://example.com/schema', type: 'string' }; |
| 110 | + |
| 111 | + provider.getValidator(schema); |
| 112 | + provider.getValidator(schema); |
| 113 | + |
| 114 | + expect(getSchemaCount()).toBe(2); |
| 115 | + // getSchema always misses in the fake engine, so each call compiles once — |
| 116 | + // the $id path is unchanged by this fix. |
| 117 | + expect(compileCount()).toBe(2); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns a working validator after caching', () => { |
| 121 | + const provider = new AjvJsonSchemaValidator(); |
| 122 | + const schema = { type: 'object', properties: { a: { type: 'string' } } }; |
| 123 | + |
| 124 | + const validate = provider.getValidator<{ a?: string }>(schema); |
| 125 | + expect(validate({ a: 'x' }).valid).toBe(true); |
| 126 | + expect(validate({ a: 1 }).valid).toBe(false); |
| 127 | + }); |
| 128 | +}); |
0 commit comments