|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | | -import { stripPlaceholderArguments } from "./placeholder-args.js"; |
| 2 | +import { |
| 3 | + advertiseNullForOptionalProperties, |
| 4 | + stripPlaceholderArguments, |
| 5 | +} from "./placeholder-args.js"; |
3 | 6 |
|
4 | 7 | const schema = { |
5 | 8 | type: "object", |
@@ -213,3 +216,173 @@ describe("stripPlaceholderArguments with nullMeansUnset", () => { |
213 | 216 | ).toEqual({ extra: null }); |
214 | 217 | }); |
215 | 218 | }); |
| 219 | + |
| 220 | +function propertiesOf( |
| 221 | + inputSchema: Record<string, unknown>, |
| 222 | +): Record<string, unknown> { |
| 223 | + const properties = inputSchema["properties"]; |
| 224 | + if (typeof properties !== "object" || properties === null) |
| 225 | + throw new Error("schema has no properties"); |
| 226 | + return Object.fromEntries(Object.entries(properties)); |
| 227 | +} |
| 228 | + |
| 229 | +describe("advertiseNullForOptionalProperties", () => { |
| 230 | + // Shapes from Linear's list_issues and save_issue. |
| 231 | + const linear = { |
| 232 | + type: "object", |
| 233 | + properties: { |
| 234 | + id: { type: "string" }, |
| 235 | + priority: { type: "number", description: "0=None, 1=Urgent" }, |
| 236 | + customView: { type: "string", minLength: 1 }, |
| 237 | + orderBy: { type: "string", enum: ["createdAt", "updatedAt"] }, |
| 238 | + fields: { type: "array", items: { type: "string" } }, |
| 239 | + assignee: { type: ["string", "null"] }, |
| 240 | + slaType: { |
| 241 | + anyOf: [{ enum: ["all", "onlyBusinessDays"] }, { type: "null" }], |
| 242 | + }, |
| 243 | + label: { oneOf: [{ type: "string" }, { type: "number" }] }, |
| 244 | + filter: { type: "object", properties: { state: { type: "string" } } }, |
| 245 | + scope: { type: "string", const: "all" }, |
| 246 | + anything: { description: "untyped" }, |
| 247 | + }, |
| 248 | + required: ["id"], |
| 249 | + additionalProperties: false, |
| 250 | + }; |
| 251 | + |
| 252 | + test("adds null to optional properties that reject it", () => { |
| 253 | + const out = advertiseNullForOptionalProperties(linear); |
| 254 | + expect(out["properties"]).toEqual({ |
| 255 | + ...linear.properties, |
| 256 | + priority: { type: ["number", "null"], description: "0=None, 1=Urgent" }, |
| 257 | + customView: { type: ["string", "null"], minLength: 1 }, |
| 258 | + orderBy: { |
| 259 | + type: ["string", "null"], |
| 260 | + enum: ["createdAt", "updatedAt", null], |
| 261 | + }, |
| 262 | + fields: { type: ["array", "null"], items: { type: "string" } }, |
| 263 | + label: { |
| 264 | + oneOf: [{ type: "string" }, { type: "number" }, { type: "null" }], |
| 265 | + }, |
| 266 | + filter: { |
| 267 | + type: ["object", "null"], |
| 268 | + properties: { state: { type: "string" } }, |
| 269 | + }, |
| 270 | + }); |
| 271 | + expect(out["required"]).toEqual(["id"]); |
| 272 | + expect(out["additionalProperties"]).toBe(false); |
| 273 | + }); |
| 274 | + |
| 275 | + test("leaves required, already-nullable, untyped, and const properties as they are", () => { |
| 276 | + const before = propertiesOf(linear); |
| 277 | + const after = propertiesOf(advertiseNullForOptionalProperties(linear)); |
| 278 | + for (const key of ["id", "assignee", "slaType", "scope", "anything"]) |
| 279 | + expect(after[key]).toBe(before[key]); |
| 280 | + }); |
| 281 | + |
| 282 | + test("leaves shapes the flat form cannot widen validly as they are", () => { |
| 283 | + const ambiguous = { |
| 284 | + type: "object", |
| 285 | + properties: { |
| 286 | + due: { |
| 287 | + type: "string", |
| 288 | + oneOf: [{ format: "date" }, { format: "time" }], |
| 289 | + }, |
| 290 | + id: { type: "string", allOf: [{ type: "string" }] }, |
| 291 | + code: { type: "string", not: { const: "x" } }, |
| 292 | + ref: { type: "string", $ref: "#/$defs/S" }, |
| 293 | + branchy: { |
| 294 | + type: "string", |
| 295 | + oneOf: [{ type: "string" }, { minLength: 1 }], |
| 296 | + }, |
| 297 | + conditional: { type: "string", if: true, then: { type: "string" } }, |
| 298 | + dynamic: { type: "string", $dynamicRef: "#/$defs/S" }, |
| 299 | + exact: { const: "all" }, |
| 300 | + openAPI: { |
| 301 | + type: "object", |
| 302 | + oneOf: [{ type: "string", nullable: true }, { type: "number" }], |
| 303 | + }, |
| 304 | + strict: { type: "string", nullable: false }, |
| 305 | + }, |
| 306 | + }; |
| 307 | + expect(advertiseNullForOptionalProperties(ambiguous)).toBe(ambiguous); |
| 308 | + }); |
| 309 | + |
| 310 | + test("adds a null branch to anyOf whatever its other branches hold", () => { |
| 311 | + const opaque = { |
| 312 | + type: "object", |
| 313 | + properties: { |
| 314 | + either: { type: "string", anyOf: [{ $ref: "#/$defs/S" }] }, |
| 315 | + nested: { type: "string", anyOf: [{ allOf: [{ type: "string" }] }] }, |
| 316 | + }, |
| 317 | + }; |
| 318 | + expect(propertiesOf(advertiseNullForOptionalProperties(opaque))).toEqual({ |
| 319 | + either: { |
| 320 | + type: ["string", "null"], |
| 321 | + anyOf: [{ $ref: "#/$defs/S" }, { type: "null" }], |
| 322 | + }, |
| 323 | + nested: { |
| 324 | + type: ["string", "null"], |
| 325 | + anyOf: [{ allOf: [{ type: "string" }] }, { type: "null" }], |
| 326 | + }, |
| 327 | + }); |
| 328 | + }); |
| 329 | + |
| 330 | + test("does not repeat a null that a keyword already lists", () => { |
| 331 | + const partial = { |
| 332 | + type: "object", |
| 333 | + properties: { |
| 334 | + state: { type: "string", enum: ["open", null] }, |
| 335 | + view: { |
| 336 | + type: "string", |
| 337 | + anyOf: [{ type: "string", minLength: 1 }, { type: "null" }], |
| 338 | + }, |
| 339 | + }, |
| 340 | + }; |
| 341 | + expect(propertiesOf(advertiseNullForOptionalProperties(partial))).toEqual({ |
| 342 | + state: { type: ["string", "null"], enum: ["open", null] }, |
| 343 | + view: { |
| 344 | + type: ["string", "null"], |
| 345 | + anyOf: [{ type: "string", minLength: 1 }, { type: "null" }], |
| 346 | + }, |
| 347 | + }); |
| 348 | + }); |
| 349 | + |
| 350 | + test("leaves the input schema intact", () => { |
| 351 | + const before = structuredClone(linear); |
| 352 | + advertiseNullForOptionalProperties(linear); |
| 353 | + expect(linear).toEqual(before); |
| 354 | + }); |
| 355 | + |
| 356 | + test("returns the same object when no property changes", () => { |
| 357 | + const nullable = { |
| 358 | + type: "object", |
| 359 | + properties: { assignee: { type: ["string", "null"] } }, |
| 360 | + }; |
| 361 | + expect(advertiseNullForOptionalProperties(nullable)).toBe(nullable); |
| 362 | + const empty = { type: "object", properties: {} }; |
| 363 | + expect(advertiseNullForOptionalProperties(empty)).toBe(empty); |
| 364 | + const bare = { type: "object" }; |
| 365 | + expect(advertiseNullForOptionalProperties(bare)).toBe(bare); |
| 366 | + const unusable = { type: "object", required: "id" }; |
| 367 | + expect(advertiseNullForOptionalProperties(unusable)).toBe(unusable); |
| 368 | + }); |
| 369 | + |
| 370 | + test("adds null only where the strip rule drops it again", () => { |
| 371 | + const before = propertiesOf(schema); |
| 372 | + const after = propertiesOf(advertiseNullForOptionalProperties(schema)); |
| 373 | + const added = Object.keys(before).filter( |
| 374 | + (key) => after[key] !== before[key], |
| 375 | + ); |
| 376 | + const dropped = Object.keys(before).filter( |
| 377 | + (key) => |
| 378 | + !(key in stripPlaceholderArguments({ [key]: null }, schema, keepNull)), |
| 379 | + ); |
| 380 | + expect(added).not.toEqual([]); |
| 381 | + // A const cannot be widened to null, so its null is dropped unadvertised. |
| 382 | + expect(dropped).toEqual( |
| 383 | + Object.keys(before).filter( |
| 384 | + (key) => added.includes(key) || key === "scope", |
| 385 | + ), |
| 386 | + ); |
| 387 | + }); |
| 388 | +}); |
0 commit comments