Skip to content

Commit 938b45d

Browse files
Updated reciever to handle null params slightly differently, and changed how Request Routers router requests
1 parent 1158fd8 commit 938b45d

File tree

6 files changed

+32
-38
lines changed

6 files changed

+32
-38
lines changed

src/JsonRpc/Reciever.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected virtual Renor GetRenor(JToken @object)
8888
// that we don't fall over and throw an error.
8989
if (@params?.Type == JTokenType.Null)
9090
{
91-
@params = null;
91+
@params = new JObject();
9292
}
9393

9494
// id == request

src/JsonRpc/ReflectionRequestHandlers.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Reflection;
1+
using System.Reflection;
22
using System.Threading;
33
using System.Threading.Tasks;
44

@@ -22,14 +22,6 @@ public static Task HandleNotification(IHandlerDescriptor instance, object @param
2222
return (Task)method.Invoke(instance.Handler, new[] { @params });
2323
}
2424

25-
public static Task HandleRequest(IHandlerDescriptor instance, CancellationToken token)
26-
{
27-
var method = instance.HandlerType.GetTypeInfo()
28-
.GetMethod(nameof(IRequestHandler<object>.Handle), BindingFlags.Public | BindingFlags.Instance);
29-
30-
return (Task)method.Invoke(instance.Handler, new object[] { token });
31-
}
32-
3325
public static Task HandleRequest(IHandlerDescriptor instance, object @params, CancellationToken token)
3426
{
3527
var method = instance.HandlerType.GetTypeInfo()
@@ -38,4 +30,4 @@ public static Task HandleRequest(IHandlerDescriptor instance, object @params, Ca
3830
return (Task)method.Invoke(instance.Handler, new[] { @params, token });
3931
}
4032
}
41-
}
33+
}

src/JsonRpc/RequestRouter.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ protected virtual async Task<ErrorResponse> RouteRequest(IHandlerDescriptor hand
5656
return new MethodNotFound(request.Id, request.Method);
5757
}
5858

59-
Task result;
60-
if (handler.Params is null)
61-
{
62-
result = ReflectionRequestHandlers.HandleRequest(handler, token);
63-
}
64-
else
65-
{
6659
object @params;
6760
try
6861
{
@@ -73,8 +66,7 @@ protected virtual async Task<ErrorResponse> RouteRequest(IHandlerDescriptor hand
7366
return new InvalidParams(request.Id);
7467
}
7568

76-
result = ReflectionRequestHandlers.HandleRequest(handler, @params, token);
77-
}
69+
var result = ReflectionRequestHandlers.HandleRequest(handler, @params, token);
7870

7971
await result.ConfigureAwait(false);
8072

src/Server/LspRequestRouter.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,18 @@ public async Task<ErrorResponse> RouteRequest(IHandlerDescriptor descriptor, Req
111111
return new MethodNotFound(request.Id, request.Method);
112112
}
113113

114-
Task result;
115-
if (descriptor.Params is null)
114+
object @params;
115+
try
116116
{
117-
result = ReflectionRequestHandlers.HandleRequest(descriptor, cts.Token);
117+
@params = request.Params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
118118
}
119-
else
119+
catch
120120
{
121-
object @params;
122-
try
123-
{
124-
@params = request.Params.ToObject(descriptor.Params, _serializer.JsonSerializer);
125-
}
126-
catch
127-
{
128-
return new InvalidParams(request.Id);
129-
}
130-
131-
result = ReflectionRequestHandlers.HandleRequest(descriptor, @params, cts.Token);
121+
return new InvalidParams(request.Id);
132122
}
133123

134-
await result.ConfigureAwait(false);
124+
var result = ReflectionRequestHandlers.HandleRequest(descriptor, @params, cts.Token).ConfigureAwait(false);
125+
await result;
135126

136127
object responseValue = null;
137128
if (result.GetType().GetTypeInfo().IsGenericType)

test/JsonRpc.Tests/Server/SpecifictionRecieverTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public override IEnumerable<ValueTuple<string, Renor[]>> GetValues()
7171
@"{""jsonrpc"": ""2.0"", ""method"": ""subtract"", ""params"": null, ""id"": 4}",
7272
new Renor[]
7373
{
74-
new Request(4, "subtract", null)
74+
new Request(4, "subtract", new JObject())
7575
});
7676

7777
yield return (
@@ -96,7 +96,7 @@ public override IEnumerable<ValueTuple<string, Renor[]>> GetValues()
9696
@"{""jsonrpc"": ""2.0"", ""method"": ""foobar"", ""params"": null}",
9797
new Renor[]
9898
{
99-
new Notification("foobar", null)
99+
new Notification("foobar", new JObject())
100100
});
101101

102102
yield return (

test/Lsp.Tests/LspRequestRouterTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,24 @@ public async Task ShouldRouteToCorrect_Request_WithManyHandlers()
144144
await codeActionHandler.Received(1).Handle(Arg.Any<CodeActionParams>(), Arg.Any<CancellationToken>());
145145
await codeActionHandler2.Received(0).Handle(Arg.Any<CodeActionParams>(), Arg.Any<CancellationToken>());
146146
}
147+
148+
[Fact]
149+
public async Task ShouldRouteTo_CorrectRequestWhenGivenNullParams()
150+
{
151+
var handler = Substitute.For<IShutdownHandler>();
152+
handler
153+
.Handle(Arg.Any<object>(), Arg.Any<CancellationToken>())
154+
.Returns(Task.CompletedTask);
155+
156+
var collection = new HandlerCollection { handler };
157+
var mediator = new LspRequestRouter(collection, _testLoggerFactory, _handlerMatcherCollection, new Serializer());
158+
159+
var id = Guid.NewGuid().ToString();
160+
var request = new Request(id, GeneralNames.Shutdown, new JObject());
161+
162+
await mediator.RouteRequest(mediator.GetDescriptor(request), request);
163+
164+
await handler.Received(1).Handle(Arg.Any<object>(), Arg.Any<CancellationToken>());
165+
}
147166
}
148167
}

0 commit comments

Comments
 (0)