Spaces:
Running
Running
File size: 15,366 Bytes
112de41 5d9b56c db72e0c 9e0a59f db72e0c 112de41 9e0a59f 112de41 388409a 112de41 9e0a59f 112de41 9e0a59f 112de41 5d9b56c 9e0a59f 5d9b56c cb2e1c5 9e0a59f cb2e1c5 9e0a59f cb2e1c5 9e0a59f cb2e1c5 9e0a59f cb2e1c5 112de41 9e0a59f 112de41 9e0a59f 112de41 db72e0c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | from fastmcp.utilities.json_schema import (
_prune_param,
compress_schema,
)
# Wrapper for backward compatibility with tests
def _prune_additional_properties(schema):
"""Wrapper for compress_schema that only prunes additionalProperties: false."""
return compress_schema(
schema, prune_defs=False, prune_additional_properties=True, prune_titles=False
)
class TestPruneParam:
"""Tests for the _prune_param function."""
def test_nonexistent(self):
"""Test pruning a parameter that doesn't exist."""
schema = {"properties": {"foo": {"type": "string"}}}
result = _prune_param(schema, "bar")
assert result == schema # Schema should be unchanged
def test_exists(self):
"""Test pruning a parameter that exists."""
schema = {"properties": {"foo": {"type": "string"}, "bar": {"type": "integer"}}}
result = _prune_param(schema, "bar")
assert result["properties"] == {"foo": {"type": "string"}}
def test_last_property(self):
"""Test pruning the only/last parameter, should leave empty properties object."""
schema = {"properties": {"foo": {"type": "string"}}}
result = _prune_param(schema, "foo")
assert "properties" in result
assert result["properties"] == {}
def test_from_required(self):
"""Test pruning a parameter that's in the required list."""
schema = {
"properties": {"foo": {"type": "string"}, "bar": {"type": "integer"}},
"required": ["foo", "bar"],
}
result = _prune_param(schema, "bar")
assert result["required"] == ["foo"]
def test_last_required(self):
"""Test pruning the last required parameter, should remove required field."""
schema = {
"properties": {"foo": {"type": "string"}, "bar": {"type": "integer"}},
"required": ["foo"],
}
result = _prune_param(schema, "foo")
assert "required" not in result
class TestPruneUnusedDefs:
"""Tests for unused definition pruning (via compress_schema)."""
def test_removes_unreferenced_defs(self):
"""Test that unreferenced definitions are removed."""
schema = {
"properties": {
"foo": {"$ref": "#/$defs/foo_def"},
},
"$defs": {
"foo_def": {"type": "string"},
"unused_def": {"type": "integer"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "foo_def" in result["$defs"]
assert "unused_def" not in result["$defs"]
def test_nested_references_kept(self):
"""Test that definitions referenced via nesting are kept."""
schema = {
"properties": {
"foo": {"$ref": "#/$defs/foo_def"},
},
"$defs": {
"foo_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/nested_def"}},
},
"nested_def": {"type": "string"},
"unused_def": {"type": "integer"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "foo_def" in result["$defs"]
assert "nested_def" in result["$defs"]
assert "unused_def" not in result["$defs"]
def test_nested_references_removed(self):
"""Test that definitions referenced via nesting in unused defs are removed."""
schema = {
"properties": {},
"$defs": {
"foo_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/nested_def"}},
},
"nested_def": {"type": "string"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "$defs" not in result
def test_nested_references_with_recursion_kept(self):
"""Test that definitions with recursion referenced via nesting are kept."""
schema = {
"properties": {
"foo": {"$ref": "#/$defs/foo_def"},
},
"$defs": {
"foo_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/foo_def"}},
},
"unused_def": {"type": "integer"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "foo_def" in result["$defs"]
assert "unused_def" not in result["$defs"]
def test_nested_references_with_recursion_removed(self):
"""Test that definitions with recursion referenced via nesting in unused defs are removed."""
schema = {
"properties": {},
"$defs": {
"foo_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/foo_def"}},
},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "$defs" not in result
def test_multiple_nested_references_with_recursion_kept(self):
"""Test that definitions with multiple levels of recursion referenced via nesting are kept."""
schema = {
"properties": {
"foo": {"$ref": "#/$defs/foo_def"},
},
"$defs": {
"foo_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/nested_def"}},
},
"nested_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/foo_def"}},
},
"unused_def": {"type": "integer"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "foo_def" in result["$defs"]
assert "nested_def" in result["$defs"]
assert "unused_def" not in result["$defs"]
def test_multiple_nested_references_with_recursion_removed(self):
"""Test that definitions with multiple levels of recursion referenced via nesting in unused defs are removed."""
schema = {
"properties": {},
"$defs": {
"foo_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/nested_def"}},
},
"nested_def": {
"type": "object",
"properties": {"nested": {"$ref": "#/$defs/foo_def"}},
},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "$defs" not in result
def test_array_references_kept(self):
"""Test that definitions referenced in array items are kept."""
schema = {
"properties": {
"items": {"type": "array", "items": {"$ref": "#/$defs/item_def"}},
},
"$defs": {
"item_def": {"type": "string"},
"unused_def": {"type": "integer"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "item_def" in result["$defs"]
assert "unused_def" not in result["$defs"]
def test_removes_defs_field_when_empty(self):
"""Test that $defs field is removed when all definitions are unused."""
schema = {
"properties": {
"foo": {"type": "string"},
},
"$defs": {
"unused_def": {"type": "integer"},
},
}
result = compress_schema(
schema,
prune_defs=True,
prune_additional_properties=False,
prune_titles=False,
)
assert "$defs" not in result
class TestPruneAdditionalProperties:
"""Tests for the _prune_additional_properties function."""
def test_removes_when_false(self):
"""Test that additionalProperties is removed when it's false."""
schema = {
"type": "object",
"properties": {"foo": {"type": "string"}},
"additionalProperties": False,
}
result = _prune_additional_properties(schema)
assert "additionalProperties" not in result
def test_keeps_when_true(self):
"""Test that additionalProperties is kept when it's true."""
schema = {
"type": "object",
"properties": {"foo": {"type": "string"}},
"additionalProperties": True,
}
result = _prune_additional_properties(schema)
assert "additionalProperties" in result
assert result["additionalProperties"] is True
def test_keeps_when_object(self):
"""Test that additionalProperties is kept when it's an object schema."""
schema = {
"type": "object",
"properties": {"foo": {"type": "string"}},
"additionalProperties": {"type": "string"},
}
result = _prune_additional_properties(schema)
assert "additionalProperties" in result
assert result["additionalProperties"] == {"type": "string"}
class TestCompressSchema:
"""Tests for the compress_schema function."""
def test_prune_params(self):
"""Test pruning parameters with compress_schema."""
schema = {
"properties": {
"foo": {"type": "string"},
"bar": {"type": "integer"},
"baz": {"type": "boolean"},
},
"required": ["foo", "bar"],
}
result = compress_schema(schema, prune_params=["foo", "baz"])
assert result["properties"] == {"bar": {"type": "integer"}}
assert result["required"] == ["bar"]
def test_prune_defs(self):
"""Test pruning unused definitions with compress_schema."""
schema = {
"properties": {
"foo": {"$ref": "#/$defs/foo_def"},
"bar": {"type": "integer"},
},
"$defs": {
"foo_def": {"type": "string"},
"unused_def": {"type": "number"},
},
}
result = compress_schema(schema)
assert "foo_def" in result["$defs"]
assert "unused_def" not in result["$defs"]
def test_disable_prune_defs(self):
"""Test disabling pruning of unused definitions."""
schema = {
"properties": {
"foo": {"$ref": "#/$defs/foo_def"},
"bar": {"type": "integer"},
},
"$defs": {
"foo_def": {"type": "string"},
"unused_def": {"type": "number"},
},
}
result = compress_schema(schema, prune_defs=False)
assert "foo_def" in result["$defs"]
assert "unused_def" in result["$defs"]
def test_pruning_additional_properties(self):
"""Test pruning additionalProperties when False."""
schema = {
"type": "object",
"properties": {"foo": {"type": "string"}},
"additionalProperties": False,
}
result = compress_schema(schema)
assert "additionalProperties" not in result
def test_disable_pruning_additional_properties(self):
"""Test disabling pruning of additionalProperties."""
schema = {
"type": "object",
"properties": {"foo": {"type": "string"}},
"additionalProperties": False,
}
result = compress_schema(schema, prune_additional_properties=False)
assert "additionalProperties" in result
assert result["additionalProperties"] is False
def test_combined_operations(self):
"""Test all pruning operations together."""
schema = {
"type": "object",
"properties": {
"keep": {"type": "string"},
"remove": {"$ref": "#/$defs/remove_def"},
},
"required": ["keep", "remove"],
"additionalProperties": False,
"$defs": {
"remove_def": {"type": "string"},
"unused_def": {"type": "number"},
},
}
result = compress_schema(schema, prune_params=["remove"])
# Check that parameter was removed
assert "remove" not in result["properties"]
# Check that required list was updated
assert result["required"] == ["keep"]
# Check that unused definitions were removed
assert "$defs" not in result # Both defs should be gone
# Check that additionalProperties was removed
assert "additionalProperties" not in result
def test_prune_titles(self):
"""Test pruning title fields."""
schema = {
"title": "Root Schema",
"type": "object",
"properties": {
"foo": {"title": "Foo Property", "type": "string"},
"bar": {
"title": "Bar Property",
"type": "object",
"properties": {
"nested": {"title": "Nested Property", "type": "string"}
},
},
},
}
result = compress_schema(schema, prune_titles=True)
assert "title" not in result
assert "title" not in result["properties"]["foo"]
assert "title" not in result["properties"]["bar"]
assert "title" not in result["properties"]["bar"]["properties"]["nested"]
def test_prune_nested_additional_properties(self):
"""Test pruning additionalProperties: false at all levels."""
schema = {
"type": "object",
"additionalProperties": False,
"properties": {
"foo": {
"type": "object",
"additionalProperties": False,
"properties": {
"nested": {
"type": "object",
"additionalProperties": False,
}
},
},
},
}
result = compress_schema(schema)
assert "additionalProperties" not in result
assert "additionalProperties" not in result["properties"]["foo"]
assert (
"additionalProperties"
not in result["properties"]["foo"]["properties"]["nested"]
)
|