Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 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 | 'string-format-outside-accepted-list-stripped', |
| 130 | 'A string `format` the API rejects is stripped and reported, including on a schema without `type` and for an accepted value in the wrong case; formats on numbers and a property named `format` stay.', |
| 131 | obj( |
| 132 | { |
| 133 | url: { type: 'string', format: 'uri' }, |
| 134 | file: { type: ['string', 'null'], format: 'path' }, |
| 135 | repos: { type: 'array', items: { type: 'string', format: 'repo.nwo' } }, |
| 136 | untyped: { format: 'uri' }, |
| 137 | shouted: { type: 'string', format: 'DATE-TIME' }, |
| 138 | count: { type: 'integer', format: 'int64' }, |
| 139 | format: { type: 'string', enum: ['json', 'csv'] }, |
| 140 | }, |
| 141 | ['url', 'file', 'repos', 'untyped', 'shouted', 'count', 'format'] |
| 142 | ), |
| 143 | ], |
| 144 | [ |
| 145 | 'accepted-string-formats-kept', |
| 146 | 'Every string `format` the API accepts is kept unchanged.', |
| 147 | obj( |
| 148 | Object.fromEntries( |
| 149 | ['date-time', 'time', 'date', 'duration', 'email', 'hostname', 'ipv4', 'ipv6', 'uuid'].map( |
| 150 | format => [format, { type: 'string', format }] |
| 151 | ) |
| 152 | ), |
| 153 | ['date-time', 'time', 'date', 'duration', 'email', 'hostname', 'ipv4', 'ipv6', 'uuid'], |
| 154 | { additionalProperties: false } |
| 155 | ), |
| 156 | ], |
| 157 | [ |
| 158 | 'openapi-nullable-folded', |
| 159 | 'OpenAPI `nullable`, which Anthropic rejects on an object, is removed; `true` widens the node to accept null the way the Zod conversion reads it, including on a typed enum, and `false` is dropped.', |
| 160 | obj( |
| 161 | { |
| 162 | cfg: { |
| 163 | type: 'object', |
| 164 | nullable: true, |
| 165 | properties: { a: { type: 'string' } }, |
| 166 | required: ['a'], |
| 167 | }, |
| 168 | note: { type: 'string', nullable: true }, |
| 169 | mode: { type: 'string', enum: ['asc', 'desc'], nullable: true }, |
| 170 | untyped: { nullable: true, description: 'anything' }, |
| 171 | count: { type: 'integer', nullable: false }, |
| 172 | rows: { |
| 173 | type: 'array', |
| 174 | items: { type: 'object', nullable: true, properties: { id: { type: 'string' } } }, |
| 175 | }, |
| 176 | }, |
| 177 | ['cfg', 'mode', 'count', 'rows'] |
| 178 | ), |
| 179 | ], |
| 180 | [ |
| 181 | 'recursive-nullable-union-gets-null-branch', |
| 182 | 'A `nullable: true` union that refers back to itself, or a nullable reference to the root, gains a null branch, because a reference never counts as accepting null here; a reference that does accept null costs a redundant branch.', |
| 183 | obj( |
| 184 | { a: { $ref: '#/$defs/A' }, b: { $ref: '#/$defs/B' }, self: { $ref: '#', nullable: true } }, |
| 185 | ['a', 'b', 'self'], |
| 186 | { |
| 187 | $defs: { |
| 188 | A: { anyOf: [{ $ref: '#/$defs/A' }, { type: 'string' }], nullable: true }, |
| 189 | B: { anyOf: [{ $ref: '#/$defs/Nullable' }, { type: 'string' }], nullable: true }, |
| 190 | Nullable: { type: ['string', 'null'] }, |
| 191 | }, |
| 192 | } |
| 193 | ), |
| 194 | ], |
| 195 | [ |
| 196 | 'enum-values-are-data-not-keywords', |
| 197 | 'Keys inside enum/const values are data and must not be treated as keywords.', |
| 198 | obj({ opts: { enum: [{ default: 1, properties: {} }, null] } }, ['opts']), |
| 199 | ], |
| 200 | [ |
| 201 | 'property-names-are-not-keywords', |
| 202 | 'Properties named like keywords are ordinary properties.', |
| 203 | obj({ |
| 204 | default: { type: 'string' }, |
| 205 | $ref: { type: 'string' }, |
| 206 | type: { type: 'string' }, |
| 207 | properties: obj({ x: { type: 'string' } }), |
| 208 | }), |
| 209 | ], |
| 210 | [ |
| 211 | 'prototype-key-property-name', |
| 212 | 'A property named __proto__ or constructor is handled as a plain name.', |
| 213 | JSON.parse( |
| 214 | '{"type":"object","properties":{"__proto__":{"type":"string"},"constructor":{"type":"number"}}}' |
| 215 | ), |
| 216 | ], |
| 217 | [ |
| 218 | 'schema-valued-additional-properties-unsupported', |
| 219 | 'A map-style object cannot be expressed and is reported, not closed.', |
| 220 | obj({ headers: { type: 'object', additionalProperties: { type: 'string' } } }, ['headers']), |
| 221 | ], |
| 222 | [ |
| 223 | 'additional-properties-true-unsupported', |
| 224 | 'An explicitly open object is reported.', |
| 225 | obj({ open: { type: 'object', additionalProperties: true } }, ['open']), |
| 226 | ], |
| 227 | [ |
| 228 | 'additional-properties-empty-schema-unsupported', |
| 229 | 'An empty-schema additionalProperties accepts anything and is reported.', |
| 230 | obj({ open: { type: 'object', additionalProperties: {} } }, ['open']), |
| 231 | ], |
| 232 | [ |
| 233 | 'free-form-object-unsupported', |
| 234 | 'A property-less object accepts arbitrary keys and is reported.', |
| 235 | obj({ meta: { type: 'object', description: 'any json' } }, ['meta']), |
| 236 | ], |
| 237 | [ |
| 238 | 'pattern-properties-unsupported', |
| 239 | 'patternProperties cannot be expressed and is reported.', |
| 240 | obj({ tagged: { type: 'object', patternProperties: { '^x-': { type: 'string' } } } }, [ |
| 241 | 'tagged', |
| 242 | ]), |
| 243 | ], |
| 244 | [ |
| 245 | 'empty-closed-object-representable', |
| 246 | 'An object closed with no properties is representable.', |
| 247 | obj({ empty: { type: 'object', additionalProperties: false } }, ['empty']), |
| 248 | ], |
| 249 | [ |
| 250 | 'all-of-unsupported', |
| 251 | 'allOf has no lossless rewrite and is reported.', |
| 252 | obj({ all: { allOf: [{ type: 'string' }] } }, ['all']), |
| 253 | ], |
| 254 | [ |
| 255 | 'prefix-items-unsupported', |
| 256 | 'prefixItems has no lossless rewrite and is reported.', |
| 257 | obj({ pair: { type: 'array', prefixItems: [{ type: 'number' }] } }, ['pair']), |
| 258 | ], |
| 259 | [ |
| 260 | 'defs-ref-kept-and-definition-normalized', |
| 261 | 'Local $refs are kept and the definition is normalized where it is declared.', |
| 262 | obj({ cfg: { $ref: '#/$defs/Config' } }, ['cfg'], { |
| 263 | $defs: { Config: obj({ url: { type: 'string' }, note: { type: 'string' } }, ['url']) }, |
| 264 | }), |
| 265 | ], |
| 266 | [ |
| 267 | 'optional-ref-widened-with-null-branch', |
| 268 | 'An optional $ref property is widened with an anyOf null branch.', |
| 269 | obj({ cfg: { $ref: '#/$defs/Config', description: 'optional' } }, undefined, { |
| 270 | $defs: { Config: obj({ url: { type: 'string' } }, ['url']) }, |
| 271 | }), |
| 272 | ], |
| 273 | [ |
| 274 | 'recursive-defs-kept', |
| 275 | 'Self-recursive definitions are representable.', |
| 276 | obj({ node: { $ref: '#/$defs/Node' } }, ['node'], { |
| 277 | $defs: { |
| 278 | Node: obj({ child: { $ref: '#/$defs/Node' }, label: { type: 'string' } }, ['label']), |
| 279 | }, |
| 280 | }), |
| 281 | ], |
| 282 | [ |
| 283 | 'mutually-recursive-defs-kept', |
| 284 | 'Mutually recursive definitions are representable.', |
| 285 | obj({ a: { $ref: '#/$defs/A' } }, ['a'], { |
| 286 | $defs: { A: obj({ b: { $ref: '#/$defs/B' } }), B: obj({ a: { $ref: '#/$defs/A' } }) }, |
| 287 | }), |
| 288 | ], |
| 289 | [ |
| 290 | 'legacy-definitions-ref-kept', |
| 291 | 'Legacy definitions are supported like $defs.', |
| 292 | obj({ cfg: { $ref: '#/definitions/Config' } }, ['cfg'], { |
| 293 | definitions: { Config: obj({ url: { type: 'string' } }) }, |
| 294 | }), |
| 295 | ], |
| 296 | [ |
| 297 | 'ref-into-properties-kept', |
| 298 | 'A local $ref outside $defs resolves and is kept.', |
| 299 | obj({ a: obj({ x: { type: 'string' } }, ['x']), b: { $ref: '#/properties/a' } }, ['a', 'b']), |
| 300 | ], |
| 301 | [ |
| 302 | 'root-ref-kept', |
| 303 | 'A local $ref to the document root resolves and is kept.', |
| 304 | obj({ child: { $ref: '#' }, label: { type: 'string' } }, ['label']), |
| 305 | ], |
| 306 | [ |
| 307 | 'dangling-ref-unsupported', |
| 308 | 'A $ref without a target is reported at its own path.', |
| 309 | obj({ cfg: { $ref: '#/$defs/Nope' } }, ['cfg']), |
| 310 | ], |
| 311 | [ |
| 312 | 'external-ref-unsupported', |
| 313 | 'A $ref outside the document is reported.', |
| 314 | obj({ cfg: { $ref: 'https://example.com/schema.json' } }, ['cfg']), |
| 315 | ], |
| 316 | [ |
| 317 | 'duplicate-required-deduped', |
| 318 | 'Duplicate required entries are removed.', |
| 319 | obj({ a: { type: 'string' } }, ['a', 'a']), |
| 320 | ], |
| 321 | [ |
| 322 | 'required-unknown-names-dropped', |
| 323 | 'Required names without a property are dropped from required.', |
| 324 | obj({ a: { type: 'string' } }, ['a', 'ghost']), |
| 325 | ], |
| 326 | [ |
| 327 | 'required-not-an-array-ignored', |
| 328 | 'A malformed required is treated as absent.', |
| 329 | obj({ a: { type: 'string' } }, 'a'), |
| 330 | ], |
| 331 | [ |
| 332 | 'description-kept-on-widened-node', |
| 333 | 'Annotations survive widening.', |
| 334 | obj({ a: { type: 'integer', description: 'count', title: 'Count', minimum: 0 } }), |
| 335 | ], |
| 336 | ['non-object-root-unsupported', 'The root must be an object.', { type: 'string' }], |
| 337 | [ |
| 338 | 'nullable-root-unsupported', |
| 339 | 'The root must not be nullable.', |
| 340 | { type: ['object', 'null'], properties: { a: { type: 'string' } } }, |
| 341 | ], |
| 342 | [ |
| 343 | 'properties-without-type-become-object', |
| 344 | 'A node with properties but no type is treated as an object.', |
| 345 | obj({ cfg: { properties: { a: { type: 'string' } } } }, ['cfg']), |
| 346 | ], |
| 347 | [ |
| 348 | 'root-without-type-become-object', |
| 349 | 'A root with properties but no type is treated as an object.', |
| 350 | { properties: { a: { type: 'string' } } }, |
| 351 | ], |
| 352 | // Edge cases enumerated independently with a second model. |
| 353 | [ |
| 354 | 'root-single-element-type-array-accepted', |
| 355 | 'A root typed ["object"] is an object root.', |
| 356 | { type: ['object'], properties: { a: { type: 'string' } }, required: ['a'] }, |
| 357 | ], |
| 358 | [ |
| 359 | 'three-member-type-array-gains-null', |
| 360 | 'A three-member type array gains null once.', |
| 361 | obj({ v: { type: ['string', 'number', 'boolean'] } }), |
| 362 | ], |
| 363 | [ |
| 364 | 'optional-null-only-property-unchanged', |
| 365 | 'A property typed null already accepts null.', |
| 366 | obj({ nothing: { type: 'null' } }), |
| 367 | ], |
| 368 | [ |
| 369 | 'optional-enum-with-null-not-rewrapped', |
| 370 | 'An enum that already lists null is not wrapped again.', |
| 371 | obj({ choice: { enum: ['a', null] } }), |
| 372 | ], |
| 373 | [ |
| 374 | 'optional-const-null-not-rewrapped', |
| 375 | 'A const null already accepts null.', |
| 376 | obj({ nothing: { const: null } }), |
| 377 | ], |
| 378 | [ |
| 379 | 'nested-any-of-branch-widened', |
| 380 | 'Optional properties inside anyOf branches are widened.', |
| 381 | obj({ v: { anyOf: [obj({ a: { type: 'string' } }), { type: 'string' }] } }, ['v']), |
| 382 | ], |
| 383 | [ |
| 384 | 'array-of-nullable-objects-closed', |
| 385 | 'Nullable objects inside array items are closed and stay nullable.', |
| 386 | obj( |
| 387 | { |
| 388 | rows: { |
| 389 | type: 'array', |
| 390 | items: { type: ['object', 'null'], properties: { id: { type: 'string' } } }, |
| 391 | }, |
| 392 | }, |
| 393 | ['rows'] |
| 394 | ), |
| 395 | ], |
| 396 | [ |
| 397 | 'tuple-form-items-unsupported', |
| 398 | 'Draft-4 tuple items have no strict-mode equivalent.', |
| 399 | obj({ pair: { type: 'array', items: [{ type: 'number' }, { type: 'string' }] } }, ['pair']), |
| 400 | ], |
| 401 | [ |
| 402 | 'boolean-items-unsupported', |
| 403 | 'A boolean items subschema is reported.', |
| 404 | obj({ xs: { type: 'array', items: true } }, ['xs']), |
| 405 | ], |
| 406 | [ |
| 407 | 'not-keyword-unsupported', |
| 408 | 'not has no strict-mode equivalent.', |
| 409 | obj({ v: { not: { type: 'string' } } }, ['v']), |
| 410 | ], |
| 411 | [ |
| 412 | 'if-then-else-unsupported', |
| 413 | 'Conditional keywords have no strict-mode equivalent.', |
| 414 | obj( |
| 415 | { v: { type: 'string', if: { minLength: 1 }, then: { maxLength: 5 }, else: { const: '' } } }, |
| 416 | ['v'] |
| 417 | ), |
| 418 | ], |
| 419 | [ |
| 420 | 'dependent-schemas-unsupported', |
| 421 | 'dependentSchemas has no strict-mode equivalent.', |
| 422 | obj({ a: { type: 'string' } }, ['a'], { |
| 423 | dependentSchemas: { a: obj({ b: { type: 'string' } }) }, |
| 424 | }), |
| 425 | ], |
| 426 | [ |
| 427 | 'property-names-keyword-unsupported', |
| 428 | 'propertyNames has no strict-mode equivalent.', |
| 429 | obj({ a: { type: 'string' } }, ['a'], { propertyNames: { pattern: '^a' } }), |
| 430 | ], |
| 431 | [ |
| 432 | 'one-of-beside-any-of-unsupported', |
| 433 | 'oneOf next to anyOf cannot be merged and is reported.', |
| 434 | obj({ v: { anyOf: [{ type: 'string' }], oneOf: [{ type: 'number' }] } }, ['v']), |
| 435 | ], |
| 436 | [ |
| 437 | 'boolean-property-subschema-unsupported', |
| 438 | 'A boolean property subschema is reported.', |
| 439 | obj({ anything: true, nothing: false }, ['anything', 'nothing']), |
| 440 | ], |
| 441 | [ |
| 442 | 'properties-not-an-object-unsupported', |
| 443 | 'A malformed properties value is reported instead of emptied.', |
| 444 | obj('not-a-map', ['a']), |
| 445 | ], |
| 446 | [ |
| 447 | 'ref-with-siblings-kept', |
| 448 | 'Sibling keywords next to a $ref are kept.', |
| 449 | obj({ cfg: { $ref: '#/$defs/Config', description: 'cfg', title: 'Config' } }, ['cfg'], { |
| 450 | $defs: { Config: obj({ a: { type: 'string' } }, ['a']) }, |
| 451 | }), |
| 452 | ], |
| 453 | [ |
| 454 | 'required-non-string-entries-ignored', |
| 455 | 'Non-string required entries are ignored.', |
| 456 | obj({ a: { type: 'string' }, b: { type: 'string' } }, ['a', 1, null]), |
| 457 | ], |
| 458 | [ |
| 459 | 'dynamic-keys-reported-once-with-pattern-properties', |
| 460 | 'additionalProperties and patternProperties on one node report additionalProperties.', |
| 461 | obj( |
| 462 | { |
| 463 | m: { |
| 464 | type: 'object', |
| 465 | additionalProperties: { type: 'string' }, |
| 466 | patternProperties: { '^x': { type: 'string' } }, |
| 467 | }, |
| 468 | }, |
| 469 | ['m'] |
| 470 | ), |
| 471 | ], |
| 472 | [ |
| 473 | 'empty-properties-open-object-unsupported', |
| 474 | 'An object with empty properties and no closing is free-form.', |
| 475 | obj({ m: { type: 'object', properties: {} } }, ['m']), |
| 476 | ], |
| 477 | [ |
| 478 | 'default-stripped-then-widened', |
| 479 | 'Stripping default and widening compose.', |
| 480 | obj({ a: { type: 'string', default: 'x' } }), |
| 481 | ], |
| 482 | [ |
| 483 | 'nested-any-of-in-any-of-kept', |
| 484 | 'A composition nested inside a composition is normalized branch by branch.', |
| 485 | obj( |
| 486 | { |
| 487 | v: { |
| 488 | anyOf: [ |
| 489 | { anyOf: [obj({ a: { type: 'string' } }), { type: 'number' }] }, |
| 490 | { type: 'string' }, |
| 491 | ], |
| 492 | }, |
| 493 | }, |
| 494 | ['v'] |
| 495 | ), |
| 496 | ], |
| 497 | [ |
| 498 | 'definitions-and-defs-both-normalized', |
| 499 | 'Legacy definitions and $defs coexist and both are normalized.', |
| 500 | obj({ a: { $ref: '#/$defs/A' }, b: { $ref: '#/definitions/B' } }, ['a', 'b'], { |
| 501 | $defs: { A: obj({ x: { type: 'string' } }) }, |
| 502 | definitions: { B: obj({ y: { type: 'string' } }) }, |
| 503 | }), |
| 504 | ], |
| 505 | [ |
| 506 | 'deep-nesting-normalized-fully', |
| 507 | 'Ten levels of nested objects are normalized at every level.', |
| 508 | (() => { |
| 509 | let node = { type: 'object', properties: { leaf: { type: 'string' } } }; |
| 510 | for (let i = 0; i < 10; i++) |
| 511 | node = { type: 'object', properties: { child: node }, required: ['child'] }; |
| 512 | return node; |
| 513 | })(), |
| 514 | ], |
| 515 | [ |
| 516 | 'chained-refs-resolved', |
| 517 | 'A $ref to a $ref resolves to the final definition.', |
| 518 | obj({ a: { $ref: '#/$defs/Alias' } }, ['a'], { |
| 519 | $defs: { Alias: { $ref: '#/$defs/Real' }, Real: obj({ x: { type: 'string' } }, ['x']) }, |
| 520 | }), |
| 521 | ], |
| 522 | [ |
| 523 | 'optional-typed-enum-wrapped', |
| 524 | 'A typed enum is wrapped whole: adding null to its type would leave the enum rejecting it.', |
| 525 | obj({ direction: { type: 'string', enum: ['asc', 'desc'], description: 'sort order' } }), |
| 526 | ], |
| 527 | [ |
| 528 | 'optional-nullable-typed-enum-wrapped', |
| 529 | 'A null in `type` does not make an enum without null nullable.', |
| 530 | obj({ direction: { type: ['string', 'null'], enum: ['asc', 'desc'] } }), |
| 531 | ], |
| 532 | [ |
| 533 | 'optional-typed-const-wrapped', |
| 534 | 'A typed const is wrapped whole, like a typed enum.', |
| 535 | obj({ mode: { type: 'string', const: 'asc' } }), |
| 536 | ], |
| 537 | [ |
| 538 | 'optional-typed-enum-with-null-unchanged', |
| 539 | 'A typed enum that lists null under a nullable type already accepts null.', |
| 540 | obj({ direction: { type: ['string', 'null'], enum: ['asc', null] } }), |
| 541 | ], |
| 542 | [ |
| 543 | 'optional-any-of-with-sibling-enum-wrapped', |
| 544 | 'A null branch in anyOf does not lift a sibling enum, so the node is wrapped.', |
| 545 | obj({ direction: { enum: ['asc', 'desc'], anyOf: [{ type: 'string' }, { type: 'null' }] } }), |
| 546 | ], |
| 547 | [ |
| 548 | 'optional-ref-with-sibling-type-wrapped', |
| 549 | 'Keywords beside a $ref apply together with its target.', |
| 550 | obj({ direction: { $ref: '#/$defs/Direction', type: 'string' } }, undefined, { |
| 551 | $defs: { Direction: { enum: ['asc', null] } }, |
| 552 | }), |
| 553 | ], |
| 554 | [ |
| 555 | 'optional-ref-to-nullable-definition-unchanged', |
| 556 | 'A $ref whose target accepts null needs no extra null branch.', |
| 557 | obj({ note: { $ref: '#/$defs/Note' } }, undefined, { |
| 558 | $defs: { Note: { type: ['string', 'null'] } }, |
| 559 | }), |
| 560 | ], |
| 561 | [ |
| 562 | 'optional-ref-to-typeless-object-wrapped', |
| 563 | 'A definition with properties but no type is typed as an object by the rewrite, so a $ref to it needs a null branch.', |
| 564 | obj({ cfg: { $ref: '#/$defs/Config' } }, undefined, { |
| 565 | $defs: { Config: { properties: { url: { type: 'string' } }, required: ['url'] } }, |
| 566 | }), |
| 567 | ], |
| 568 | [ |
| 569 | 'ref-into-wrapped-property-unsupported', |
| 570 | 'Wrapping a property moves what is nested in it; a $ref into it would dangle.', |
| 571 | obj( |
| 572 | { |
| 573 | value: { type: 'string', enum: ['asc'], $defs: { Text: { type: 'string' } } }, |
| 574 | alias: { $ref: '#/properties/value/$defs/Text' }, |
| 575 | }, |
| 576 | ['alias'] |
| 577 | ), |
| 578 | ], |
| 579 | ]; |
| 580 | const argumentCases = { |
| 581 | 'recursive-nullable-union-gets-null-branch': [ |
| 582 | { input: { a: null, b: null, self: null }, output: { a: null, b: null, self: null } }, |
| 583 | ], |
| 584 | 'openapi-nullable-folded': [ |
| 585 | { |
| 586 | input: { |
| 587 | cfg: null, |
| 588 | note: null, |
| 589 | mode: null, |
| 590 | untyped: null, |
| 591 | count: null, |
| 592 | rows: [null, { id: null }], |
| 593 | }, |
| 594 | output: { cfg: null, note: null, mode: null, untyped: null, rows: [null, {}] }, |
| 595 | }, |
| 596 | ], |
| 597 | 'optional-ref-to-typeless-object-wrapped': [ |
| 598 | { input: { cfg: null }, output: { cfg: null } }, |
| 599 | { input: { cfg: { url: 'u' } }, output: { cfg: { url: 'u' } } }, |
| 600 | ], |
| 601 | 'optional-typed-enum-wrapped': [ |
| 602 | { input: { direction: null }, output: {} }, |
| 603 | { input: { direction: 'asc' }, output: { direction: 'asc' } }, |
| 604 | ], |
| 605 | 'optional-nullable-typed-enum-wrapped': [{ input: { direction: null }, output: {} }], |
| 606 | 'optional-typed-const-wrapped': [{ input: { mode: null }, output: {} }], |
| 607 | 'optional-typed-enum-with-null-unchanged': [ |
| 608 | { input: { direction: null }, output: { direction: null } }, |
| 609 | ], |
| 610 | 'optional-any-of-with-sibling-enum-wrapped': [{ input: { direction: null }, output: {} }], |
| 611 | 'optional-ref-with-sibling-type-wrapped': [{ input: { direction: null }, output: {} }], |
| 612 | 'optional-ref-to-nullable-definition-unchanged': [ |
| 613 | { input: { note: null }, output: { note: null } }, |
| 614 | ], |
| 615 | 'nested-optional-property-widened': [ |
| 616 | { input: { cfg: { url: 'u', note: null } }, output: { cfg: { url: 'u' } } }, |
| 617 | ], |
| 618 | 'nullable-type-array-kept': [{ input: { id: null }, output: { id: null } }], |
| 619 | 'nullable-object-stays-nullable': [ |
| 620 | { input: { cfg: null }, output: { cfg: null } }, |
| 621 | { input: { cfg: { a: 'x' } }, output: { cfg: { a: 'x' } } }, |
| 622 | ], |
| 623 | 'any-of-already-nullable-unchanged': [{ input: { value: null }, output: { value: null } }], |
| 624 | 'optional-enum-only-wrapped': [{ input: { choice: null }, output: {} }], |
| 625 | 'array-items-object-normalized': [ |
| 626 | { input: { rows: [{ id: '1', tag: null }, null] }, output: { rows: [{ id: '1' }, null] } }, |
| 627 | ], |
| 628 | 'enum-values-are-data-not-keywords': [{ input: { opts: null }, output: { opts: null } }], |
| 629 | 'property-names-are-not-keywords': [ |
| 630 | { |
| 631 | input: { default: null, $ref: 'x', type: null, properties: { x: null } }, |
| 632 | output: { $ref: 'x', properties: {} }, |
| 633 | }, |
| 634 | ], |
| 635 | 'defs-ref-kept-and-definition-normalized': [ |
| 636 | { input: { cfg: { url: 'u', note: null } }, output: { cfg: { url: 'u' } } }, |
| 637 | ], |
| 638 | 'optional-ref-widened-with-null-branch': [{ input: { cfg: null }, output: {} }], |
| 639 | 'recursive-defs-kept': [ |
| 640 | { |
| 641 | input: { node: { label: 'a', child: { label: 'b', child: null } } }, |
| 642 | output: { node: { label: 'a', child: { label: 'b' } } }, |
| 643 | }, |
| 644 | ], |
| 645 | 'no-required-array-widens-every-property': [ |
| 646 | { input: { a: null, b: 1, unknown: null }, output: { b: 1, unknown: null } }, |
| 647 | ], |
| 648 | 'optional-null-only-property-unchanged': [ |
| 649 | { input: { nothing: null }, output: { nothing: null } }, |
| 650 | ], |
| 651 | 'optional-enum-with-null-not-rewrapped': [{ input: { choice: null }, output: { choice: null } }], |
| 652 | 'optional-const-null-not-rewrapped': [{ input: { nothing: null }, output: { nothing: null } }], |
| 653 | 'array-of-nullable-objects-closed': [ |
| 654 | { input: { rows: [null, { id: null }] }, output: { rows: [null, {}] } }, |
| 655 | ], |
| 656 | 'nested-any-of-branch-widened': [ |
| 657 | { input: { v: { a: null } }, output: { v: {} } }, |
| 658 | { input: { v: null }, output: {} }, |
| 659 | ], |
| 660 | 'nested-any-of-in-any-of-kept': [{ input: { v: { a: null } }, output: { v: {} } }], |
| 661 | 'chained-refs-resolved': [ |
| 662 | { input: { a: { x: null } }, output: { a: {} } }, |
| 663 | { input: { a: null }, output: {} }, |
| 664 | ], |
| 665 | 'nullable-array-kept': [ |
| 666 | { input: { xs: null }, output: { xs: null } }, |
| 667 | { input: { xs: [null] }, output: { xs: [null] } }, |
| 668 | ], |
| 669 | 'nested-optional-property-widened': [ |
| 670 | { input: { cfg: { url: 'u', note: null } }, output: { cfg: { url: 'u' } } }, |
| 671 | { input: { cfg: null }, output: {} }, |
| 672 | { input: { cfg: { url: 'u', extra: null } }, output: { cfg: { url: 'u', extra: null } } }, |
| 673 | ], |
| 674 | }; |
| 675 | const out = { cases: [] }; |
| 676 | for (const [id, description, schema] of cases) { |
| 677 | const snapshot = JSON.stringify(schema); |
| 678 | const r = toStrictJsonSchema(schema); |
| 679 | if (JSON.stringify(schema) !== snapshot) throw new Error(`mutated: ${id}`); |
| 680 | const entry = { id, description, schema }; |
| 681 | if (r.unsupported.length) |
| 682 | entry.strict = { unsupported: r.unsupported.map(u => ({ path: u.path, keyword: u.keyword })) }; |
| 683 | else { |
| 684 | const again = toStrictJsonSchema(r.schema); |
| 685 | if (JSON.stringify(again.schema) !== JSON.stringify(r.schema) || again.changes.length) |
| 686 | throw new Error(`not idempotent: ${id}`); |
| 687 | entry.strict = { schema: r.schema }; |
| 688 | } |
| 689 | if (r.changes.length) entry.changes = r.changes.map(c => ({ path: c.path, reason: c.reason })); |
| 690 | if (argumentCases[id]) { |
| 691 | entry.arguments = argumentCases[id].map(({ input, output }) => { |
| 692 | const actual = omitNullToolArguments(input, r.source); |
| 693 | if (JSON.stringify(actual) !== JSON.stringify(output)) |
| 694 | throw new Error( |
| 695 | `arguments mismatch ${id}: ${JSON.stringify(actual)} vs ${JSON.stringify(output)}` |
| 696 | ); |
| 697 | return { input, output }; |
| 698 | }); |
| 699 | } |
| 700 | out.cases.push(entry); |
| 701 | } |
| 702 | const json = JSON.stringify(out, null, 2) + '\n'; |
| 703 | fs.writeFileSync(path.join(here, 'strict-cases.json'), json); |
| 704 | fs.writeFileSync( |
| 705 | path.join(repoRoot, 'python/tests/fixtures/json-schema-conversion/strict-cases.json'), |
| 706 | json |
| 707 | ); |
| 708 | for (const c of out.cases) |
| 709 | console.log( |
| 710 | c.id.padEnd(50), |
| 711 | c.strict.unsupported |
| 712 | ? 'UNSUPPORTED ' + JSON.stringify(c.strict.unsupported) |
| 713 | : 'ok' + (c.changes ? ' changes=' + c.changes.map(x => x.reason.split('-')[0]).join(',') : '') |
| 714 | ); |