Skip to content

Commit 79d47ae

Browse files
committed
Replace ElicitResult extension methods with JsonIgnore properties
1 parent fa0dc6a commit 79d47ae

File tree

4 files changed

+181
-163
lines changed

4 files changed

+181
-163
lines changed

src/ModelContextProtocol.Core/ExtensionMethods/ElicitResultExtensions.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/ModelContextProtocol.Core/Protocol/ElicitResult.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,25 @@ public sealed class ElicitResult : Result
4444
/// </remarks>
4545
[JsonPropertyName("content")]
4646
public IDictionary<string, JsonElement>? Content { get; set; }
47+
48+
/// <summary>
49+
/// Gets a value indicating whether the user accepted the elicitation request.
50+
/// </summary>
51+
/// <returns><see langword="true"/> if the action is "accept"; otherwise, <see langword="false"/>.</returns>
52+
[JsonIgnore]
53+
public bool IsAccepted => string.Equals(Action, "accept", StringComparison.OrdinalIgnoreCase);
54+
55+
/// <summary>
56+
/// Gets a value indicating whether the user declined the elicitation request.
57+
/// </summary>
58+
/// <returns><see langword="true"/> if the action is "decline"; otherwise, <see langword="false"/>.</returns>
59+
[JsonIgnore]
60+
public bool IsDeclined => string.Equals(Action, "decline", StringComparison.OrdinalIgnoreCase);
61+
62+
/// <summary>
63+
/// Gets a value indicating whether the user cancelled the elicitation request.
64+
/// </summary>
65+
/// <returns><see langword="true"/> if the action is "cancel"; otherwise, <see langword="false"/>.</returns>
66+
[JsonIgnore]
67+
public bool IsCancelled => string.Equals(Action, "cancel", StringComparison.OrdinalIgnoreCase);
4768
}

tests/ModelContextProtocol.Tests/ExtensionMethods/ElicitResultExtensionsTests.cs

Lines changed: 0 additions & 118 deletions
This file was deleted.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System.Text.Json;
2+
using ModelContextProtocol.Protocol;
3+
4+
namespace ModelContextProtocol.Tests.Protocol;
5+
6+
public class ElicitResultTests
7+
{
8+
[Theory]
9+
[InlineData("accept")]
10+
[InlineData("Accept")]
11+
[InlineData("ACCEPT")]
12+
[InlineData("AccEpt")]
13+
public void IsAccepted_Returns_True_For_VariousAcceptedActions(string action)
14+
{
15+
// Arrange
16+
var result = new ElicitResult { Action = action };
17+
18+
// Act
19+
var isAccepted = result.IsAccepted;
20+
21+
// Assert
22+
Assert.True(isAccepted);
23+
}
24+
25+
[Theory]
26+
[InlineData("decline")]
27+
[InlineData("Decline")]
28+
[InlineData("DECLINE")]
29+
[InlineData("DecLine")]
30+
public void IsDeclined_Returns_True_For_VariousDeclinedActions(string action)
31+
{
32+
// Arrange
33+
var result = new ElicitResult { Action = action };
34+
35+
// Act
36+
var isDeclined = result.IsDeclined;
37+
38+
// Assert
39+
Assert.True(isDeclined);
40+
}
41+
42+
[Theory]
43+
[InlineData("cancel")]
44+
[InlineData("Cancel")]
45+
[InlineData("CANCEL")]
46+
[InlineData("CanCel")]
47+
public void IsCancelled_Returns_True_For_VariousCancelledActions(string action)
48+
{
49+
// Arrange
50+
var result = new ElicitResult { Action = action };
51+
52+
// Act
53+
var isCancelled = result.IsCancelled;
54+
55+
// Assert
56+
Assert.True(isCancelled);
57+
}
58+
59+
[Fact]
60+
public void IsAccepted_Returns_False_For_DefaultAction()
61+
{
62+
// Arrange
63+
var result = new ElicitResult();
64+
65+
// Act & Assert
66+
Assert.False(result.IsAccepted);
67+
}
68+
69+
[Fact]
70+
public void IsDeclined_Returns_False_For_DefaultAction()
71+
{
72+
// Arrange
73+
var result = new ElicitResult();
74+
75+
// Act & Assert
76+
Assert.False(result.IsDeclined);
77+
}
78+
79+
[Fact]
80+
public void IsCancelled_Returns_True_For_DefaultAction()
81+
{
82+
// Arrange
83+
var result = new ElicitResult();
84+
85+
// Act & Assert
86+
Assert.True(result.IsCancelled);
87+
}
88+
89+
[Fact]
90+
public void IsAccepted_Returns_False_For_Null_Action()
91+
{
92+
// Arrange
93+
var result = new ElicitResult { Action = null! };
94+
95+
// Act & Assert
96+
Assert.False(result.IsAccepted);
97+
}
98+
99+
[Fact]
100+
public void IsDeclined_Returns_False_For_Null_Action()
101+
{
102+
// Arrange
103+
var result = new ElicitResult { Action = null! };
104+
105+
// Act & Assert
106+
Assert.False(result.IsDeclined);
107+
}
108+
109+
[Fact]
110+
public void IsCancelled_Returns_False_For_Null_Action()
111+
{
112+
// Arrange
113+
var result = new ElicitResult { Action = null! };
114+
115+
// Act & Assert
116+
Assert.False(result.IsCancelled);
117+
}
118+
119+
[Theory]
120+
[InlineData("accept")]
121+
[InlineData("decline")]
122+
[InlineData("cancel")]
123+
[InlineData("unknown")]
124+
public void JsonSerialization_ExcludesJsonIgnoredProperties(string action)
125+
{
126+
// Arrange
127+
var result = new ElicitResult { Action = action };
128+
129+
// Act
130+
var json = JsonSerializer.Serialize(result, McpJsonUtilities.DefaultOptions);
131+
132+
// Assert
133+
Assert.DoesNotContain("IsAccepted", json);
134+
Assert.DoesNotContain("IsDeclined", json);
135+
Assert.DoesNotContain("IsCancelled", json);
136+
Assert.Contains($"\"action\":\"{action}\"", json);
137+
}
138+
139+
[Theory]
140+
[InlineData("accept", true, false, false)]
141+
[InlineData("decline", false, true, false)]
142+
[InlineData("cancel", false, false, true)]
143+
[InlineData("unknown", false, false, false)]
144+
public void JsonRoundTrip_PreservesActionAndComputedProperties(string action, bool isAccepted, bool isDeclined, bool isCancelled)
145+
{
146+
// Arrange
147+
var result = new ElicitResult { Action = action };
148+
149+
// Act
150+
var json = JsonSerializer.Serialize(result, McpJsonUtilities.DefaultOptions);
151+
var deserialized = JsonSerializer.Deserialize<ElicitResult>(json, McpJsonUtilities.DefaultOptions);
152+
153+
// Assert
154+
Assert.NotNull(deserialized);
155+
Assert.Equal(action, deserialized.Action);
156+
Assert.Equal(isAccepted, deserialized.IsAccepted);
157+
Assert.Equal(isDeclined, deserialized.IsDeclined);
158+
Assert.Equal(isCancelled, deserialized.IsCancelled);
159+
}
160+
}

0 commit comments

Comments
 (0)