|
5 | 5 | inlineRefs,
|
6 | 6 | applyCompatibilityTransformations,
|
7 | 7 | removeFormats,
|
| 8 | + findUsedDefs, |
8 | 9 | } from '../src/compat';
|
9 | 10 | import { Tool } from '@modelcontextprotocol/sdk/types.js';
|
10 | 11 | import { JSONSchema } from '../src/compat';
|
@@ -316,6 +317,103 @@ describe('removeAnyOf', () => {
|
316 | 317 | });
|
317 | 318 | });
|
318 | 319 |
|
| 320 | +describe('findUsedDefs', () => { |
| 321 | + it('should handle circular references without stack overflow', () => { |
| 322 | + const defs = { |
| 323 | + person: { |
| 324 | + type: 'object', |
| 325 | + properties: { |
| 326 | + name: { type: 'string' }, |
| 327 | + friend: { $ref: '#/$defs/person' }, // Circular reference |
| 328 | + }, |
| 329 | + }, |
| 330 | + }; |
| 331 | + |
| 332 | + const schema = { |
| 333 | + type: 'object', |
| 334 | + properties: { |
| 335 | + user: { $ref: '#/$defs/person' }, |
| 336 | + }, |
| 337 | + }; |
| 338 | + |
| 339 | + // This should not throw a stack overflow error |
| 340 | + expect(() => { |
| 341 | + const result = findUsedDefs(schema, defs); |
| 342 | + expect(result).toHaveProperty('person'); |
| 343 | + }).not.toThrow(); |
| 344 | + }); |
| 345 | + |
| 346 | + it('should handle indirect circular references without stack overflow', () => { |
| 347 | + const defs = { |
| 348 | + node: { |
| 349 | + type: 'object', |
| 350 | + properties: { |
| 351 | + value: { type: 'string' }, |
| 352 | + child: { $ref: '#/$defs/childNode' }, |
| 353 | + }, |
| 354 | + }, |
| 355 | + childNode: { |
| 356 | + type: 'object', |
| 357 | + properties: { |
| 358 | + value: { type: 'string' }, |
| 359 | + parent: { $ref: '#/$defs/node' }, // Indirect circular reference |
| 360 | + }, |
| 361 | + }, |
| 362 | + }; |
| 363 | + |
| 364 | + const schema = { |
| 365 | + type: 'object', |
| 366 | + properties: { |
| 367 | + root: { $ref: '#/$defs/node' }, |
| 368 | + }, |
| 369 | + }; |
| 370 | + |
| 371 | + // This should not throw a stack overflow error |
| 372 | + expect(() => { |
| 373 | + const result = findUsedDefs(schema, defs); |
| 374 | + expect(result).toHaveProperty('node'); |
| 375 | + expect(result).toHaveProperty('childNode'); |
| 376 | + }).not.toThrow(); |
| 377 | + }); |
| 378 | + |
| 379 | + it('should find all used definitions in non-circular schemas', () => { |
| 380 | + const defs = { |
| 381 | + user: { |
| 382 | + type: 'object', |
| 383 | + properties: { |
| 384 | + name: { type: 'string' }, |
| 385 | + address: { $ref: '#/$defs/address' }, |
| 386 | + }, |
| 387 | + }, |
| 388 | + address: { |
| 389 | + type: 'object', |
| 390 | + properties: { |
| 391 | + street: { type: 'string' }, |
| 392 | + city: { type: 'string' }, |
| 393 | + }, |
| 394 | + }, |
| 395 | + unused: { |
| 396 | + type: 'object', |
| 397 | + properties: { |
| 398 | + data: { type: 'string' }, |
| 399 | + }, |
| 400 | + }, |
| 401 | + }; |
| 402 | + |
| 403 | + const schema = { |
| 404 | + type: 'object', |
| 405 | + properties: { |
| 406 | + person: { $ref: '#/$defs/user' }, |
| 407 | + }, |
| 408 | + }; |
| 409 | + |
| 410 | + const result = findUsedDefs(schema, defs); |
| 411 | + expect(result).toHaveProperty('user'); |
| 412 | + expect(result).toHaveProperty('address'); |
| 413 | + expect(result).not.toHaveProperty('unused'); |
| 414 | + }); |
| 415 | +}); |
| 416 | + |
319 | 417 | describe('inlineRefs', () => {
|
320 | 418 | it('should return the original schema if it does not contain $refs', () => {
|
321 | 419 | const schema: JSONSchema = {
|
|
0 commit comments