Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 911 lines 29.1 KB
Raw
1 import { describe, it, expect, vi } from 'vitest';
2 import {
3 deduplicateJsonSchemaRequiredArrays,
4 dereferenceJsonSchema,
5 ensureObjectTypeOnProperties,
6 jsonSchemaToZodSchema,
7 } from '../../src/utils/jsonSchema';
8 import {
9 JsonSchemaRefResolutionError,
10 JsonSchemaToZodError,
11 } from '../../src/errors/ValidationErrors';
12 import logger from '../../src/utils/logger';
13 import { zodSchemaToJsonSchema } from '../../src/utils/zodSchema';
14 import { ToolSchema } from '../../src/types/tool.types';
15 import {
16 assertCorpusInvariants,
17 loadObjectCases,
18 type CorpusCase,
19 } from '../fixtures/json-schema-conversion/corpus';
20
21 const containsRef = (value: unknown): boolean => {
22 if (value === null || typeof value !== 'object') return false;
23 if (Array.isArray(value)) return value.some(containsRef);
24 if ('$ref' in (value as Record<string, unknown>)) return true;
25 return Object.values(value as Record<string, unknown>).some(containsRef);
26 };
27
28 describe('dereferenceJsonSchema', () => {
29 it('inlines a single internal $ref under $defs', () => {
30 const out = dereferenceJsonSchema({
31 type: 'object',
32 properties: { user: { $ref: '#/$defs/User' } },
33 required: ['user'],
34 $defs: { User: { type: 'string' } },
35 });
36
37 expect(out).toEqual({
38 type: 'object',
39 properties: { user: { type: 'string' } },
40 required: ['user'],
41 });
42 expect(containsRef(out)).toBe(false);
43 });
44
45 it('resolves a chain of refs A -> B -> C', () => {
46 const out = dereferenceJsonSchema({
47 type: 'object',
48 properties: { v: { $ref: '#/$defs/A' } },
49 $defs: {
50 A: { $ref: '#/$defs/B' },
51 B: { $ref: '#/$defs/C' },
52 C: { type: 'integer' },
53 },
54 });
55
56 expect(containsRef(out)).toBe(false);
57 expect((out as { properties: { v: unknown } }).properties.v).toEqual({ type: 'integer' });
58 });
59
60 it('walks containers reflectively (items, oneOf, anyOf, allOf, not, additionalProperties, patternProperties, prefixItems)', () => {
61 const out = dereferenceJsonSchema({
62 type: 'object',
63 properties: {
64 a: { type: 'array', items: { $ref: '#/$defs/Leaf' } },
65 b: { oneOf: [{ $ref: '#/$defs/Leaf' }, { type: 'null' }] },
66 c: { anyOf: [{ $ref: '#/$defs/Leaf' }] },
67 d: { allOf: [{ $ref: '#/$defs/Leaf' }] },
68 e: { not: { $ref: '#/$defs/Leaf' } },
69 f: { type: 'object', additionalProperties: { $ref: '#/$defs/Leaf' } },
70 g: {
71 type: 'object',
72 patternProperties: { '^x_': { $ref: '#/$defs/Leaf' } },
73 },
74 h: { type: 'array', prefixItems: [{ $ref: '#/$defs/Leaf' }] },
75 },
76 $defs: { Leaf: { type: 'string' } },
77 });
78
79 expect(containsRef(out)).toBe(false);
80 });
81
82 it('resolves Draft 7 legacy `definitions`', () => {
83 const out = dereferenceJsonSchema({
84 type: 'object',
85 properties: { name: { $ref: '#/definitions/Name' } },
86 definitions: { Name: { type: 'string', minLength: 1 } },
87 });
88
89 expect(containsRef(out)).toBe(false);
90 expect((out as { properties: { name: unknown } }).properties.name).toEqual({
91 type: 'string',
92 minLength: 1,
93 });
94 });
95
96 it('mixes $defs and definitions transitively', () => {
97 const out = dereferenceJsonSchema({
98 type: 'object',
99 properties: { v: { $ref: '#/definitions/A' } },
100 definitions: { A: { $ref: '#/$defs/B' } },
101 $defs: { B: { type: 'boolean' } },
102 });
103
104 expect(containsRef(out)).toBe(false);
105 expect((out as { properties: { v: unknown } }).properties.v).toEqual({ type: 'boolean' });
106 });
107
108 it('shallow-merges sibling keywords next to $ref (Draft 2020-12 semantics; siblings win on collision)', () => {
109 const out = dereferenceJsonSchema({
110 type: 'object',
111 properties: {
112 v: {
113 $ref: '#/$defs/Foo',
114 description: 'caller override',
115 default: null,
116 },
117 },
118 $defs: {
119 Foo: { type: 'string', description: 'target description' },
120 },
121 });
122
123 const v = (out as { properties: { v: Record<string, unknown> } }).properties.v;
124 expect(v.type).toBe('string');
125 expect(v.description).toBe('caller override');
126 expect(v.default).toBeNull();
127 expect(containsRef(out)).toBe(false);
128 });
129
130 it('breaks recursive $ref cycles with a permissive object sentinel', () => {
131 const out = dereferenceJsonSchema({
132 $ref: '#/$defs/Tree',
133 $defs: {
134 Tree: {
135 type: 'object',
136 properties: {
137 children: { type: 'array', items: { $ref: '#/$defs/Tree' } },
138 },
139 },
140 },
141 });
142
143 expect((out as { type: string }).type).toBe('object');
144 const children = (
145 out as {
146 properties: { children: { items: { type: string; additionalProperties: boolean } } };
147 }
148 ).properties.children;
149 expect(children.items).toEqual({ type: 'object', additionalProperties: true });
150 expect(containsRef(out)).toBe(false);
151 });
152
153 it('strips $defs and definitions from the returned root schema', () => {
154 const out = dereferenceJsonSchema({
155 type: 'object',
156 properties: { x: { $ref: '#/$defs/Foo' } },
157 $defs: { Foo: { type: 'integer' } },
158 definitions: { Bar: { type: 'string' } },
159 }) as Record<string, unknown>;
160
161 expect(out.$defs).toBeUndefined();
162 expect(out.definitions).toBeUndefined();
163 });
164
165 it('throws JsonSchemaRefResolutionError when target is missing', () => {
166 expect(() =>
167 dereferenceJsonSchema({
168 type: 'object',
169 properties: { v: { $ref: '#/$defs/Missing' } },
170 $defs: {},
171 })
172 ).toThrow(JsonSchemaRefResolutionError);
173 });
174
175 it('throws JsonSchemaRefResolutionError when chain depth exceeds the cap', () => {
176 const $defs: Record<string, unknown> = {};
177 for (let i = 0; i < 200; i++) {
178 $defs[`A${i}`] = { $ref: i + 1 < 200 ? `#/$defs/A${i + 1}` : '#/$defs/A0' };
179 }
180 expect(() =>
181 dereferenceJsonSchema({
182 type: 'object',
183 properties: { v: { $ref: '#/$defs/A0' } },
184 $defs,
185 })
186 ).toThrow(JsonSchemaRefResolutionError);
187 });
188
189 it('throws JsonSchemaRefResolutionError on pathologically deep object nesting', () => {
190 // Build { properties: { x: { properties: { x: { ... } } } } } 1000 levels deep.
191 // Without a node-depth cap this would blow the V8 stack; with the cap, it throws
192 // a typed error before recursion grows unbounded.
193 type Nested = { type: 'object'; properties: { x: Nested | { type: 'string' } } };
194 let leaf: Nested | { type: 'string' } = { type: 'string' };
195 for (let i = 0; i < 1000; i++) {
196 leaf = { type: 'object', properties: { x: leaf } } as Nested;
197 }
198 expect(() => dereferenceJsonSchema(leaf)).toThrow(JsonSchemaRefResolutionError);
199 });
200
201 it('breaks JS object cycles that do not flow through a $ref', () => {
202 // Composio ingests schemas as live JS objects (custom tools, telemetry, …),
203 // so a non-$ref cycle is plausible. Without object-identity cycle detection
204 // this would infinite-loop; with it, the cycle returns the permissive sentinel.
205 const root: Record<string, unknown> = {
206 type: 'object',
207 properties: {},
208 };
209 (root.properties as Record<string, unknown>).self = root;
210
211 const out = dereferenceJsonSchema(root) as {
212 properties: { self: { type: string; additionalProperties: boolean } };
213 };
214 expect(out.properties.self).toEqual({ type: 'object', additionalProperties: true });
215 });
216
217 it('leaves external $ref pointers untouched and warns once for audit', () => {
218 const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});
219 try {
220 const out = dereferenceJsonSchema({
221 type: 'object',
222 properties: { v: { $ref: 'https://example.com/Foo' } },
223 });
224
225 expect((out as { properties: { v: { $ref: string } } }).properties.v.$ref).toBe(
226 'https://example.com/Foo'
227 );
228 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('https://example.com/Foo'));
229 } finally {
230 warnSpy.mockRestore();
231 }
232 });
233
234 it('filters prototype-pollution keys when cloning', () => {
235 const out = dereferenceJsonSchema({
236 type: 'object',
237 properties: {
238 v: { $ref: '#/$defs/User' },
239 },
240 $defs: {
241 User: { type: 'object', __proto__: { polluted: true } },
242 },
243 } as Record<string, unknown>);
244
245 const v = (out as { properties: { v: Record<string, unknown> } }).properties.v;
246 expect('__proto__' in v && (v as Record<string, unknown>).__proto__).not.toEqual({
247 polluted: true,
248 });
249 // The cloned node's prototype should remain Object.prototype.
250 expect(Object.getPrototypeOf(v)).toBe(Object.prototype);
251 });
252
253 it('does not mutate the input schema', () => {
254 const input = {
255 type: 'object',
256 properties: { v: { $ref: '#/$defs/Foo' } },
257 $defs: { Foo: { type: 'string' } },
258 };
259 const snapshot = structuredClone(input);
260 dereferenceJsonSchema(input);
261 expect(input).toEqual(snapshot);
262 });
263
264 it('decodes JSON Pointer escape sequences (~1 -> /, ~0 -> ~) in the correct order', () => {
265 const out = dereferenceJsonSchema({
266 type: 'object',
267 properties: {
268 a: { $ref: '#/$defs/with~1slash' },
269 b: { $ref: '#/$defs/with~0tilde' },
270 c: { $ref: '#/$defs/tilde-then-slash~01' },
271 },
272 $defs: {
273 'with/slash': { type: 'string' },
274 'with~tilde': { type: 'integer' },
275 'tilde-then-slash~1': { type: 'boolean' },
276 },
277 } as unknown as Record<string, unknown>);
278
279 const props = (out as { properties: Record<string, { type: string }> }).properties;
280 expect(props.a.type).toBe('string');
281 expect(props.b.type).toBe('integer');
282 expect(props.c.type).toBe('boolean');
283 });
284
285 it('resolves array-index pointers (#/$defs/foo/oneOf/0)', () => {
286 const out = dereferenceJsonSchema({
287 type: 'object',
288 properties: { v: { $ref: '#/$defs/foo/oneOf/0' } },
289 $defs: {
290 foo: { oneOf: [{ type: 'string' }, { type: 'number' }] },
291 },
292 } as unknown as Record<string, unknown>);
293
294 expect((out as { properties: { v: { type: string } } }).properties.v.type).toBe('string');
295 });
296
297 describe("with onUnresolved: 'sentinel' lenient mode", () => {
298 // Sentinel injected for an unresolvable $ref carries an LLM-visible hint
299 // describing the degradation. The hint is overwritten by a caller-provided
300 // `description` sibling when present (Draft 2020-12 sibling-keyword merge).
301 const PERMISSIVE = {
302 type: 'object',
303 additionalProperties: true,
304 description: expect.stringContaining('Schema shape unresolved at the source'),
305 };
306
307 it('replaces a dangling internal $ref (no $defs block at all) with the cycle-break sentinel', () => {
308 // Mirrors GMAIL_FETCH_EMAILS.outputParameters: a $ref into #/$defs/...
309 // without any $defs declared on the root.
310 const out = dereferenceJsonSchema(
311 {
312 type: 'object',
313 properties: {
314 data: { $ref: '#/$defs/FetchEmailsResponse' },
315 error: { type: 'string' },
316 successful: { type: 'boolean' },
317 },
318 required: ['data', 'successful'],
319 title: 'FetchEmailsResponseWrapper',
320 },
321 { onUnresolved: 'sentinel' }
322 ) as {
323 type: string;
324 properties: { data: unknown; error: unknown; successful: unknown };
325 required: string[];
326 };
327
328 expect(out.properties.data).toEqual(PERMISSIVE);
329 // Siblings of the replaced node are untouched.
330 expect(out.properties.error).toEqual({ type: 'string' });
331 expect(out.properties.successful).toEqual({ type: 'boolean' });
332 expect(out.required).toEqual(['data', 'successful']);
333 expect(containsRef(out)).toBe(false);
334 });
335
336 it('replaces a $ref that points into an empty $defs (missing key) with the sentinel', () => {
337 const out = dereferenceJsonSchema(
338 {
339 type: 'object',
340 properties: { v: { $ref: '#/$defs/Missing' } },
341 $defs: {},
342 },
343 { onUnresolved: 'sentinel' }
344 ) as { properties: { v: unknown } };
345
346 expect(out.properties.v).toEqual(PERMISSIVE);
347 expect(containsRef(out)).toBe(false);
348 });
349
350 it("invokes onReplace once per replacement with the original ref and reason 'missing-target'", () => {
351 const calls: Array<[string, string]> = [];
352 dereferenceJsonSchema(
353 {
354 type: 'object',
355 properties: {
356 a: { $ref: '#/$defs/Missing' },
357 b: { $ref: '#/$defs/Missing' },
358 c: { $ref: '#/$defs/AlsoMissing' },
359 },
360 },
361 {
362 onUnresolved: 'sentinel',
363 onReplace: (ref, reason) => calls.push([ref, reason]),
364 }
365 );
366
367 expect(calls).toEqual([
368 ['#/$defs/Missing', 'missing-target'],
369 ['#/$defs/Missing', 'missing-target'],
370 ['#/$defs/AlsoMissing', 'missing-target'],
371 ]);
372 });
373
374 it("invokes onReplace with reason 'malformed-pointer' for a non '/' internal pointer", () => {
375 const calls: Array<[string, string]> = [];
376 const out = dereferenceJsonSchema(
377 {
378 type: 'object',
379 properties: { v: { $ref: '#bar' } },
380 },
381 {
382 onUnresolved: 'sentinel',
383 onReplace: (ref, reason) => calls.push([ref, reason]),
384 }
385 ) as { properties: { v: unknown } };
386
387 expect(calls).toEqual([['#bar', 'malformed-pointer']]);
388 expect(out.properties.v).toEqual(PERMISSIVE);
389 });
390
391 it('still throws JsonSchemaRefResolutionError when chain depth exceeds the cap (safety cap is not lenient)', () => {
392 const $defs: Record<string, unknown> = {};
393 for (let i = 0; i < 200; i++) {
394 $defs[`A${i}`] = { $ref: i + 1 < 200 ? `#/$defs/A${i + 1}` : '#/$defs/A0' };
395 }
396 expect(() =>
397 dereferenceJsonSchema(
398 {
399 type: 'object',
400 properties: { v: { $ref: '#/$defs/A0' } },
401 $defs,
402 },
403 { onUnresolved: 'sentinel' }
404 )
405 ).toThrow(JsonSchemaRefResolutionError);
406 });
407
408 it('still throws JsonSchemaRefResolutionError when node depth exceeds the cap (safety cap is not lenient)', () => {
409 type Nested = { type: 'object'; properties: { x: Nested | { type: 'string' } } };
410 let leaf: Nested | { type: 'string' } = { type: 'string' };
411 for (let i = 0; i < 1000; i++) {
412 leaf = { type: 'object', properties: { x: leaf } } as Nested;
413 }
414 expect(() => dereferenceJsonSchema(leaf, { onUnresolved: 'sentinel' })).toThrow(
415 JsonSchemaRefResolutionError
416 );
417 });
418
419 it("explicit onUnresolved: 'throw' matches default behavior", () => {
420 expect(() =>
421 dereferenceJsonSchema(
422 {
423 type: 'object',
424 properties: { v: { $ref: '#/$defs/Missing' } },
425 $defs: {},
426 },
427 { onUnresolved: 'throw' }
428 )
429 ).toThrow(JsonSchemaRefResolutionError);
430 });
431
432 it('does not call onReplace in strict mode (regression guard)', () => {
433 const onReplace = vi.fn();
434 expect(() =>
435 dereferenceJsonSchema(
436 {
437 type: 'object',
438 properties: { v: { $ref: '#/$defs/Missing' } },
439 $defs: {},
440 },
441 { onUnresolved: 'throw', onReplace }
442 )
443 ).toThrow(JsonSchemaRefResolutionError);
444 expect(onReplace).not.toHaveBeenCalled();
445 });
446
447 it("injects a default LLM-visible 'description' on the sentinel when the source $ref node has none", () => {
448 const out = dereferenceJsonSchema(
449 {
450 type: 'object',
451 properties: { v: { $ref: '#/$defs/Missing' } },
452 },
453 { onUnresolved: 'sentinel' }
454 ) as { properties: { v: { description: string } } };
455
456 expect(out.properties.v.description).toMatch(/Schema shape unresolved at the source/);
457 expect(out.properties.v.description).toContain(
458 'https://github.com/ComposioHQ/composio/issues/3307'
459 );
460 });
461
462 it("preserves a caller-provided 'description' sibling instead of overwriting with the default", () => {
463 const out = dereferenceJsonSchema(
464 {
465 type: 'object',
466 properties: {
467 v: {
468 $ref: '#/$defs/Missing',
469 description: 'caller-supplied prose context',
470 },
471 },
472 },
473 { onUnresolved: 'sentinel' }
474 ) as { properties: { v: { description: string; type: string } } };
475
476 // Sibling-merge wins over the default — caller context survives.
477 expect(out.properties.v.description).toBe('caller-supplied prose context');
478 expect(out.properties.v.type).toBe('object');
479 });
480
481 it('preserves resolvable $defs while replacing only the dangling branch (mixed schema)', () => {
482 const out = dereferenceJsonSchema(
483 {
484 type: 'object',
485 properties: {
486 resolved: { $ref: '#/$defs/Real' },
487 dangling: { $ref: '#/$defs/Ghost' },
488 },
489 $defs: {
490 Real: { type: 'integer' },
491 },
492 },
493 { onUnresolved: 'sentinel' }
494 ) as { properties: { resolved: { type: string }; dangling: unknown } };
495
496 expect(out.properties.resolved).toEqual({ type: 'integer' });
497 expect(out.properties.dangling).toEqual(PERMISSIVE);
498 });
499 });
500 });
501
502 describe('deduplicateJsonSchemaRequiredArrays', () => {
503 it('deduplicates every required array without mutating the input schema', () => {
504 const schema = {
505 type: 'object',
506 required: ['owner', 'name', 'owner'],
507 properties: {
508 options: {
509 type: 'object',
510 required: ['enabled', 'enabled'],
511 properties: { enabled: { type: 'boolean' } },
512 },
513 },
514 };
515
516 const normalized = deduplicateJsonSchemaRequiredArrays(schema);
517
518 expect(normalized).toEqual({
519 type: 'object',
520 required: ['owner', 'name'],
521 properties: {
522 options: {
523 type: 'object',
524 required: ['enabled'],
525 properties: { enabled: { type: 'boolean' } },
526 },
527 },
528 });
529 expect(schema.required).toEqual(['owner', 'name', 'owner']);
530 expect(schema.properties.options.required).toEqual(['enabled', 'enabled']);
531 });
532
533 it('does not normalize literal instance values', () => {
534 const sharedValue = {
535 type: 'object',
536 required: ['preserve', 'preserve'],
537 properties: { preserve: { type: 'string' } },
538 };
539 const schema = {
540 type: 'object',
541 required: ['value', 'value'],
542 properties: {
543 schemaValue: sharedValue,
544 value: {
545 const: sharedValue,
546 default: { required: ['preserve', 'preserve'] },
547 enum: [{ required: ['preserve', 'preserve'] }],
548 examples: [{ required: ['preserve', 'preserve'] }],
549 },
550 },
551 };
552
553 const normalized = deduplicateJsonSchemaRequiredArrays(schema);
554
555 expect(normalized).toMatchObject({
556 required: ['value'],
557 properties: {
558 schemaValue: { required: ['preserve'] },
559 value: {
560 const: { required: ['preserve', 'preserve'] },
561 default: { required: ['preserve', 'preserve'] },
562 enum: [{ required: ['preserve', 'preserve'] }],
563 examples: [{ required: ['preserve', 'preserve'] }],
564 },
565 },
566 });
567 expect(normalized.properties.value.const).not.toBe(schema.properties.value.const);
568 });
569
570 it('fails predictably for schemas nested beyond the supported depth', () => {
571 const defaultValue: Record<string, unknown> = {};
572 let cursor = defaultValue;
573 for (let i = 0; i <= 512; i++) {
574 cursor.child = {};
575 cursor = cursor.child as Record<string, unknown>;
576 }
577
578 expect(() =>
579 deduplicateJsonSchemaRequiredArrays({
580 type: 'object',
581 properties: { value: { default: defaultValue } },
582 })
583 ).toThrow('JSON Schema exceeds maximum nesting depth of 512');
584 });
585
586 it('normalizes schemas at the shared ToolSchema API boundary', () => {
587 const tool = ToolSchema.parse({
588 slug: 'TEST_TOOL',
589 name: 'Test tool',
590 inputParameters: {
591 type: 'object',
592 required: ['name', 'name'],
593 properties: {
594 name: { type: 'string' },
595 options: {
596 type: 'object',
597 required: ['enabled', 'enabled'],
598 properties: { enabled: { type: 'boolean' } },
599 },
600 },
601 },
602 });
603
604 expect(tool.inputParameters?.required).toEqual(['name']);
605 expect(tool.inputParameters?.properties.options.required).toEqual(['enabled']);
606 });
607 });
608
609 describe('ToolSchema parameter-root preservation', () => {
610 for (const testCase of loadObjectCases()) {
611 const { ingress } = testCase;
612 if (!ingress) {
613 continue;
614 }
615
616 it(`preserves the ${testCase.id} parameter root exactly`, () => {
617 const result = ToolSchema.safeParse({
618 slug: 'TEST_TOOL',
619 name: 'Test tool',
620 inputParameters: testCase.schema,
621 });
622
623 expect(result.success).toBe(ingress.accepted);
624 if (result.success) {
625 expect(result.data.inputParameters).toEqual(ingress.preserved);
626 }
627 });
628 }
629
630 it('leaves an omitted root additionalProperties omitted', () => {
631 const tool = ToolSchema.parse({
632 slug: 'TEST_TOOL',
633 name: 'Test tool',
634 inputParameters: { type: 'object', properties: { name: { type: 'string' } } },
635 });
636
637 expect(tool.inputParameters).not.toHaveProperty('additionalProperties');
638 });
639 });
640
641 describe('jsonSchemaToZodSchema', () => {
642 it('names the property whose pattern cannot be compiled', () => {
643 let caught: unknown;
644 try {
645 jsonSchemaToZodSchema({
646 type: 'object',
647 properties: { name: { type: 'string', pattern: '(' } },
648 });
649 } catch (error) {
650 caught = error;
651 }
652
653 expect(caught).toBeInstanceOf(JsonSchemaToZodError);
654 expect((caught as JsonSchemaToZodError).message).toContain('at properties.name');
655 expect((caught as JsonSchemaToZodError).cause).toBeInstanceOf(Error);
656 });
657
658 it('describes a string as base64 to the model only when its contentEncoding is base64', () => {
659 const serialized = zodSchemaToJsonSchema(
660 jsonSchemaToZodSchema({
661 type: 'object',
662 properties: {
663 file: { type: 'string', format: 'binary' },
664 hex: { type: 'string', contentEncoding: 'base16' },
665 encoded: { type: 'string', contentEncoding: 'base64' },
666 },
667 required: ['file', 'hex', 'encoded'],
668 })
669 );
670
671 expect(serialized.properties).toEqual({
672 file: { type: 'string' },
673 hex: { type: 'string' },
674 encoded: { type: 'string', contentEncoding: 'base64' },
675 });
676 });
677 });
678
679 describe('shared corpus invariants', () => {
680 const baseCase = (id: string): CorpusCase => ({
681 id,
682 schema: { type: 'object' },
683 instances: [{ input: {}, accepted: true }],
684 });
685
686 it('accepts the checked-in corpus', () => {
687 expect(() => assertCorpusInvariants(loadObjectCases())).not.toThrow();
688 });
689
690 it('documents the patternProperties strictness divergence from JSON Schema', () => {
691 const testCase = loadObjectCases().find(
692 ({ id }) => id === 'named-properties-with-pattern-properties'
693 );
694
695 expect(testCase?.divergesFromJsonSchema).toContain('omitted `additionalProperties`');
696 });
697
698 it('rejects duplicate case ids', () => {
699 expect(() => assertCorpusInvariants([baseCase('dup'), baseCase('dup')])).toThrow(
700 'Duplicate corpus case id: dup'
701 );
702 });
703
704 it('rejects a per-language acceptance override without a declared divergence', () => {
705 const undeclared: CorpusCase = {
706 id: 'undeclared',
707 schema: { type: 'object' },
708 instances: [{ input: {}, accepted: true, python: { accepted: false } }],
709 };
710
711 expect(() => assertCorpusInvariants([undeclared])).toThrow(
712 'overrides python acceptance without a divergence reason'
713 );
714 });
715
716 it('allows a per-language acceptance override that declares its reason', () => {
717 const declared: CorpusCase = {
718 id: 'declared',
719 schema: { type: 'object' },
720 instances: [
721 {
722 input: {},
723 accepted: true,
724 python: { accepted: false },
725 divergence: { reason: 'documented Pydantic limitation' },
726 },
727 ],
728 };
729
730 expect(() => assertCorpusInvariants([declared])).not.toThrow();
731 });
732 });
733
734 describe('ensureObjectTypeOnProperties', () => {
735 it('adds type:object to nested nodes that have properties but no type', () => {
736 const schema = {
737 type: 'object',
738 properties: {
739 target: {
740 properties: {
741 name: { type: 'string' },
742 },
743 },
744 },
745 };
746
747 const result = ensureObjectTypeOnProperties(schema);
748 expect(result).toEqual({
749 type: 'object',
750 properties: {
751 target: {
752 type: 'object',
753 properties: {
754 name: { type: 'string' },
755 },
756 },
757 },
758 });
759 });
760
761 it('leaves nodes that already declare a type untouched', () => {
762 const schema = {
763 type: 'object',
764 properties: {
765 target: {
766 type: 'object',
767 properties: { name: { type: 'string' } },
768 },
769 },
770 };
771
772 const result = ensureObjectTypeOnProperties(schema);
773 expect(result).toEqual(schema);
774 });
775
776 it('handles deeply nested properties inside array items', () => {
777 const schema = {
778 type: 'object',
779 properties: {
780 list: {
781 type: 'array',
782 items: {
783 properties: { id: { type: 'string' } },
784 },
785 },
786 },
787 };
788
789 const result = ensureObjectTypeOnProperties(schema) as typeof schema;
790 expect(result.properties.list.items).toHaveProperty('type', 'object');
791 });
792
793 it('does not mutate the input schema', () => {
794 const schema = {
795 type: 'object',
796 properties: {
797 target: { properties: { name: { type: 'string' } } },
798 },
799 };
800
801 ensureObjectTypeOnProperties(schema);
802 expect(schema.properties.target).not.toHaveProperty('type');
803 });
804
805 it('does not inject type:object into instance-value keywords (const, default, enum, examples)', () => {
806 const schema = {
807 type: 'object',
808 properties: {
809 choice: {
810 type: 'object',
811 properties: { id: { type: 'string' } },
812 default: { properties: { should_not_gain_type: true } },
813 const: { properties: { marker: true } },
814 examples: [{ properties: { marker: true } }],
815 enum: [{ properties: { marker: true } }],
816 },
817 },
818 };
819
820 const result = ensureObjectTypeOnProperties(schema) as typeof schema;
821 const choice = result.properties.choice as Record<string, unknown>;
822 expect((choice.default as Record<string, unknown>).type).toBeUndefined();
823 expect((choice.const as Record<string, unknown>).type).toBeUndefined();
824 expect((choice.examples as Record<string, unknown>[])[0].type).toBeUndefined();
825 expect((choice.enum as Record<string, unknown>[])[0].type).toBeUndefined();
826 });
827
828 it('does not treat a properties map as a schema when a field is named properties', () => {
829 const schema = {
830 type: 'object',
831 properties: {
832 properties: {
833 properties: {
834 name: { type: 'string' },
835 },
836 },
837 },
838 };
839
840 const result = ensureObjectTypeOnProperties(schema) as {
841 properties: Record<string, unknown>;
842 };
843
844 expect(result.properties).toEqual({
845 properties: {
846 type: 'object',
847 properties: {
848 name: { type: 'string' },
849 },
850 },
851 });
852 expect(result.properties).not.toHaveProperty('type');
853 });
854
855 it('preserves reserved property names without changing the property-map prototype', () => {
856 const propertySchemas: Record<string, unknown> = {};
857 for (const key of ['constructor', 'prototype', '__proto__']) {
858 Object.defineProperty(propertySchemas, key, {
859 configurable: true,
860 enumerable: true,
861 value: { properties: { value: { type: 'string' } } },
862 writable: true,
863 });
864 }
865
866 const result = ensureObjectTypeOnProperties({
867 type: 'object',
868 properties: propertySchemas,
869 });
870
871 expect(Object.getPrototypeOf(result.properties)).toBe(Object.prototype);
872 for (const key of ['constructor', 'prototype', '__proto__']) {
873 expect(Object.hasOwn(result.properties, key)).toBe(true);
874 expect(result.properties[key]).toEqual({
875 type: 'object',
876 properties: { value: { type: 'string' } },
877 });
878 }
879 });
880
881 it('preserves reserved keys in instance defaults without treating their values as schemas', () => {
882 const defaultValue: Record<string, unknown> = {};
883 for (const key of ['constructor', 'prototype', '__proto__']) {
884 Object.defineProperty(defaultValue, key, {
885 configurable: true,
886 enumerable: true,
887 value: { properties: { marker: true } },
888 writable: true,
889 });
890 }
891
892 const result = ensureObjectTypeOnProperties({
893 type: 'object',
894 properties: {
895 choice: {
896 type: 'object',
897 properties: {},
898 default: defaultValue,
899 },
900 },
901 });
902 const normalizedDefault = result.properties.choice.default;
903
904 expect(Object.getPrototypeOf(normalizedDefault)).toBe(Object.prototype);
905 for (const key of ['constructor', 'prototype', '__proto__']) {
906 expect(Object.hasOwn(normalizedDefault, key)).toBe(true);
907 expect(normalizedDefault[key]).toEqual({ properties: { marker: true } });
908 expect(normalizedDefault[key]).not.toHaveProperty('type');
909 }
910 });
911 });