mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-20 12:34:17 +02:00
Ensure default fields are not marked nullable (#1224)
This commit is contained in:
parent
1da1eb581a
commit
72fb8d4ce9
2 changed files with 56 additions and 3 deletions
|
|
@ -561,9 +561,7 @@ def _create_dataclass(
|
|||
else:
|
||||
field_def = field(default=None, metadata=meta)
|
||||
|
||||
if is_required and default_val is not MISSING:
|
||||
fields.append((field_name, field_type, field_def))
|
||||
elif is_required:
|
||||
if is_required or default_val is not MISSING:
|
||||
fields.append((field_name, field_type, field_def))
|
||||
else:
|
||||
fields.append((field_name, Union[field_type, type(None)], field_def)) # type: ignore[misc] # noqa: UP007
|
||||
|
|
|
|||
|
|
@ -1523,3 +1523,58 @@ class TestAdditionalProperties:
|
|||
# Should be the same cached class
|
||||
assert Type1 is Type2
|
||||
assert issubclass(Type1, BaseModel)
|
||||
|
||||
|
||||
class TestFieldsWithDefaults:
|
||||
"""Test suite for fields with default values not being made nullable."""
|
||||
|
||||
def test_field_with_default_preserves_type(self):
|
||||
"""Test that fields with defaults preserve their original type."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {"flag": {"type": "boolean", "default": False}},
|
||||
}
|
||||
|
||||
generated_type = json_schema_to_type(schema)
|
||||
regenerated_schema = TypeAdapter(generated_type).json_schema()
|
||||
|
||||
assert regenerated_schema["properties"]["flag"]["type"] == "boolean"
|
||||
|
||||
def test_field_with_default_not_nullable(self):
|
||||
"""Test that fields with defaults are not made nullable."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {"flag": {"type": "boolean", "default": False}},
|
||||
}
|
||||
|
||||
generated_type = json_schema_to_type(schema)
|
||||
regenerated_schema = TypeAdapter(generated_type).json_schema()
|
||||
|
||||
flag_prop = regenerated_schema["properties"]["flag"]
|
||||
assert "anyOf" not in flag_prop
|
||||
|
||||
def test_field_with_default_uses_default(self):
|
||||
"""Test that fields with defaults use their default values."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {"flag": {"type": "boolean", "default": False}},
|
||||
}
|
||||
|
||||
generated_type = json_schema_to_type(schema)
|
||||
validator = TypeAdapter(generated_type)
|
||||
result = validator.validate_python({})
|
||||
|
||||
assert result.flag is False
|
||||
|
||||
def test_field_with_default_accepts_explicit_value(self):
|
||||
"""Test that fields with defaults accept explicit values."""
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {"flag": {"type": "boolean", "default": False}},
|
||||
}
|
||||
|
||||
generated_type = json_schema_to_type(schema)
|
||||
validator = TypeAdapter(generated_type)
|
||||
result = validator.validate_python({"flag": True})
|
||||
|
||||
assert result.flag is True
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue