Skip to content

Commit 0fef034

Browse files
Merge pull request #60 from tintoy/fix/server/invalid-params
Enable handling of requests with no parameters.
2 parents 82ce908 + b29d214 commit 0fef034

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Server/LspRequestRouter.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ public async Task<ErrorResponse> RouteRequest(IHandlerDescriptor descriptor, Req
116116
{
117117
@params = request.Params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
118118
}
119-
catch
119+
catch (Exception cannotDeserializeRequestParams)
120120
{
121+
_logger.LogError(new EventId(-32602), cannotDeserializeRequestParams, "Failed to deserialise request parameters.");
122+
121123
return new InvalidParams(request.Id);
122124
}
123125

test/Lsp.Tests/LspRequestRouterTests.cs

+49
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;
1717
using OmniSharp.Extensions.LanguageServer.Server;
1818
using OmniSharp.Extensions.LanguageServer.Server.Abstractions;
19+
using OmniSharp.Extensions.LanguageServer.Server.Handlers;
1920
using Xunit;
2021
using Xunit.Abstractions;
2122
using Xunit.Sdk;
@@ -163,5 +164,53 @@ public async Task ShouldRouteTo_CorrectRequestWhenGivenNullParams()
163164

164165
await handler.Received(1).Handle(Arg.Any<object>(), Arg.Any<CancellationToken>());
165166
}
167+
168+
[Fact]
169+
public async Task ShouldHandle_Request_WithNullParameters()
170+
{
171+
bool wasShutDown = false;
172+
173+
ShutdownHandler shutdownHandler = new ShutdownHandler();
174+
shutdownHandler.Shutdown += shutdownRequested =>
175+
{
176+
wasShutDown = true;
177+
};
178+
179+
var collection = new HandlerCollection { shutdownHandler };
180+
var mediator = new LspRequestRouter(collection, _testLoggerFactory, _handlerMatcherCollection, new Serializer());
181+
182+
JToken @params = JValue.CreateNull(); // If the "params" property present but null, this will be JTokenType.Null.
183+
184+
var id = Guid.NewGuid().ToString();
185+
var request = new Request(id, GeneralNames.Shutdown, @params);
186+
187+
await mediator.RouteRequest(mediator.GetDescriptor(request), request);
188+
189+
Assert.True(wasShutDown, "WasShutDown");
190+
}
191+
192+
[Fact]
193+
public async Task ShouldHandle_Request_WithMissingParameters()
194+
{
195+
bool wasShutDown = false;
196+
197+
ShutdownHandler shutdownHandler = new ShutdownHandler();
198+
shutdownHandler.Shutdown += shutdownRequested =>
199+
{
200+
wasShutDown = true;
201+
};
202+
203+
var collection = new HandlerCollection { shutdownHandler };
204+
var mediator = new LspRequestRouter(collection, _testLoggerFactory, _handlerMatcherCollection, new Serializer());
205+
206+
JToken @params = null; // If the "params" property was missing entirely, this will be null.
207+
208+
var id = Guid.NewGuid().ToString();
209+
var request = new Request(id, GeneralNames.Shutdown, @params);
210+
211+
await mediator.RouteRequest(mediator.GetDescriptor(request), request);
212+
213+
Assert.True(wasShutDown, "WasShutDown");
214+
}
166215
}
167216
}

0 commit comments

Comments
 (0)