|
| 1 | +import { addAdditionalPropertiesFalse } from "../json-schema" |
| 2 | + |
| 3 | +describe("addAdditionalPropertiesFalse", () => { |
| 4 | + it("should add additionalProperties: false to a simple object schema", () => { |
| 5 | + const schema = { |
| 6 | + type: "object", |
| 7 | + properties: { |
| 8 | + name: { type: "string" }, |
| 9 | + }, |
| 10 | + } |
| 11 | + |
| 12 | + const result = addAdditionalPropertiesFalse(schema) |
| 13 | + |
| 14 | + expect(result).toEqual({ |
| 15 | + type: "object", |
| 16 | + properties: { |
| 17 | + name: { type: "string" }, |
| 18 | + }, |
| 19 | + additionalProperties: false, |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + it("should add additionalProperties: false to nested object schemas", () => { |
| 24 | + const schema = { |
| 25 | + type: "object", |
| 26 | + properties: { |
| 27 | + user: { |
| 28 | + type: "object", |
| 29 | + properties: { |
| 30 | + name: { type: "string" }, |
| 31 | + address: { |
| 32 | + type: "object", |
| 33 | + properties: { |
| 34 | + street: { type: "string" }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + const result = addAdditionalPropertiesFalse(schema) |
| 43 | + |
| 44 | + expect(result).toEqual({ |
| 45 | + type: "object", |
| 46 | + properties: { |
| 47 | + user: { |
| 48 | + type: "object", |
| 49 | + properties: { |
| 50 | + name: { type: "string" }, |
| 51 | + address: { |
| 52 | + type: "object", |
| 53 | + properties: { |
| 54 | + street: { type: "string" }, |
| 55 | + }, |
| 56 | + additionalProperties: false, |
| 57 | + }, |
| 58 | + }, |
| 59 | + additionalProperties: false, |
| 60 | + }, |
| 61 | + }, |
| 62 | + additionalProperties: false, |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + it("should add additionalProperties: false to array items that are objects", () => { |
| 67 | + const schema = { |
| 68 | + type: "object", |
| 69 | + properties: { |
| 70 | + entities: { |
| 71 | + type: "array", |
| 72 | + items: { |
| 73 | + type: "object", |
| 74 | + properties: { |
| 75 | + name: { type: "string" }, |
| 76 | + entityType: { type: "string" }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + const result = addAdditionalPropertiesFalse(schema) |
| 84 | + |
| 85 | + expect(result).toEqual({ |
| 86 | + type: "object", |
| 87 | + properties: { |
| 88 | + entities: { |
| 89 | + type: "array", |
| 90 | + items: { |
| 91 | + type: "object", |
| 92 | + properties: { |
| 93 | + name: { type: "string" }, |
| 94 | + entityType: { type: "string" }, |
| 95 | + }, |
| 96 | + additionalProperties: false, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + additionalProperties: false, |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it("should handle tuple-style array items", () => { |
| 105 | + const schema = { |
| 106 | + type: "object", |
| 107 | + properties: { |
| 108 | + tuple: { |
| 109 | + type: "array", |
| 110 | + items: [ |
| 111 | + { type: "object", properties: { a: { type: "string" } } }, |
| 112 | + { type: "object", properties: { b: { type: "number" } } }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + const result = addAdditionalPropertiesFalse(schema) |
| 119 | + |
| 120 | + expect(result).toEqual({ |
| 121 | + type: "object", |
| 122 | + properties: { |
| 123 | + tuple: { |
| 124 | + type: "array", |
| 125 | + items: [ |
| 126 | + { type: "object", properties: { a: { type: "string" } }, additionalProperties: false }, |
| 127 | + { type: "object", properties: { b: { type: "number" } }, additionalProperties: false }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + }, |
| 131 | + additionalProperties: false, |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + it("should preserve existing additionalProperties: false", () => { |
| 136 | + const schema = { |
| 137 | + type: "object", |
| 138 | + properties: { |
| 139 | + name: { type: "string" }, |
| 140 | + }, |
| 141 | + additionalProperties: false, |
| 142 | + } |
| 143 | + |
| 144 | + const result = addAdditionalPropertiesFalse(schema) |
| 145 | + |
| 146 | + expect(result).toEqual({ |
| 147 | + type: "object", |
| 148 | + properties: { |
| 149 | + name: { type: "string" }, |
| 150 | + }, |
| 151 | + additionalProperties: false, |
| 152 | + }) |
| 153 | + }) |
| 154 | + |
| 155 | + it("should handle anyOf schemas", () => { |
| 156 | + const schema = { |
| 157 | + anyOf: [{ type: "object", properties: { a: { type: "string" } } }, { type: "string" }], |
| 158 | + } |
| 159 | + |
| 160 | + const result = addAdditionalPropertiesFalse(schema) |
| 161 | + |
| 162 | + expect(result).toEqual({ |
| 163 | + anyOf: [ |
| 164 | + { type: "object", properties: { a: { type: "string" } }, additionalProperties: false }, |
| 165 | + { type: "string" }, |
| 166 | + ], |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + it("should handle oneOf schemas", () => { |
| 171 | + const schema = { |
| 172 | + oneOf: [{ type: "object", properties: { a: { type: "string" } } }, { type: "number" }], |
| 173 | + } |
| 174 | + |
| 175 | + const result = addAdditionalPropertiesFalse(schema) |
| 176 | + |
| 177 | + expect(result).toEqual({ |
| 178 | + oneOf: [ |
| 179 | + { type: "object", properties: { a: { type: "string" } }, additionalProperties: false }, |
| 180 | + { type: "number" }, |
| 181 | + ], |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + it("should handle allOf schemas", () => { |
| 186 | + const schema = { |
| 187 | + allOf: [ |
| 188 | + { type: "object", properties: { a: { type: "string" } } }, |
| 189 | + { type: "object", properties: { b: { type: "number" } } }, |
| 190 | + ], |
| 191 | + } |
| 192 | + |
| 193 | + const result = addAdditionalPropertiesFalse(schema) |
| 194 | + |
| 195 | + expect(result).toEqual({ |
| 196 | + allOf: [ |
| 197 | + { type: "object", properties: { a: { type: "string" } }, additionalProperties: false }, |
| 198 | + { type: "object", properties: { b: { type: "number" } }, additionalProperties: false }, |
| 199 | + ], |
| 200 | + }) |
| 201 | + }) |
| 202 | + |
| 203 | + it("should not mutate the original schema", () => { |
| 204 | + const schema = { |
| 205 | + type: "object", |
| 206 | + properties: { |
| 207 | + name: { type: "string" }, |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + const original = JSON.parse(JSON.stringify(schema)) |
| 212 | + addAdditionalPropertiesFalse(schema) |
| 213 | + |
| 214 | + expect(schema).toEqual(original) |
| 215 | + }) |
| 216 | + |
| 217 | + it("should return non-object values as-is", () => { |
| 218 | + expect(addAdditionalPropertiesFalse(null as any)).toBeNull() |
| 219 | + expect(addAdditionalPropertiesFalse("string" as any)).toBe("string") |
| 220 | + }) |
| 221 | + |
| 222 | + it("should handle deeply nested complex schemas", () => { |
| 223 | + const schema = { |
| 224 | + type: "object", |
| 225 | + properties: { |
| 226 | + level1: { |
| 227 | + type: "object", |
| 228 | + properties: { |
| 229 | + level2: { |
| 230 | + type: "array", |
| 231 | + items: { |
| 232 | + type: "object", |
| 233 | + properties: { |
| 234 | + level3: { |
| 235 | + type: "object", |
| 236 | + properties: { |
| 237 | + value: { type: "string" }, |
| 238 | + }, |
| 239 | + }, |
| 240 | + }, |
| 241 | + }, |
| 242 | + }, |
| 243 | + }, |
| 244 | + }, |
| 245 | + }, |
| 246 | + } |
| 247 | + |
| 248 | + const result = addAdditionalPropertiesFalse(schema) |
| 249 | + |
| 250 | + expect(result.additionalProperties).toBe(false) |
| 251 | + expect((result.properties as any).level1.additionalProperties).toBe(false) |
| 252 | + expect((result.properties as any).level1.properties.level2.items.additionalProperties).toBe(false) |
| 253 | + expect((result.properties as any).level1.properties.level2.items.properties.level3.additionalProperties).toBe( |
| 254 | + false, |
| 255 | + ) |
| 256 | + }) |
| 257 | + |
| 258 | + it("should handle the real-world MCP memory create_entities schema", () => { |
| 259 | + // This is based on the actual schema that caused the error |
| 260 | + const schema = { |
| 261 | + type: "object", |
| 262 | + properties: { |
| 263 | + entities: { |
| 264 | + type: "array", |
| 265 | + items: { |
| 266 | + type: "object", |
| 267 | + properties: { |
| 268 | + name: { type: "string", description: "The name of the entity" }, |
| 269 | + entityType: { type: "string", description: "The type of the entity" }, |
| 270 | + observations: { |
| 271 | + type: "array", |
| 272 | + items: { type: "string" }, |
| 273 | + description: "An array of observation contents", |
| 274 | + }, |
| 275 | + }, |
| 276 | + required: ["name", "entityType", "observations"], |
| 277 | + }, |
| 278 | + description: "An array of entities to create", |
| 279 | + }, |
| 280 | + }, |
| 281 | + required: ["entities"], |
| 282 | + } |
| 283 | + |
| 284 | + const result = addAdditionalPropertiesFalse(schema) |
| 285 | + |
| 286 | + // Top-level object should have additionalProperties: false |
| 287 | + expect(result.additionalProperties).toBe(false) |
| 288 | + // Items in the entities array should have additionalProperties: false |
| 289 | + expect((result.properties as any).entities.items.additionalProperties).toBe(false) |
| 290 | + }) |
| 291 | +}) |
0 commit comments