Skip to content

Commit 201d765

Browse files
Added check to allow non spec compliant consumers to send us null for params. When a null is given we won't error, instead we will just pretend we never got any params at all.
1 parent cde3283 commit 201d765

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/JsonRpc/Reciever.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,18 @@ protected virtual Renor GetRenor(JToken @object)
7979
}
8080

8181
var hasParams = request.TryGetValue("params", out var @params);
82-
if (hasParams && @params?.Type != JTokenType.Array && @params?.Type != JTokenType.Object)
82+
if (hasParams && @params?.Type != JTokenType.Array && @params?.Type != JTokenType.Object && @params?.Type != JTokenType.Null)
8383
{
8484
return new InvalidRequest(requestId, "Invalid params");
8585
}
8686

87+
// Special case params such that if we get a null value (from a non spec compliant system)
88+
// that we don't fall over and throw an error.
89+
if (@params?.Type == JTokenType.Null)
90+
{
91+
@params = null;
92+
}
93+
8794
// id == request
8895
// !id == notification
8996
if (!hasRequestId)

test/JsonRpc.Tests/Server/SpecifictionRecieverTests.cs

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using FluentAssertions;
@@ -56,6 +56,24 @@ public override IEnumerable<ValueTuple<string, Renor[]>> GetValues()
5656
new Request(4, "subtract", JObject.FromObject(new {minuend = 42, subtrahend = 23}))
5757
});
5858

59+
yield return (
60+
@"{""jsonrpc"": ""2.0"", ""method"": ""subtract"", ""id"": 4}",
61+
new Renor[]
62+
{
63+
new Request(4, "subtract", null)
64+
});
65+
66+
// http://www.jsonrpc.org/specification says:
67+
// If present, parameters for the rpc call MUST be provided as a Structured value.
68+
// Some clients may serialize params as null, instead of omitting it
69+
// We're going to pretend we never got the null in the first place.
70+
yield return (
71+
@"{""jsonrpc"": ""2.0"", ""method"": ""subtract"", ""params"": null, ""id"": 4}",
72+
new Renor[]
73+
{
74+
new Request(4, "subtract", null)
75+
});
76+
5977
yield return (
6078
@"{""jsonrpc"": ""2.0"", ""method"": ""update"", ""params"": [1,2,3,4,5]}",
6179
new Renor[]
@@ -70,6 +88,17 @@ public override IEnumerable<ValueTuple<string, Renor[]>> GetValues()
7088
new Notification("foobar", null)
7189
});
7290

91+
// http://www.jsonrpc.org/specification says:
92+
// If present, parameters for the rpc call MUST be provided as a Structured value.
93+
// Some clients may serialize params as null, instead of omitting it
94+
// We're going to pretend we never got the null in the first place.
95+
yield return (
96+
@"{""jsonrpc"": ""2.0"", ""method"": ""foobar"", ""params"": null}",
97+
new Renor[]
98+
{
99+
new Notification("foobar", null)
100+
});
101+
73102
yield return (
74103
@"{""jsonrpc"": ""2.0"", ""method"": 1, ""params"": ""bar""}",
75104
new Renor[]
@@ -144,4 +173,4 @@ public override IEnumerable<ValueTuple<string, bool>> GetValues()
144173
}
145174
}
146175
}
147-
}
176+
}

0 commit comments

Comments
 (0)