Skip to content

Commit ee30083

Browse files
author
Yoshihiro Takahara
committed
fix: remove hash key from EditResult on error
- Add model validator to set hash to None when result is "error" - Improve to_dict method to exclude keys with None values - Update test cases to verify new behavior
1 parent 19882be commit ee30083

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/mcp_text_editor/models.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,21 @@ class EditResult(BaseModel):
6363
None, description="Current content hash (None for missing files)"
6464
)
6565

66+
@model_validator(mode="after")
67+
def validate_error_result(self) -> "EditResult":
68+
"""Remove hash when result is error."""
69+
if self.result == "error":
70+
object.__setattr__(self, "hash", None)
71+
return self
72+
6673
def to_dict(self) -> Dict:
6774
"""Convert EditResult to a dictionary."""
68-
return {
69-
"result": self.result,
70-
"reason": self.reason,
71-
"hash": self.hash,
72-
}
75+
result = {"result": self.result}
76+
if self.reason is not None:
77+
result["reason"] = self.reason
78+
if self.hash is not None:
79+
result["hash"] = self.hash
80+
return result
7381

7482

7583
class EditTextFileContentsRequest(BaseModel):

tests/test_models.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ def test_edit_result():
114114
assert result.result == "ok"
115115
assert result.hash == "newhash123"
116116
assert result.reason is None
117+
result_dict = result.to_dict()
118+
assert result_dict["result"] == "ok"
119+
assert result_dict["hash"] == "newhash123"
120+
assert "reason" not in result_dict
117121

118122
# Test error result with reason
119123
result = EditResult(
@@ -123,7 +127,11 @@ def test_edit_result():
123127
)
124128
assert result.result == "error"
125129
assert result.reason == "hash mismatch"
126-
assert result.hash == "currenthash123"
130+
assert result.hash is None
131+
result_dict = result.to_dict()
132+
assert result_dict["result"] == "error"
133+
assert result_dict["reason"] == "hash mismatch"
134+
assert "hash" not in result_dict
127135

128136
# Test validation error - missing required fields
129137
with pytest.raises(ValidationError):
@@ -177,11 +185,7 @@ def test_edit_result_to_dict():
177185
# Test successful result
178186
result = EditResult(result="ok", hash="newhash123")
179187
result_dict = result.to_dict()
180-
assert result_dict == {
181-
"result": "ok",
182-
"hash": "newhash123",
183-
"reason": None,
184-
}
188+
assert result_dict == {"result": "ok", "hash": "newhash123"}
185189

186190
# Test error result
187191
result = EditResult(
@@ -190,11 +194,7 @@ def test_edit_result_to_dict():
190194
hash="currenthash123",
191195
)
192196
result_dict = result.to_dict()
193-
assert result_dict == {
194-
"result": "error",
195-
"reason": "hash mismatch",
196-
"hash": "currenthash123",
197-
}
197+
assert result_dict == {"result": "error", "reason": "hash mismatch"}
198198

199199

200200
def test_file_range():

0 commit comments

Comments
 (0)