Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { omitNullToolArguments, toStrictJsonSchema } from '../../src/utils/jsonSchema'; |
| 3 | import type { StrictSchemaChange } from '../../src/utils/jsonSchema'; |
| 4 | |
| 5 | const propertyOf = (schema: Record<string, unknown>, name: string): Record<string, unknown> => |
| 6 | (schema.properties as Record<string, Record<string, unknown>>)[name]; |
| 7 | |
| 8 | const reasons = (changes: StrictSchemaChange[]): string[] => changes.map(change => change.reason); |
| 9 | |
| 10 | /** Structural invariants OpenAI enforces on every node of a strict schema. */ |
| 11 | function assertStrictShape(node: unknown, path = ''): void { |
| 12 | if (Array.isArray(node)) { |
| 13 | node.forEach((item, index) => assertStrictShape(item, `${path}[${index}]`)); |
| 14 | return; |
| 15 | } |
| 16 | if (!node || typeof node !== 'object') return; |
| 17 | const record = node as Record<string, unknown>; |
| 18 | if (record.anyOf !== undefined) { |
| 19 | expect(record.type, `${path}: type beside anyOf`).toBeUndefined(); |
| 20 | } |
| 21 | expect(record.default, `${path}: default`).toBeUndefined(); |
| 22 | expect(record.examples, `${path}: examples`).toBeUndefined(); |
| 23 | expect(record.oneOf, `${path}: oneOf`).toBeUndefined(); |
| 24 | expect(record.patternProperties, `${path}: patternProperties`).toBeUndefined(); |
| 25 | const isObject = |
| 26 | record.type === 'object' || (Array.isArray(record.type) && record.type.includes('object')); |
| 27 | if (isObject || record.properties !== undefined) { |
| 28 | const properties = (record.properties ?? {}) as Record<string, unknown>; |
| 29 | expect(record.required, `${path}: required`).toEqual(Object.keys(properties)); |
| 30 | expect(record.additionalProperties, `${path}: additionalProperties`).toBe(false); |
| 31 | } |
| 32 | for (const [key, child] of Object.entries(record)) { |
| 33 | if (key === 'enum' || key === 'const') continue; |
| 34 | assertStrictShape(child, path ? `${path}.${key}` : key); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | describe('toStrictJsonSchema', () => { |
| 39 | it('keeps an already-flat all-required object valid', () => { |
| 40 | const input = { |
| 41 | type: 'object', |
| 42 | properties: { query: { type: 'string' } }, |
| 43 | required: ['query'], |
| 44 | additionalProperties: false, |
| 45 | }; |
| 46 | |
| 47 | const { schema, changes } = toStrictJsonSchema(input); |
| 48 | |
| 49 | expect(schema).toEqual(input); |
| 50 | expect(changes).toEqual([]); |
| 51 | }); |
| 52 | |
| 53 | it('keeps optional properties, requires them and widens them to accept null', () => { |
| 54 | const { schema, changes } = toStrictJsonSchema({ |
| 55 | type: 'object', |
| 56 | properties: { |
| 57 | cfg: { |
| 58 | type: 'object', |
| 59 | properties: { |
| 60 | url: { type: 'string' }, |
| 61 | note: { type: 'string', description: 'optional note' }, |
| 62 | }, |
| 63 | required: ['url'], |
| 64 | }, |
| 65 | }, |
| 66 | required: ['cfg'], |
| 67 | }); |
| 68 | |
| 69 | expect(schema).toEqual({ |
| 70 | type: 'object', |
| 71 | properties: { |
| 72 | cfg: { |
| 73 | type: 'object', |
| 74 | properties: { |
| 75 | url: { type: 'string' }, |
| 76 | note: { type: ['string', 'null'], description: 'optional note' }, |
| 77 | }, |
| 78 | required: ['url', 'note'], |
| 79 | additionalProperties: false, |
| 80 | }, |
| 81 | }, |
| 82 | required: ['cfg'], |
| 83 | additionalProperties: false, |
| 84 | }); |
| 85 | expect(changes).toEqual([ |
| 86 | { |
| 87 | path: 'properties.cfg.properties.note', |
| 88 | reason: 'optional-property-nullable', |
| 89 | detail: 'property "note" is now required and accepts null', |
| 90 | }, |
| 91 | ]); |
| 92 | }); |
| 93 | |
| 94 | it('requires and widens every property when the object has no required array', () => { |
| 95 | const { schema } = toStrictJsonSchema({ |
| 96 | type: 'object', |
| 97 | properties: { |
| 98 | a: { type: 'string' }, |
| 99 | b: { type: 'number' }, |
| 100 | }, |
| 101 | }); |
| 102 | |
| 103 | expect(schema).toEqual({ |
| 104 | type: 'object', |
| 105 | properties: { |
| 106 | a: { type: ['string', 'null'] }, |
| 107 | b: { type: ['number', 'null'] }, |
| 108 | }, |
| 109 | required: ['a', 'b'], |
| 110 | additionalProperties: false, |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | it('keeps nullable type arrays as-is and closes nullable objects', () => { |
| 115 | const { schema, changes } = toStrictJsonSchema({ |
| 116 | type: 'object', |
| 117 | properties: { |
| 118 | id: { type: ['string', 'null'], description: 'identifier' }, |
| 119 | cfg: { |
| 120 | type: ['object', 'null'], |
| 121 | properties: { a: { type: 'string' } }, |
| 122 | required: ['a'], |
| 123 | }, |
| 124 | xs: { type: ['array', 'null'], items: { type: 'string' } }, |
| 125 | }, |
| 126 | required: ['id', 'cfg', 'xs'], |
| 127 | }); |
| 128 | |
| 129 | expect(schema).toEqual({ |
| 130 | type: 'object', |
| 131 | properties: { |
| 132 | id: { type: ['string', 'null'], description: 'identifier' }, |
| 133 | cfg: { |
| 134 | type: ['object', 'null'], |
| 135 | properties: { a: { type: 'string' } }, |
| 136 | required: ['a'], |
| 137 | additionalProperties: false, |
| 138 | }, |
| 139 | xs: { type: ['array', 'null'], items: { type: 'string' } }, |
| 140 | }, |
| 141 | required: ['id', 'cfg', 'xs'], |
| 142 | additionalProperties: false, |
| 143 | }); |
| 144 | expect(changes).toEqual([]); |
| 145 | assertStrictShape(schema); |
| 146 | }); |
| 147 | |
| 148 | it('widens optional composition and enum-only properties without placing type beside anyOf', () => { |
| 149 | const { schema } = toStrictJsonSchema({ |
| 150 | type: 'object', |
| 151 | properties: { |
| 152 | value: { anyOf: [{ type: 'string' }, { type: 'number' }] }, |
| 153 | already: { anyOf: [{ type: 'string' }, { type: 'null' }] }, |
| 154 | choice: { enum: ['a', 'b'], description: 'pick one' }, |
| 155 | multi: { type: ['string', 'number'] }, |
| 156 | }, |
| 157 | }); |
| 158 | |
| 159 | expect(propertyOf(schema, 'value')).toEqual({ |
| 160 | anyOf: [{ type: 'string' }, { type: 'number' }, { type: 'null' }], |
| 161 | }); |
| 162 | expect(propertyOf(schema, 'already')).toEqual({ |
| 163 | anyOf: [{ type: 'string' }, { type: 'null' }], |
| 164 | }); |
| 165 | expect(propertyOf(schema, 'choice')).toEqual({ |
| 166 | description: 'pick one', |
| 167 | anyOf: [{ enum: ['a', 'b'] }, { type: 'null' }], |
| 168 | }); |
| 169 | expect(propertyOf(schema, 'multi')).toEqual({ type: ['string', 'number', 'null'] }); |
| 170 | assertStrictShape(schema); |
| 171 | }); |
| 172 | |
| 173 | it('normalizes composition branches and array items recursively', () => { |
| 174 | const { schema, changes } = toStrictJsonSchema({ |
| 175 | type: 'object', |
| 176 | properties: { |
| 177 | payload: { |
| 178 | anyOf: [ |
| 179 | { |
| 180 | type: 'object', |
| 181 | properties: { inner: { type: 'string' }, extra: { type: 'string' } }, |
| 182 | required: ['inner'], |
| 183 | }, |
| 184 | { type: 'null' }, |
| 185 | ], |
| 186 | }, |
| 187 | rows: { |
| 188 | type: 'array', |
| 189 | items: { |
| 190 | type: 'object', |
| 191 | properties: { id: { type: 'string' }, tag: { type: 'string' } }, |
| 192 | required: ['id'], |
| 193 | }, |
| 194 | }, |
| 195 | either: { oneOf: [{ type: 'string' }, { type: 'number' }] }, |
| 196 | }, |
| 197 | required: ['payload', 'rows', 'either'], |
| 198 | }); |
| 199 | |
| 200 | expect((propertyOf(schema, 'payload').anyOf as unknown[])[0]).toEqual({ |
| 201 | type: 'object', |
| 202 | properties: { inner: { type: 'string' }, extra: { type: ['string', 'null'] } }, |
| 203 | required: ['inner', 'extra'], |
| 204 | additionalProperties: false, |
| 205 | }); |
| 206 | expect(propertyOf(schema, 'rows').items).toEqual({ |
| 207 | type: 'object', |
| 208 | properties: { id: { type: 'string' }, tag: { type: ['string', 'null'] } }, |
| 209 | required: ['id', 'tag'], |
| 210 | additionalProperties: false, |
| 211 | }); |
| 212 | expect(propertyOf(schema, 'either')).toEqual({ |
| 213 | anyOf: [{ type: 'string' }, { type: 'number' }], |
| 214 | }); |
| 215 | expect(reasons(changes)).toContain('one-of-converted'); |
| 216 | assertStrictShape(schema); |
| 217 | }); |
| 218 | |
| 219 | it('reports dynamic-key and free-form objects as unsupported instead of closing them', () => { |
| 220 | const { schema, unsupported } = toStrictJsonSchema({ |
| 221 | type: 'object', |
| 222 | properties: { |
| 223 | headers: { type: 'object', additionalProperties: { type: 'string' } }, |
| 224 | meta: { type: 'object', description: 'any json' }, |
| 225 | tagged: { type: 'object', patternProperties: { '^x-': { type: 'string' } } }, |
| 226 | open: { type: 'object', additionalProperties: true }, |
| 227 | }, |
| 228 | required: ['headers', 'meta', 'tagged', 'open'], |
| 229 | }); |
| 230 | |
| 231 | expect(unsupported).toEqual([ |
| 232 | { |
| 233 | path: 'properties.headers', |
| 234 | keyword: 'additionalProperties', |
| 235 | detail: 'object accepts arbitrary keys', |
| 236 | }, |
| 237 | { |
| 238 | path: 'properties.meta', |
| 239 | keyword: 'properties', |
| 240 | detail: 'free-form object accepts arbitrary keys', |
| 241 | }, |
| 242 | { |
| 243 | path: 'properties.tagged', |
| 244 | keyword: 'patternProperties', |
| 245 | detail: 'object accepts pattern-matched keys', |
| 246 | }, |
| 247 | { |
| 248 | path: 'properties.open', |
| 249 | keyword: 'additionalProperties', |
| 250 | detail: 'object accepts arbitrary keys', |
| 251 | }, |
| 252 | ]); |
| 253 | // The schema-valued additionalProperties is preserved, not overwritten. |
| 254 | expect(propertyOf(schema, 'headers').additionalProperties).toEqual({ type: 'string' }); |
| 255 | expect(propertyOf(schema, 'tagged').patternProperties).toBeDefined(); |
| 256 | }); |
| 257 | |
| 258 | it('strips annotation keywords and reports keywords it cannot rewrite', () => { |
| 259 | const { schema, changes, unsupported } = toStrictJsonSchema({ |
| 260 | type: 'object', |
| 261 | properties: { |
| 262 | name: { type: 'string', examples: ['a'], default: 'x' }, |
| 263 | pair: { type: 'array', prefixItems: [{ type: 'number' }] }, |
| 264 | all: { allOf: [{ type: 'string' }] }, |
| 265 | }, |
| 266 | required: ['name', 'pair', 'all'], |
| 267 | }); |
| 268 | |
| 269 | expect(propertyOf(schema, 'name')).toEqual({ type: 'string' }); |
| 270 | expect(changes.filter(change => change.reason === 'unsupported-keyword-stripped')).toHaveLength( |
| 271 | 2 |
| 272 | ); |
| 273 | expect(unsupported.map(entry => [entry.path, entry.keyword])).toEqual([ |
| 274 | ['properties.pair', 'prefixItems'], |
| 275 | ['properties.all', 'allOf'], |
| 276 | ]); |
| 277 | }); |
| 278 | |
| 279 | it('keeps $defs and $ref, normalizes the definitions and reports dangling refs', () => { |
| 280 | const { schema, changes, unsupported } = toStrictJsonSchema({ |
| 281 | type: 'object', |
| 282 | properties: { |
| 283 | cfg: { $ref: '#/$defs/Config' }, |
| 284 | optionalCfg: { $ref: '#/$defs/Config', description: 'optional' }, |
| 285 | missing: { $ref: '#/$defs/Nope' }, |
| 286 | external: { $ref: 'https://example.com/schema.json' }, |
| 287 | }, |
| 288 | required: ['cfg', 'cfg', 'missing', 'external'], |
| 289 | $defs: { |
| 290 | Config: { |
| 291 | type: 'object', |
| 292 | properties: { url: { type: 'string' }, note: { type: 'string' } }, |
| 293 | required: ['url'], |
| 294 | }, |
| 295 | }, |
| 296 | }); |
| 297 | |
| 298 | expect(propertyOf(schema, 'cfg')).toEqual({ $ref: '#/$defs/Config' }); |
| 299 | expect(propertyOf(schema, 'optionalCfg')).toEqual({ |
| 300 | description: 'optional', |
| 301 | anyOf: [{ $ref: '#/$defs/Config' }, { type: 'null' }], |
| 302 | }); |
| 303 | expect(schema.$defs).toEqual({ |
| 304 | Config: { |
| 305 | type: 'object', |
| 306 | properties: { url: { type: 'string' }, note: { type: ['string', 'null'] } }, |
| 307 | required: ['url', 'note'], |
| 308 | additionalProperties: false, |
| 309 | }, |
| 310 | }); |
| 311 | expect(schema.required).toEqual(['cfg', 'optionalCfg', 'missing', 'external']); |
| 312 | expect(unsupported).toEqual([ |
| 313 | { path: 'properties.missing', keyword: '$ref', detail: 'unresolved $ref "#/$defs/Nope"' }, |
| 314 | { |
| 315 | path: 'properties.external', |
| 316 | keyword: '$ref', |
| 317 | detail: 'unresolved $ref "https://example.com/schema.json"', |
| 318 | }, |
| 319 | ]); |
| 320 | expect(changes).toContainEqual( |
| 321 | expect.objectContaining({ |
| 322 | path: '$defs.Config.properties.note', |
| 323 | reason: 'optional-property-nullable', |
| 324 | }) |
| 325 | ); |
| 326 | }); |
| 327 | |
| 328 | it('caps the change log at 50 entries without losing properties', () => { |
| 329 | const properties: Record<string, unknown> = {}; |
| 330 | for (let i = 0; i < 60; i++) properties[`p${i}`] = { type: 'string' }; |
| 331 | |
| 332 | const { schema, changes, totalChanges } = toStrictJsonSchema({ |
| 333 | type: 'object', |
| 334 | properties, |
| 335 | required: ['p0'], |
| 336 | }); |
| 337 | |
| 338 | expect(Object.keys(schema.properties as object)).toHaveLength(60); |
| 339 | expect(changes).toHaveLength(50); |
| 340 | expect(totalChanges).toBe(59); |
| 341 | }); |
| 342 | |
| 343 | it('does not mutate the input schema', () => { |
| 344 | const input = { |
| 345 | type: 'object', |
| 346 | properties: { |
| 347 | cfg: { |
| 348 | type: 'object', |
| 349 | properties: { opt: { type: 'string', default: 1 } }, |
| 350 | }, |
| 351 | }, |
| 352 | required: ['cfg'], |
| 353 | }; |
| 354 | const snapshot = JSON.parse(JSON.stringify(input)); |
| 355 | |
| 356 | toStrictJsonSchema(input); |
| 357 | |
| 358 | expect(input).toEqual(snapshot); |
| 359 | }); |
| 360 | |
| 361 | it('is idempotent', () => { |
| 362 | const once = toStrictJsonSchema({ |
| 363 | type: 'object', |
| 364 | properties: { |
| 365 | cfg: { $ref: '#/$defs/Config' }, |
| 366 | id: { type: ['string', 'null'] }, |
| 367 | }, |
| 368 | $defs: { |
| 369 | Config: { |
| 370 | type: 'object', |
| 371 | properties: { url: { type: 'string' }, note: { type: 'string' } }, |
| 372 | required: ['url'], |
| 373 | }, |
| 374 | }, |
| 375 | }).schema; |
| 376 | |
| 377 | const twice = toStrictJsonSchema(once); |
| 378 | expect(twice.schema).toEqual(once); |
| 379 | expect(twice.changes).toEqual([]); |
| 380 | expect(twice.unsupported).toEqual([]); |
| 381 | }); |
| 382 | |
| 383 | it('reports non-object roots as unsupported and keeps recursive $defs', () => { |
| 384 | expect(toStrictJsonSchema({ type: 'string' }).unsupported).toEqual([ |
| 385 | { path: '', keyword: 'type', detail: 'root must be a non-nullable object' }, |
| 386 | ]); |
| 387 | expect( |
| 388 | toStrictJsonSchema({ type: ['object', 'null'], properties: { a: { type: 'string' } } }) |
| 389 | .unsupported |
| 390 | ).toContainEqual(expect.objectContaining({ path: '', keyword: 'type' })); |
| 391 | |
| 392 | const cyclic = toStrictJsonSchema({ |
| 393 | type: 'object', |
| 394 | properties: { node: { $ref: '#/$defs/Node' } }, |
| 395 | required: ['node'], |
| 396 | $defs: { |
| 397 | Node: { |
| 398 | type: 'object', |
| 399 | properties: { child: { $ref: '#/$defs/Node' }, label: { type: 'string' } }, |
| 400 | required: ['label'], |
| 401 | }, |
| 402 | }, |
| 403 | }); |
| 404 | // Recursion through $defs is representable in strict mode. |
| 405 | expect(cyclic.unsupported).toEqual([]); |
| 406 | expect((cyclic.schema.$defs as Record<string, unknown>).Node).toEqual({ |
| 407 | type: 'object', |
| 408 | properties: { |
| 409 | child: { anyOf: [{ $ref: '#/$defs/Node' }, { type: 'null' }] }, |
| 410 | label: { type: 'string' }, |
| 411 | }, |
| 412 | required: ['child', 'label'], |
| 413 | additionalProperties: false, |
| 414 | }); |
| 415 | }); |
| 416 | |
| 417 | it('throws past the maximum nesting depth', () => { |
| 418 | let deep: Record<string, unknown> = { type: 'string' }; |
| 419 | for (let i = 0; i < 600; i++) { |
| 420 | deep = { type: 'object', properties: { nest: deep }, required: ['nest'] }; |
| 421 | } |
| 422 | |
| 423 | expect(() => toStrictJsonSchema(deep)).toThrow(/depth/); |
| 424 | }); |
| 425 | }); |
| 426 | |
| 427 | describe('omitNullToolArguments', () => { |
| 428 | const schema = { |
| 429 | type: 'object', |
| 430 | properties: { |
| 431 | cfg: { |
| 432 | type: 'object', |
| 433 | properties: { url: { type: 'string' }, note: { type: 'string' } }, |
| 434 | required: ['url'], |
| 435 | }, |
| 436 | label: { type: 'string' }, |
| 437 | clearable: { type: ['string', 'null'] }, |
| 438 | choice: { anyOf: [{ enum: ['a'] }, { type: 'null' }] }, |
| 439 | rows: { |
| 440 | type: 'array', |
| 441 | items: { type: 'object', properties: { id: { type: 'string' }, tag: { type: 'string' } } }, |
| 442 | }, |
| 443 | refd: { $ref: '#/$defs/Str' }, |
| 444 | refdNullable: { $ref: '#/$defs/NullableStr' }, |
| 445 | }, |
| 446 | $defs: { Str: { type: 'string' }, NullableStr: { type: ['string', 'null'] } }, |
| 447 | }; |
| 448 | |
| 449 | it('drops nulls the schema does not accept and keeps the ones it does', () => { |
| 450 | const input = { |
| 451 | cfg: { url: 'https://example.com', note: null }, |
| 452 | label: null, |
| 453 | clearable: null, |
| 454 | choice: null, |
| 455 | unknown: null, |
| 456 | rows: [{ id: '1', tag: null }, null], |
| 457 | refd: null, |
| 458 | refdNullable: null, |
| 459 | }; |
| 460 | const snapshot = JSON.parse(JSON.stringify(input)); |
| 461 | |
| 462 | expect(omitNullToolArguments(input, schema)).toEqual({ |
| 463 | cfg: { url: 'https://example.com' }, |
| 464 | clearable: null, |
| 465 | choice: null, |
| 466 | unknown: null, |
| 467 | rows: [{ id: '1' }, null], |
| 468 | refdNullable: null, |
| 469 | }); |
| 470 | expect(input).toEqual(snapshot); |
| 471 | }); |
| 472 | }); |
| 473 | |
| 474 | describe('null acceptance', () => { |
| 475 | const NULL_BRANCH = { type: 'null' }; |
| 476 | const DIRECTION = { enum: ['asc', null] }; |
| 477 | const wrapped = (schema: unknown) => ({ anyOf: [schema, NULL_BRANCH] }); |
| 478 | const toolSchema = (property: unknown, definitions: unknown = { Direction: DIRECTION }) => ({ |
| 479 | type: 'object', |
| 480 | properties: { value: property }, |
| 481 | $defs: definitions, |
| 482 | }); |
| 483 | |
| 484 | it.each<[Record<string, unknown>, Record<string, unknown>]>([ |
| 485 | [{ type: 'string', enum: ['asc', 'desc'] }, wrapped({ type: 'string', enum: ['asc', 'desc'] })], |
| 486 | [ |
| 487 | { type: ['string', 'null'], enum: ['asc', 'desc'] }, |
| 488 | wrapped({ type: ['string', 'null'], enum: ['asc', 'desc'] }), |
| 489 | ], |
| 490 | [{ type: 'string', const: 'asc' }, wrapped({ type: 'string', const: 'asc' })], |
| 491 | [ |
| 492 | { type: 'integer', enum: [1, 2], description: 'page size' }, |
| 493 | { description: 'page size', ...wrapped({ type: 'integer', enum: [1, 2] }) }, |
| 494 | ], |
| 495 | [ |
| 496 | { type: 'string', anyOf: [{ minLength: 1 }] }, |
| 497 | wrapped({ type: 'string', anyOf: [{ minLength: 1 }] }), |
| 498 | ], |
| 499 | [ |
| 500 | { enum: ['asc'], anyOf: [{ type: 'string' }, NULL_BRANCH] }, |
| 501 | wrapped({ enum: ['asc'], anyOf: [{ type: 'string' }, NULL_BRANCH] }), |
| 502 | ], |
| 503 | [ |
| 504 | { $ref: '#/$defs/Direction', type: 'string' }, |
| 505 | wrapped({ $ref: '#/$defs/Direction', type: 'string' }), |
| 506 | ], |
| 507 | // Already nullable: left alone. |
| 508 | [ |
| 509 | { type: ['string', 'null'], enum: ['asc', null] }, |
| 510 | { type: ['string', 'null'], enum: ['asc', null] }, |
| 511 | ], |
| 512 | [{ $ref: '#/$defs/Direction' }, { $ref: '#/$defs/Direction' }], |
| 513 | [{ anyOf: [{ type: ['string', 'null'] }] }, { anyOf: [{ type: ['string', 'null'] }] }], |
| 514 | // `type` or `anyOf` as the only obstacle: widened in place. |
| 515 | [ |
| 516 | { type: 'string', minLength: 1 }, |
| 517 | { type: ['string', 'null'], minLength: 1 }, |
| 518 | ], |
| 519 | [ |
| 520 | { anyOf: [{ type: 'string' }], description: 'd' }, |
| 521 | { anyOf: [{ type: 'string' }, NULL_BRANCH], description: 'd' }, |
| 522 | ], |
| 523 | ])('widening keeps every constraint: %j', (property, expected) => { |
| 524 | const source = toolSchema(property); |
| 525 | const snapshot = structuredClone(source); |
| 526 | const result = toStrictJsonSchema(source); |
| 527 | |
| 528 | expect(result.unsupported).toEqual([]); |
| 529 | expect(propertyOf(result.schema, 'value')).toEqual(expected); |
| 530 | expect(result.schema.required).toEqual(['value']); |
| 531 | expect(source).toEqual(snapshot); |
| 532 | const again = toStrictJsonSchema(result.schema); |
| 533 | expect(again.schema).toEqual(result.schema); |
| 534 | expect(again.changes).toEqual([]); |
| 535 | }); |
| 536 | |
| 537 | it.each<[unknown, boolean]>([ |
| 538 | [{ type: ['string', 'null'], enum: ['asc', 'desc'] }, false], |
| 539 | [{ type: ['string', 'null'], const: 'asc' }, false], |
| 540 | [{ type: ['string', 'null'], enum: ['asc', null] }, true], |
| 541 | [{ type: ['string', 'null'], const: null }, true], |
| 542 | [{ $ref: '#/$defs/Direction' }, true], |
| 543 | [{ $ref: '#/$defs/Direction', type: 'string' }, false], |
| 544 | [{ $ref: '#/$defs/Direction', enum: ['asc'] }, false], |
| 545 | [{ $ref: '#/$defs/Missing' }, false], |
| 546 | [{ $ref: 1 }, false], |
| 547 | [{ allOf: [{ type: ['string', 'null'] }, { enum: ['asc'] }] }, false], |
| 548 | [{ allOf: [{ type: ['string', 'null'] }, DIRECTION] }, true], |
| 549 | [{ oneOf: [NULL_BRANCH, { type: 'string' }] }, true], |
| 550 | [{ oneOf: [NULL_BRANCH, { enum: [null] }] }, false], |
| 551 | [{ not: NULL_BRANCH }, false], |
| 552 | [{ not: { type: 'string' } }, true], |
| 553 | [{ if: NULL_BRANCH, then: { type: 'string' } }, false], |
| 554 | [{ if: { type: 'string' }, then: { type: 'string' } }, true], |
| 555 | [{ if: { type: 'string' }, else: { type: 'string' } }, false], |
| 556 | [true, true], |
| 557 | [false, false], |
| 558 | ])('null is kept only when every keyword accepts it: %j', (property, kept) => { |
| 559 | const input = { value: null }; |
| 560 | expect(omitNullToolArguments(input, toolSchema(property))).toEqual(kept ? { value: null } : {}); |
| 561 | expect(input).toEqual({ value: null }); |
| 562 | }); |
| 563 | |
| 564 | it.each<[Record<string, unknown>, boolean]>([ |
| 565 | [{ a: { $ref: '#/$defs/a' } }, false], |
| 566 | [{ a: { $ref: '#/$defs/b' }, b: { $ref: '#/$defs/a' } }, false], |
| 567 | [{ a: { anyOf: [{ $ref: '#/$defs/a' }] } }, false], |
| 568 | [{ a: { type: 'null', oneOf: [{ $ref: '#/$defs/a' }] } }, false], |
| 569 | [{ a: { anyOf: [{ $ref: '#/$defs/b' }] }, b: { anyOf: [{ $ref: '#/$defs/a' }] } }, false], |
| 570 | [{ a: { anyOf: [{ $ref: '#/$defs/a' }, NULL_BRANCH] } }, true], |
| 571 | [{ a: { oneOf: [{ $ref: '#/$defs/a' }, NULL_BRANCH] } }, true], |
| 572 | ])('reference cycles terminate: %j', (definitions, kept) => { |
| 573 | const source = toolSchema({ $ref: '#/$defs/a' }, definitions); |
| 574 | expect(omitNullToolArguments({ value: null }, source)).toEqual(kept ? { value: null } : {}); |
| 575 | expect(toStrictJsonSchema(source).unsupported).toEqual([]); |
| 576 | }); |
| 577 | |
| 578 | it('checks a node reached twice outside a cycle each time', () => { |
| 579 | const shared = { anyOf: [NULL_BRANCH] }; |
| 580 | const source = { type: 'object', properties: { value: { allOf: [shared, shared] } } }; |
| 581 | expect(omitNullToolArguments({ value: null }, source)).toEqual({ value: null }); |
| 582 | }); |
| 583 | |
| 584 | /** `a0` is the leaf; every `a(i)` references `a(i-1)` twice, so paths double per level. */ |
| 585 | const diamond = (levels: number, keyword: string, leaf: unknown) => { |
| 586 | const definitions: Record<string, unknown> = { a0: leaf }; |
| 587 | for (let i = 1; i <= levels; i++) { |
| 588 | const ref = { $ref: `#/$defs/a${i - 1}` }; |
| 589 | definitions[`a${i}`] = { [keyword]: [ref, ref] }; |
| 590 | } |
| 591 | return toolSchema({ $ref: `#/$defs/a${levels}` }, definitions); |
| 592 | }; |
| 593 | |
| 594 | it('evaluates a definition shared by many branches once', () => { |
| 595 | const rejecting = diamond(20, 'anyOf', { type: 'string' }); |
| 596 | expect(propertyOf(toStrictJsonSchema(rejecting).schema, 'value')).toEqual( |
| 597 | wrapped({ $ref: '#/$defs/a20' }) |
| 598 | ); |
| 599 | expect(omitNullToolArguments({ value: null }, rejecting)).toEqual({}); |
| 600 | |
| 601 | const accepting = diamond(20, 'allOf', NULL_BRANCH); |
| 602 | expect(omitNullToolArguments({ value: null }, accepting)).toEqual({ value: null }); |
| 603 | }); |
| 604 | |
| 605 | it('stays bounded when cycles keep answers from being remembered', () => { |
| 606 | // Every level also points back at the top, so no answer below it is final. |
| 607 | const levels = 20; |
| 608 | const definitions: Record<string, unknown> = { a0: { type: 'string' } }; |
| 609 | for (let i = 1; i <= levels; i++) { |
| 610 | const ref = { $ref: `#/$defs/a${i - 1}` }; |
| 611 | definitions[`a${i}`] = { anyOf: [ref, ref, { $ref: `#/$defs/a${levels}` }] }; |
| 612 | } |
| 613 | const source = toolSchema({ $ref: `#/$defs/a${levels}` }, definitions); |
| 614 | expect(omitNullToolArguments({ value: null }, source)).toEqual({}); |
| 615 | expect(propertyOf(toStrictJsonSchema(source).schema, 'value')).toEqual( |
| 616 | wrapped({ $ref: `#/$defs/a${levels}` }) |
| 617 | ); |
| 618 | }); |
| 619 | |
| 620 | it('does not prove null through a reference chain past the depth bound', () => { |
| 621 | const definitions: Record<string, unknown> = { a0: NULL_BRANCH }; |
| 622 | for (let i = 1; i <= 100; i++) definitions[`a${i}`] = { $ref: `#/$defs/a${i - 1}` }; |
| 623 | expect( |
| 624 | omitNullToolArguments({ value: null }, toolSchema({ $ref: '#/$defs/a100' }, definitions)) |
| 625 | ).toEqual({}); |
| 626 | expect( |
| 627 | omitNullToolArguments({ value: null }, toolSchema({ $ref: '#/$defs/a10' }, definitions)) |
| 628 | ).toEqual({ value: null }); |
| 629 | }); |
| 630 | |
| 631 | it('adds a null branch to a $ref whose target the rewrite types as an object', () => { |
| 632 | const source = { |
| 633 | type: 'object', |
| 634 | properties: { node: { $ref: '#/$defs/Node' } }, |
| 635 | $defs: { |
| 636 | Node: { properties: { label: { type: 'string' }, child: { $ref: '#/$defs/Node' } } }, |
| 637 | }, |
| 638 | }; |
| 639 | const { schema, unsupported } = toStrictJsonSchema(source); |
| 640 | const definitions = schema.$defs as Record<string, Record<string, unknown>>; |
| 641 | |
| 642 | expect(unsupported).toEqual([]); |
| 643 | expect(propertyOf(schema, 'node')).toEqual(wrapped({ $ref: '#/$defs/Node' })); |
| 644 | expect(definitions.Node.type).toBe('object'); |
| 645 | expect(propertyOf(definitions.Node, 'child')).toEqual(wrapped({ $ref: '#/$defs/Node' })); |
| 646 | // The tool's own schema accepts the null, so it is forwarded. |
| 647 | expect(omitNullToolArguments({ node: null }, source)).toEqual({ node: null }); |
| 648 | }); |
| 649 | |
| 650 | it('reports a $ref into a property that wrapping moved', () => { |
| 651 | const { unsupported } = toStrictJsonSchema({ |
| 652 | type: 'object', |
| 653 | properties: { |
| 654 | value: { type: 'string', enum: ['asc'], $defs: { Text: { type: 'string' } } }, |
| 655 | alias: { $ref: '#/properties/value/$defs/Text' }, |
| 656 | }, |
| 657 | required: ['alias'], |
| 658 | }); |
| 659 | expect(unsupported.map(({ path, keyword }) => ({ path, keyword }))).toEqual([ |
| 660 | { path: 'properties.alias', keyword: '$ref' }, |
| 661 | ]); |
| 662 | }); |
| 663 | |
| 664 | it('shares what it learned about a cyclic definition across many properties', () => { |
| 665 | const levels = 20; |
| 666 | const top = `#/$defs/a${levels}`; |
| 667 | const definitions: Record<string, unknown> = { a0: { type: 'string' } }; |
| 668 | for (let i = 1; i <= levels; i++) { |
| 669 | const lower = `#/$defs/a${i - 1}`; |
| 670 | definitions[`a${i}`] = { anyOf: [{ $ref: lower }, { $ref: lower }, { $ref: top }] }; |
| 671 | } |
| 672 | const names = Array.from({ length: 400 }, (_, i) => `p${i}`); |
| 673 | const source = { |
| 674 | type: 'object', |
| 675 | properties: Object.fromEntries(names.map(name => [name, { $ref: top }])), |
| 676 | $defs: definitions, |
| 677 | }; |
| 678 | const { schema } = toStrictJsonSchema(source); |
| 679 | for (const name of names) expect(propertyOf(schema, name)).toEqual(wrapped({ $ref: top })); |
| 680 | const input = Object.fromEntries(names.map(name => [name, null])); |
| 681 | expect(omitNullToolArguments(input, source)).toEqual({}); |
| 682 | }); |
| 683 | |
| 684 | it('keeps a check that runs out of budget from affecting the next one', () => { |
| 685 | const source = { |
| 686 | type: 'object', |
| 687 | properties: { |
| 688 | wide: { anyOf: Array.from({ length: 5000 }, () => ({ type: 'string' })) }, |
| 689 | note: { type: ['string', 'null'] }, |
| 690 | same: { anyOf: [{ type: 'string' }, NULL_BRANCH] }, |
| 691 | }, |
| 692 | }; |
| 693 | expect(omitNullToolArguments({ wide: null, note: null, same: null }, source)).toEqual({ |
| 694 | note: null, |
| 695 | same: null, |
| 696 | }); |
| 697 | const { schema } = toStrictJsonSchema(source); |
| 698 | expect(propertyOf(schema, 'note')).toEqual({ type: ['string', 'null'] }); |
| 699 | expect(propertyOf(schema, 'same')).toEqual({ anyOf: [{ type: 'string' }, NULL_BRANCH] }); |
| 700 | }); |
| 701 | |
| 702 | it('proves nothing in a check that meets a bound', () => { |
| 703 | // Both branches accept null, so `oneOf` rejects it; the second is only |
| 704 | // reachable past the depth bound and must not count as a rejection. |
| 705 | const definitions: Record<string, unknown> = { a0: NULL_BRANCH }; |
| 706 | for (let i = 1; i <= 70; i++) definitions[`a${i}`] = { $ref: `#/$defs/a${i - 1}` }; |
| 707 | const deep = { $ref: '#/$defs/a70' }; |
| 708 | const properties = { |
| 709 | one: { oneOf: [NULL_BRANCH, deep] }, |
| 710 | negated: { not: deep }, |
| 711 | guarded: { if: deep, else: NULL_BRANCH }, |
| 712 | // Reaches a node the checks above left unproven. |
| 713 | later: { not: { $ref: '#/$defs/a69' } }, |
| 714 | }; |
| 715 | const source = { type: 'object', properties, $defs: definitions }; |
| 716 | const input = Object.fromEntries(Object.keys(properties).map(name => [name, null])); |
| 717 | expect(omitNullToolArguments(input, source)).toEqual({}); |
| 718 | }); |
| 719 | |
| 720 | it('does not evaluate an unproven definition again for each property', () => { |
| 721 | // `top` is cyclic and wider than the budget, so nothing about it settles. |
| 722 | const top = { $ref: '#/$defs/top' }; |
| 723 | const definitions: Record<string, unknown> = { |
| 724 | top: { anyOf: Array.from({ length: 3000 }, (_, i) => ({ $ref: `#/$defs/b${i}` })) }, |
| 725 | }; |
| 726 | for (let i = 0; i < 3000; i++) definitions[`b${i}`] = { anyOf: [top, { type: 'string' }] }; |
| 727 | const names = Array.from({ length: 2000 }, (_, i) => `p${i}`); |
| 728 | const source = { |
| 729 | type: 'object', |
| 730 | properties: Object.fromEntries(names.map(name => [name, { ...top }])), |
| 731 | $defs: definitions, |
| 732 | }; |
| 733 | const input = Object.fromEntries(names.map(name => [name, null])); |
| 734 | expect(omitNullToolArguments(input, source)).toEqual({}); |
| 735 | const { schema } = toStrictJsonSchema(source); |
| 736 | for (const name of names) expect(propertyOf(schema, name)).toEqual(wrapped(top)); |
| 737 | }); |
| 738 | }); |