Skip to content

Commit d7fcde2

Browse files
committed
Bring void function result handling in line with the jsonrpc 2.0 spec. Result must be present on success.
1 parent ad1e223 commit d7fcde2

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

AustinHarris.JsonRpcTestN/Test.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void NullableFloatToNullableFloat3()
140140
public void NullableFloatToNullableFloat2()
141141
{
142142
string request = @"{method:'NullableFloatToNullableFloat',params:[null],id:1}";
143-
string expectedResult = "{\"jsonrpc\":\"2.0\",\"id\":1}";
143+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":1}";
144144
var result = JsonRpcProcessor.Process(request);
145145
result.Wait();
146146
Assert.AreEqual(result.Result, expectedResult);
@@ -1437,6 +1437,17 @@ public void TestEmptyBatchResult()
14371437
Assert.IsTrue(string.IsNullOrEmpty(result.Result));
14381438
}
14391439

1440+
1441+
[Test()]
1442+
public void TestNotificationVoidResult()
1443+
{
1444+
var secondRequest = @"{""jsonrpc"":""2.0"",""method"":""Notify"",""params"":[""Hello World!""], ""id"":73}";
1445+
var result = JsonRpcProcessor.Process(secondRequest);
1446+
result.Wait();
1447+
Console.WriteLine(result.Result);
1448+
Assert.IsTrue(result.Result.Contains("result"), "Json Rpc 2.0 Spec - 'result' - This member is REQUIRED on success. A function that returns void should have the result property included even though the value may be null.");
1449+
}
1450+
14401451
[Test()]
14411452
public void TestLeftOutParams()
14421453
{

Json-Rpc/JsonRpcProcessor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ private static string ProcessInternal(string sessionId, string jsonRpc, object j
209209
var idx = 0;
210210
foreach (var resp in batch.Where(x => x.Item2.Id != null || x.Item2.Error != null))
211211
{
212+
if (resp.Item2.Result == null && resp.Item2.Error == null)
213+
{
214+
// Per json rpc 2.0 spec
215+
// result : This member is REQUIRED on success.
216+
// This member MUST NOT exist if there was an error invoking the method.
217+
// Either the result member or error member MUST be included, but both members MUST NOT be included.
218+
resp.Item2.Result = new Newtonsoft.Json.Linq.JValue((Object)null);
219+
}
212220
responses[idx++] = JsonConvert.SerializeObject(resp.Item2);
213221
}
214222

0 commit comments

Comments
 (0)