1
+/**
2
+ * Regenerates strict-cases.json (both language copies) from the case inputs
3
+ * below, using the built @composio/core. Run from the repository root after
4
+ * `pnpm --filter @composio/core build`:
5
+ *
6
+ * node ts/packages/core/test/fixtures/json-schema-conversion/generate-strict-cases.mjs
7
+ *
8
+ * The JSON is the pinned contract; this script only derives it so both SDK
9
+ * test suites assert against identical expectations. Format the TypeScript
10
+ * copy with prettier before copying (the pre-commit hook would otherwise
11
+ * make the copies differ).
12
+ */
13
+import fs from 'node:fs';
14
+import path from 'node:path';
15
+import { fileURLToPath } from 'node:url';
16
+
17
+const here = path.dirname(fileURLToPath(import.meta.url));
18
+const repoRoot = path.resolve(here, '../../../../../..');
19
+const { toStrictJsonSchema, omitNullToolArguments } = await import(
20
+ path.join(repoRoot, 'ts/packages/core/dist/index.mjs')
21
+);
22
+const obj = (properties, required, extra = {}) => ({
23
+ type: 'object',
24
+ properties,
25
+ ...(required ? { required } : {}),
26
+ ...extra,
27
+});
28
+const cases = [
29
+ [
30
+ 'flat-all-required-unchanged',
31
+ 'An already-strict schema is returned unchanged with no changes.',
32
+ obj({ query: { type: 'string' } }, ['query'], { additionalProperties: false }),
33
+ ],
34
+ [
35
+ 'nested-optional-property-widened',
36
+ 'Optional properties stay, become required and accept null at every depth.',
37
+ obj(
38
+ {
39
+ cfg: obj(
40
+ { url: { type: 'string' }, note: { type: 'string', description: 'optional note' } },
41
+ ['url']
42
+ ),
43
+ },
44
+ ['cfg']
45
+ ),
46
+ ],
47
+ [
48
+ 'no-required-array-widens-every-property',
49
+ 'An object without `required` requires and widens every property.',
50
+ obj({ a: { type: 'string' }, b: { type: 'number' } }),
51
+ ],
52
+ [
53
+ 'required-empty-array-widens-every-property',
54
+ 'An empty `required` array is the same as an absent one.',
55
+ obj({ a: { type: 'string' } }, []),
56
+ ],
57
+ [
58
+ 'nullable-type-array-kept',
59
+ 'A `type` array with null is accepted by the API and left alone.',
60
+ obj({ id: { type: ['string', 'null'], description: 'identifier' } }, ['id']),
61
+ ],
62
+ [
63
+ 'nullable-object-stays-nullable',
64
+ 'A nullable object keeps its type array; only its own properties are closed.',
65
+ obj(
66
+ { cfg: { type: ['object', 'null'], properties: { a: { type: 'string' } }, required: ['a'] } },
67
+ ['cfg']
68
+ ),
69
+ ],
70
+ [
71
+ 'nullable-array-kept',
72
+ 'A nullable array keeps its type array and items.',
73
+ obj({ xs: { type: ['array', 'null'], items: { type: 'string' } } }, ['xs']),
74
+ ],
75
+ [
76
+ 'optional-any-of-gets-null-branch',
77
+ 'An optional composition gains a null branch instead of a sibling type.',
78
+ obj({ value: { anyOf: [{ type: 'string' }, { type: 'number' }] } }),
79
+ ],
80
+ [
81
+ 'any-of-already-nullable-unchanged',
82
+ 'A composition that already accepts null is not widened twice.',
83
+ obj({ value: { anyOf: [{ type: 'string' }, { type: 'null' }] } }),
84
+ ],
85
+ [
86
+ 'optional-enum-only-wrapped',
87
+ 'An enum-only property is wrapped so the annotation stays outside the anyOf.',
88
+ obj({ choice: { enum: ['a', 'b'], description: 'pick one', title: 'Choice' } }),
89
+ ],
90
+ [
91
+ 'optional-const-wrapped',
92
+ 'A const-only property is wrapped in an anyOf with a null branch.',
93
+ obj({ kind: { const: 'fixed' } }),
94
+ ],
95
+ [
96
+ 'optional-empty-schema-unchanged',
97
+ 'An empty schema already accepts null and is left alone.',
98
+ obj({ anything: {} }),
99
+ ],
100
+ [
101
+ 'multi-type-array-gains-null',
102
+ 'A multi-type array gains null instead of being converted to anyOf.',
103
+ obj({ value: { type: ['string', 'number'] } }),
104
+ ],
105
+ [
106
+ 'one-of-converted-to-any-of',
107
+ 'oneOf is unsupported and becomes anyOf.',
108
+ obj({ either: { oneOf: [{ type: 'string' }, { type: 'number' }] } }, ['either']),
109
+ ],
110
+ [
111
+ 'array-items-object-normalized',
112
+ 'Objects inside array items are closed and widened.',
113
+ obj(
114
+ {
115
+ rows: {
116
+ type: 'array',
117
+ items: obj({ id: { type: 'string' }, tag: { type: 'string' } }, ['id']),
118
+ },
119
+ },
120
+ ['rows']
121
+ ),
122
+ ],
123
+ [
124
+ 'default-and-examples-stripped',
125
+ 'Annotation keywords the API rejects are stripped and reported.',
126
+ obj({ name: { type: 'string', examples: ['a'], default: 'x' } }, ['name']),
127
+ ],
128
+ [
129
+ 'enum-values-are-data-not-keywords',
130
+ 'Keys inside enum/const values are data and must not be treated as keywords.',
131
+ obj({ opts: { enum: [{ default: 1, properties: {} }, null] } }, ['opts']),
132
+ ],
133
+ [
134
+ 'property-names-are-not-keywords',
135
+ 'Properties named like keywords are ordinary properties.',
136
+ obj({
137
+ default: { type: 'string' },
138
+ $ref: { type: 'string' },
139
+ type: { type: 'string' },
140
+ properties: obj({ x: { type: 'string' } }),
141
+ }),
142
+ ],
143
+ [
144
+ 'prototype-key-property-name',
145
+ 'A property named __proto__ or constructor is handled as a plain name.',
146
+ JSON.parse(
147
+ '{"type":"object","properties":{"__proto__":{"type":"string"},"constructor":{"type":"number"}}}'
148
+ ),
149
+ ],
150
+ [
151
+ 'schema-valued-additional-properties-unsupported',
152
+ 'A map-style object cannot be expressed and is reported, not closed.',
153
+ obj({ headers: { type: 'object', additionalProperties: { type: 'string' } } }, ['headers']),
154
+ ],
155
+ [
156
+ 'additional-properties-true-unsupported',
157
+ 'An explicitly open object is reported.',
158
+ obj({ open: { type: 'object', additionalProperties: true } }, ['open']),
159
+ ],
160
+ [
161
+ 'additional-properties-empty-schema-unsupported',
162
+ 'An empty-schema additionalProperties accepts anything and is reported.',
163
+ obj({ open: { type: 'object', additionalProperties: {} } }, ['open']),
164
+ ],
165
+ [
166
+ 'free-form-object-unsupported',
167
+ 'A property-less object accepts arbitrary keys and is reported.',
168
+ obj({ meta: { type: 'object', description: 'any json' } }, ['meta']),
169
+ ],
170
+ [
171
+ 'pattern-properties-unsupported',
172
+ 'patternProperties cannot be expressed and is reported.',
173
+ obj({ tagged: { type: 'object', patternProperties: { '^x-': { type: 'string' } } } }, [
174
+ 'tagged',
175
+ ]),
176
+ ],
177
+ [
178
+ 'empty-closed-object-representable',
179
+ 'An object closed with no properties is representable.',
180
+ obj({ empty: { type: 'object', additionalProperties: false } }, ['empty']),
181
+ ],
182
+ [
183
+ 'all-of-unsupported',
184
+ 'allOf has no lossless rewrite and is reported.',
185
+ obj({ all: { allOf: [{ type: 'string' }] } }, ['all']),
186
+ ],
187
+ [
188
+ 'prefix-items-unsupported',
189
+ 'prefixItems has no lossless rewrite and is reported.',
190
+ obj({ pair: { type: 'array', prefixItems: [{ type: 'number' }] } }, ['pair']),
191
+ ],
192
+ [
193
+ 'defs-ref-kept-and-definition-normalized',
194
+ 'Local $refs are kept and the definition is normalized where it is declared.',
195
+ obj({ cfg: { $ref: '#/$defs/Config' } }, ['cfg'], {
196
+ $defs: { Config: obj({ url: { type: 'string' }, note: { type: 'string' } }, ['url']) },
197
+ }),
198
+ ],
199
+ [
200
+ 'optional-ref-widened-with-null-branch',
201
+ 'An optional $ref property is widened with an anyOf null branch.',
202
+ obj({ cfg: { $ref: '#/$defs/Config', description: 'optional' } }, undefined, {
203
+ $defs: { Config: obj({ url: { type: 'string' } }, ['url']) },
204
+ }),
205
+ ],
206
+ [
207
+ 'recursive-defs-kept',
208
+ 'Self-recursive definitions are representable.',
209
+ obj({ node: { $ref: '#/$defs/Node' } }, ['node'], {
210
+ $defs: {
211
+ Node: obj({ child: { $ref: '#/$defs/Node' }, label: { type: 'string' } }, ['label']),
212
+ },
213
+ }),
214
+ ],
215
+ [
216
+ 'mutually-recursive-defs-kept',
217
+ 'Mutually recursive definitions are representable.',
218
+ obj({ a: { $ref: '#/$defs/A' } }, ['a'], {
219
+ $defs: { A: obj({ b: { $ref: '#/$defs/B' } }), B: obj({ a: { $ref: '#/$defs/A' } }) },
220
+ }),
221
+ ],
222
+ [
223
+ 'legacy-definitions-ref-kept',
224
+ 'Legacy definitions are supported like $defs.',
225
+ obj({ cfg: { $ref: '#/definitions/Config' } }, ['cfg'], {
226
+ definitions: { Config: obj({ url: { type: 'string' } }) },
227
+ }),
228
+ ],
229
+ [
230
+ 'ref-into-properties-kept',
231
+ 'A local $ref outside $defs resolves and is kept.',
232
+ obj({ a: obj({ x: { type: 'string' } }, ['x']), b: { $ref: '#/properties/a' } }, ['a', 'b']),
233
+ ],
234
+ [
235
+ 'dangling-ref-unsupported',
236
+ 'A $ref without a target is reported at its own path.',
237
+ obj({ cfg: { $ref: '#/$defs/Nope' } }, ['cfg']),
238
+ ],
239
+ [
240
+ 'external-ref-unsupported',
241
+ 'A $ref outside the document is reported.',
242
+ obj({ cfg: { $ref: 'https://example.com/schema.json' } }, ['cfg']),
243
+ ],
244
+ [
245
+ 'duplicate-required-deduped',
246
+ 'Duplicate required entries are removed.',
247
+ obj({ a: { type: 'string' } }, ['a', 'a']),
248
+ ],
249
+ [
250
+ 'required-unknown-names-dropped',
251
+ 'Required names without a property are dropped from required.',
252
+ obj({ a: { type: 'string' } }, ['a', 'ghost']),
253
+ ],
254
+ [
255
+ 'required-not-an-array-ignored',
256
+ 'A malformed required is treated as absent.',
257
+ obj({ a: { type: 'string' } }, 'a'),
258
+ ],
259
+ [
260
+ 'description-kept-on-widened-node',
261
+ 'Annotations survive widening.',
262
+ obj({ a: { type: 'integer', description: 'count', title: 'Count', minimum: 0 } }),
263
+ ],
264
+ ['non-object-root-unsupported', 'The root must be an object.', { type: 'string' }],
265
+ [
266
+ 'nullable-root-unsupported',
267
+ 'The root must not be nullable.',
268
+ { type: ['object', 'null'], properties: { a: { type: 'string' } } },
269
+ ],
270
+ [
271
+ 'properties-without-type-become-object',
272
+ 'A node with properties but no type is treated as an object.',
273
+ obj({ cfg: { properties: { a: { type: 'string' } } } }, ['cfg']),
274
+ ],
275
+ [
276
+ 'root-without-type-become-object',
277
+ 'A root with properties but no type is treated as an object.',
278
+ { properties: { a: { type: 'string' } } },
279
+ ],
280
+ // Edge cases enumerated independently with a second model.
281
+ [
282
+ 'root-single-element-type-array-accepted',
283
+ 'A root typed ["object"] is an object root.',
284
+ { type: ['object'], properties: { a: { type: 'string' } }, required: ['a'] },
285
+ ],
286
+ [
287
+ 'three-member-type-array-gains-null',
288
+ 'A three-member type array gains null once.',
289
+ obj({ v: { type: ['string', 'number', 'boolean'] } }),
290
+ ],
291
+ [
292
+ 'optional-null-only-property-unchanged',
293
+ 'A property typed null already accepts null.',
294
+ obj({ nothing: { type: 'null' } }),
295
+ ],
296
+ [
297
+ 'optional-enum-with-null-not-rewrapped',
298
+ 'An enum that already lists null is not wrapped again.',
299
+ obj({ choice: { enum: ['a', null] } }),
300
+ ],
301
+ [
302
+ 'optional-const-null-not-rewrapped',
303
+ 'A const null already accepts null.',
304
+ obj({ nothing: { const: null } }),
305
+ ],
306
+ [
307
+ 'nested-any-of-branch-widened',
308
+ 'Optional properties inside anyOf branches are widened.',
309
+ obj({ v: { anyOf: [obj({ a: { type: 'string' } }), { type: 'string' }] } }, ['v']),
310
+ ],
311
+ [
312
+ 'array-of-nullable-objects-closed',
313
+ 'Nullable objects inside array items are closed and stay nullable.',
314
+ obj(
315
+ {
316
+ rows: {
317
+ type: 'array',
318
+ items: { type: ['object', 'null'], properties: { id: { type: 'string' } } },
319
+ },
320
+ },
321
+ ['rows']
322
+ ),
323
+ ],
324
+ [
325
+ 'tuple-form-items-unsupported',
326
+ 'Draft-4 tuple items have no strict-mode equivalent.',
327
+ obj({ pair: { type: 'array', items: [{ type: 'number' }, { type: 'string' }] } }, ['pair']),
328
+ ],
329
+ [
330
+ 'boolean-items-unsupported',
331
+ 'A boolean items subschema is reported.',
332
+ obj({ xs: { type: 'array', items: true } }, ['xs']),
333
+ ],
334
+ [
335
+ 'not-keyword-unsupported',
336
+ 'not has no strict-mode equivalent.',
337
+ obj({ v: { not: { type: 'string' } } }, ['v']),
338
+ ],
339
+ [
340
+ 'if-then-else-unsupported',
341
+ 'Conditional keywords have no strict-mode equivalent.',
342
+ obj(
343
+ { v: { type: 'string', if: { minLength: 1 }, then: { maxLength: 5 }, else: { const: '' } } },
344
+ ['v']
345
+ ),
346
+ ],
347
+ [
348
+ 'dependent-schemas-unsupported',
349
+ 'dependentSchemas has no strict-mode equivalent.',
350
+ obj({ a: { type: 'string' } }, ['a'], {
351
+ dependentSchemas: { a: obj({ b: { type: 'string' } }) },
352
+ }),
353
+ ],
354
+ [
355
+ 'property-names-keyword-unsupported',
356
+ 'propertyNames has no strict-mode equivalent.',
357
+ obj({ a: { type: 'string' } }, ['a'], { propertyNames: { pattern: '^a' } }),
358
+ ],
359
+ [
360
+ 'one-of-beside-any-of-unsupported',
361
+ 'oneOf next to anyOf cannot be merged and is reported.',
362
+ obj({ v: { anyOf: [{ type: 'string' }], oneOf: [{ type: 'number' }] } }, ['v']),
363
+ ],
364
+ [
365
+ 'boolean-property-subschema-unsupported',
366
+ 'A boolean property subschema is reported.',
367
+ obj({ anything: true, nothing: false }, ['anything', 'nothing']),
368
+ ],
369
+ [
370
+ 'properties-not-an-object-unsupported',
371
+ 'A malformed properties value is reported instead of emptied.',
372
+ obj('not-a-map', ['a']),
373
+ ],
374
+ [
375
+ 'ref-with-siblings-kept',
376
+ 'Sibling keywords next to a $ref are kept.',
377
+ obj({ cfg: { $ref: '#/$defs/Config', description: 'cfg', title: 'Config' } }, ['cfg'], {
378
+ $defs: { Config: obj({ a: { type: 'string' } }, ['a']) },
379
+ }),
380
+ ],
381
+ [
382
+ 'required-non-string-entries-ignored',
383
+ 'Non-string required entries are ignored.',
384
+ obj({ a: { type: 'string' }, b: { type: 'string' } }, ['a', 1, null]),
385
+ ],
386
+ [
387
+ 'dynamic-keys-reported-once-with-pattern-properties',
388
+ 'additionalProperties and patternProperties on one node report additionalProperties.',
389
+ obj(
390
+ {
391
+ m: {
392
+ type: 'object',
393
+ additionalProperties: { type: 'string' },
394
+ patternProperties: { '^x': { type: 'string' } },
395
+ },
396
+ },
397
+ ['m']
398
+ ),
399
+ ],
400
+ [
401
+ 'empty-properties-open-object-unsupported',
402
+ 'An object with empty properties and no closing is free-form.',
403
+ obj({ m: { type: 'object', properties: {} } }, ['m']),
404
+ ],
405
+ [
406
+ 'default-stripped-then-widened',
407
+ 'Stripping default and widening compose.',
408
+ obj({ a: { type: 'string', default: 'x' } }),
409
+ ],
410
+ [
411
+ 'nested-any-of-in-any-of-kept',
412
+ 'A composition nested inside a composition is normalized branch by branch.',
413
+ obj(
414
+ {
415
+ v: {
416
+ anyOf: [
417
+ { anyOf: [obj({ a: { type: 'string' } }), { type: 'number' }] },
418
+ { type: 'string' },
419
+ ],
420
+ },
421
+ },
422
+ ['v']
423
+ ),
424
+ ],
425
+ [
426
+ 'definitions-and-defs-both-normalized',
427
+ 'Legacy definitions and $defs coexist and both are normalized.',
428
+ obj({ a: { $ref: '#/$defs/A' }, b: { $ref: '#/definitions/B' } }, ['a', 'b'], {
429
+ $defs: { A: obj({ x: { type: 'string' } }) },
430
+ definitions: { B: obj({ y: { type: 'string' } }) },
431
+ }),
432
+ ],
433
+ [
434
+ 'deep-nesting-normalized-fully',
435
+ 'Ten levels of nested objects are normalized at every level.',
436
+ (() => {
437
+ let node = { type: 'object', properties: { leaf: { type: 'string' } } };
438
+ for (let i = 0; i < 10; i++)
439
+ node = { type: 'object', properties: { child: node }, required: ['child'] };
440
+ return node;
441
+ })(),
442
+ ],
443
+ [
444
+ 'chained-refs-resolved',
445
+ 'A $ref to a $ref resolves to the final definition.',
446
+ obj({ a: { $ref: '#/$defs/Alias' } }, ['a'], {
447
+ $defs: { Alias: { $ref: '#/$defs/Real' }, Real: obj({ x: { type: 'string' } }, ['x']) },
448
+ }),
449
+ ],
450
+];
451
+const argumentCases = {
452
+ 'nested-optional-property-widened': [
453
+ { input: { cfg: { url: 'u', note: null } }, output: { cfg: { url: 'u' } } },
454
+ ],
455
+ 'nullable-type-array-kept': [{ input: { id: null }, output: { id: null } }],
456
+ 'nullable-object-stays-nullable': [
457
+ { input: { cfg: null }, output: { cfg: null } },
458
+ { input: { cfg: { a: 'x' } }, output: { cfg: { a: 'x' } } },
459
+ ],
460
+ 'any-of-already-nullable-unchanged': [{ input: { value: null }, output: { value: null } }],
461
+ 'optional-enum-only-wrapped': [{ input: { choice: null }, output: {} }],
462
+ 'array-items-object-normalized': [
463
+ { input: { rows: [{ id: '1', tag: null }, null] }, output: { rows: [{ id: '1' }, null] } },
464
+ ],
465
+ 'enum-values-are-data-not-keywords': [{ input: { opts: null }, output: { opts: null } }],
466
+ 'property-names-are-not-keywords': [
467
+ {
468
+ input: { default: null, $ref: 'x', type: null, properties: { x: null } },
469
+ output: { $ref: 'x', properties: {} },
470
+ },
471
+ ],
472
+ 'defs-ref-kept-and-definition-normalized': [
473
+ { input: { cfg: { url: 'u', note: null } }, output: { cfg: { url: 'u' } } },
474
+ ],
475
+ 'optional-ref-widened-with-null-branch': [{ input: { cfg: null }, output: {} }],
476
+ 'recursive-defs-kept': [
477
+ {
478
+ input: { node: { label: 'a', child: { label: 'b', child: null } } },
479
+ output: { node: { label: 'a', child: { label: 'b' } } },
480
+ },
481
+ ],
482
+ 'no-required-array-widens-every-property': [
483
+ { input: { a: null, b: 1, unknown: null }, output: { b: 1, unknown: null } },
484
+ ],
485
+ 'optional-null-only-property-unchanged': [
486
+ { input: { nothing: null }, output: { nothing: null } },
487
+ ],
488
+ 'optional-enum-with-null-not-rewrapped': [{ input: { choice: null }, output: { choice: null } }],
489
+ 'optional-const-null-not-rewrapped': [{ input: { nothing: null }, output: { nothing: null } }],
490
+ 'array-of-nullable-objects-closed': [
491
+ { input: { rows: [null, { id: null }] }, output: { rows: [null, {}] } },
492
+ ],
493
+ 'nested-any-of-branch-widened': [
494
+ { input: { v: { a: null } }, output: { v: {} } },
495
+ { input: { v: null }, output: {} },
496
+ ],
497
+ 'nested-any-of-in-any-of-kept': [{ input: { v: { a: null } }, output: { v: {} } }],
498
+ 'chained-refs-resolved': [
499
+ { input: { a: { x: null } }, output: { a: {} } },
500
+ { input: { a: null }, output: {} },
501
+ ],
502
+ 'nullable-array-kept': [
503
+ { input: { xs: null }, output: { xs: null } },
504
+ { input: { xs: [null] }, output: { xs: [null] } },
505
+ ],
506
+ 'nested-optional-property-widened': [
507
+ { input: { cfg: { url: 'u', note: null } }, output: { cfg: { url: 'u' } } },
508
+ { input: { cfg: null }, output: {} },
509
+ { input: { cfg: { url: 'u', extra: null } }, output: { cfg: { url: 'u', extra: null } } },
510
+ ],
511
+};
512
+const out = { cases: [] };
513
+for (const [id, description, schema] of cases) {
514
+ const snapshot = JSON.stringify(schema);
515
+ const r = toStrictJsonSchema(schema);
516
+ if (JSON.stringify(schema) !== snapshot) throw new Error(`mutated: ${id}`);
517
+ const entry = { id, description, schema };
518
+ if (r.unsupported.length)
519
+ entry.strict = { unsupported: r.unsupported.map(u => ({ path: u.path, keyword: u.keyword })) };
520
+ else {
521
+ const again = toStrictJsonSchema(r.schema);
522
+ if (JSON.stringify(again.schema) !== JSON.stringify(r.schema) || again.changes.length)
523
+ throw new Error(`not idempotent: ${id}`);
524
+ entry.strict = { schema: r.schema };
525
+ }
526
+ if (r.changes.length) entry.changes = r.changes.map(c => ({ path: c.path, reason: c.reason }));
527
+ if (argumentCases[id]) {
528
+ entry.arguments = argumentCases[id].map(({ input, output }) => {
529
+ const actual = omitNullToolArguments(input, r.source);
530
+ if (JSON.stringify(actual) !== JSON.stringify(output))
531
+ throw new Error(
532
+ `arguments mismatch ${id}: ${JSON.stringify(actual)} vs ${JSON.stringify(output)}`
533
+ );
534
+ return { input, output };
535
+ });
536
+ }
537
+ out.cases.push(entry);
538
+}
539
+const json = JSON.stringify(out, null, 2) + '\n';
540
+fs.writeFileSync(path.join(here, 'strict-cases.json'), json);
541
+fs.writeFileSync(
542
+ path.join(repoRoot, 'python/tests/fixtures/json-schema-conversion/strict-cases.json'),
543
+ json
544
+);
545
+for (const c of out.cases)
546
+ console.log(
547
+ c.id.padEnd(50),
548
+ c.strict.unsupported
549
+ ? 'UNSUPPORTED ' + JSON.stringify(c.strict.unsupported)
550
+ : 'ok' + (c.changes ? ' changes=' + c.changes.map(x => x.reason.split('-')[0]).join(',') : '')
551
+ );