π The fast, Pythonic way to build MCP servers and clients.
fix(json-schema): apply a Draft-4 boolean exclusive bound to its sibling (#5234)
kbkb62818aa0861351f
diff --git a/fastmcp_slim/fastmcp/utilities/json_schema_type.py b/fastmcp_slim/fastmcp/utilities/json_schema_type.pyindex 725bb8a74..b1bb8e393 100644--- a/fastmcp_slim/fastmcp/utilities/json_schema_type.py+++ b/fastmcp_slim/fastmcp/utilities/json_schema_type.py@@ -503,6 +503,52 @@ def _constrain_raw_string(]+def _exclusive_bound(schema: Mapping[str, Any], modifier: str, bound: str) -> Any:+ """Value for a strict bound, or None when the schema states no strict bound.++ ``exclusiveMinimum``/``exclusiveMaximum`` take a number since draft-06. Draft-04+ instead writes a boolean that tightens the numeric sibling: ``{"minimum": 1,+ "exclusiveMinimum": true}`` excludes 1. Passing that boolean straight through as+ Pydantic's ``gt``/``lt`` reads it as the number 1, which rejects every value+ except 0 regardless of the sibling.+ """+ value = schema.get(modifier)+ if isinstance(value, bool):+ return schema.get(bound) if value else None+ return value+++def _inclusive_bound(+ schema: Mapping[str, Any], modifier: str, bound: str, *, lower: bool+) -> Any:+ """Value for a non-strict bound, or None when a strict bound replaces the sibling.++ A boolean ``true`` (draft-04) always replaces the sibling: ``{"minimum": 1,+ "exclusiveMinimum": true}`` means exactly ``minimum > 1``.++ A numeric ``exclusiveMinimum``/``exclusiveMaximum`` (draft-06+) is instead a+ separate keyword, and an instance must satisfy both. The sibling is redundant β+ and can be dropped β only when the exclusive bound is at least as strict, which+ compares ``exclusive >= minimum`` for a lower bound and ``exclusive <= maximum``+ for an upper one. A *looser* numeric exclusive must keep the sibling, otherwise+ the generated ``lt``/``gt`` widens the accepted range: ``{"maximum": 10,+ "exclusiveMaximum": 15}`` would accept 12.+ """+ value = schema.get(bound)+ exclusive = schema.get(modifier)+ if exclusive is True:+ return None+ if (+ isinstance(exclusive, (int, float))+ and not isinstance(exclusive, bool)+ and isinstance(value, (int, float))+ and not isinstance(value, bool)+ and (exclusive >= value if lower else exclusive <= value)+ ):+ return None+ return value++def _create_numeric_type(base: type[int | float], schema: Mapping[str, Any]) -> type | Annotated[Any, ...]:@@ -513,10 +559,10 @@ def _create_numeric_type(constraints = {k: vfor k, v in {- "gt": schema.get("exclusiveMinimum"),- "ge": schema.get("minimum"),- "lt": schema.get("exclusiveMaximum"),- "le": schema.get("maximum"),+ "gt": _exclusive_bound(schema, "exclusiveMinimum", "minimum"),+ "ge": _inclusive_bound(schema, "exclusiveMinimum", "minimum", lower=True),+ "lt": _exclusive_bound(schema, "exclusiveMaximum", "maximum"),+ "le": _inclusive_bound(schema, "exclusiveMaximum", "maximum", lower=False),"multiple_of": schema.get("multipleOf"),}.items()if v is not Nonediff --git a/tests/utilities/json_schema_type/test_constraints.py b/tests/utilities/json_schema_type/test_constraints.pyindex 56e20cf73..27ca539c7 100644--- a/tests/utilities/json_schema_type/test_constraints.py+++ b/tests/utilities/json_schema_type/test_constraints.py@@ -133,6 +133,106 @@ class TestNumberConstraints:validator.validate_python(100)+class TestDraftFourExclusiveBounds:+ """A boolean exclusiveMinimum/exclusiveMaximum tightens its numeric sibling."""++ def test_boolean_exclusive_maximum_excludes_sibling(self):+ validator = TypeAdapter(+ json_schema_to_type(+ {+ "type": "integer",+ "minimum": 1,+ "maximum": 5,+ "exclusiveMaximum": True,+ }+ )+ )+ assert validator.validate_python(3) == 3+ with pytest.raises(ValidationError):+ validator.validate_python(5)++ def test_boolean_exclusive_minimum_excludes_sibling(self):+ validator = TypeAdapter(+ json_schema_to_type(+ {+ "type": "integer",+ "minimum": 5,+ "maximum": 9,+ "exclusiveMinimum": True,+ }+ )+ )+ assert validator.validate_python(6) == 6+ with pytest.raises(ValidationError):+ validator.validate_python(5)++ def test_boolean_false_keeps_sibling_inclusive(self):+ validator = TypeAdapter(+ json_schema_to_type(+ {"type": "integer", "maximum": 5, "exclusiveMaximum": False}+ )+ )+ assert validator.validate_python(5) == 5+ with pytest.raises(ValidationError):+ validator.validate_python(6)++ def test_boolean_exclusive_bound_without_sibling_constrains_nothing(self):+ validator = TypeAdapter(+ json_schema_to_type({"type": "integer", "exclusiveMaximum": True})+ )+ assert validator.validate_python(1000) == 1000+++def _bound_kinds(schema: dict) -> set[str]:+ """Names of the pydantic constraints a numeric schema converts to."""+ kinds: set[str] = set()+ for item in getattr(json_schema_to_type(schema), "__metadata__", ()):+ for constraint in getattr(item, "metadata", (item,)):+ kinds.add(type(constraint).__name__)+ return kinds+++class TestNumericExclusiveWithSibling:+ """A numeric exclusive bound replaces its sibling only when it is as strict."""++ def test_stricter_numeric_exclusive_drops_maximum(self):+ schema = {"type": "integer", "maximum": 10, "exclusiveMaximum": 5}+ assert _bound_kinds(schema) == {"Lt"}+ validator = TypeAdapter(json_schema_to_type(schema))+ assert validator.validate_python(4) == 4+ with pytest.raises(ValidationError):+ validator.validate_python(5)+ with pytest.raises(ValidationError):+ validator.validate_python(6)++ def test_stricter_numeric_exclusive_drops_minimum(self):+ schema = {"type": "integer", "minimum": 10, "exclusiveMinimum": 15}+ assert _bound_kinds(schema) == {"Gt"}+ validator = TypeAdapter(json_schema_to_type(schema))+ assert validator.validate_python(16) == 16+ with pytest.raises(ValidationError):+ validator.validate_python(15)+ with pytest.raises(ValidationError):+ validator.validate_python(14)++ def test_looser_numeric_exclusive_keeps_maximum(self):+ # `maximum: 10` still forbids 12, so dropping `le` would widen the range.+ schema = {"type": "integer", "maximum": 10, "exclusiveMaximum": 15}+ assert _bound_kinds(schema) == {"Lt", "Le"}+ validator = TypeAdapter(json_schema_to_type(schema))+ assert validator.validate_python(10) == 10+ with pytest.raises(ValidationError):+ validator.validate_python(12)++ def test_looser_numeric_exclusive_keeps_minimum(self):+ schema = {"type": "integer", "minimum": 10, "exclusiveMinimum": 5}+ assert _bound_kinds(schema) == {"Gt", "Ge"}+ validator = TypeAdapter(json_schema_to_type(schema))+ assert validator.validate_python(10) == 10+ with pytest.raises(ValidationError):+ validator.validate_python(7)++class TestStringFormatConstraints:"""String keywords apply to the raw string whatever its `format` (#4404)."""