Skip to content

Commit

Permalink
Merge pull request #60 from tintoy/fix/server/invalid-params
Browse files Browse the repository at this point in the history
Enable handling of requests with no parameters.
  • Loading branch information
david-driscoll authored Jan 12, 2018
2 parents 82ce908 + b29d214 commit 0fef034
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Server/LspRequestRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public async Task<ErrorResponse> RouteRequest(IHandlerDescriptor descriptor, Req
{
@params = request.Params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
}
catch
catch (Exception cannotDeserializeRequestParams)
{
_logger.LogError(new EventId(-32602), cannotDeserializeRequestParams, "Failed to deserialise request parameters.");

return new InvalidParams(request.Id);
}

Expand Down
49 changes: 49 additions & 0 deletions test/Lsp.Tests/LspRequestRouterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;
using OmniSharp.Extensions.LanguageServer.Server;
using OmniSharp.Extensions.LanguageServer.Server.Abstractions;
using OmniSharp.Extensions.LanguageServer.Server.Handlers;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
Expand Down Expand Up @@ -163,5 +164,53 @@ public async Task ShouldRouteTo_CorrectRequestWhenGivenNullParams()

await handler.Received(1).Handle(Arg.Any<object>(), Arg.Any<CancellationToken>());
}

[Fact]
public async Task ShouldHandle_Request_WithNullParameters()
{
bool wasShutDown = false;

ShutdownHandler shutdownHandler = new ShutdownHandler();
shutdownHandler.Shutdown += shutdownRequested =>
{
wasShutDown = true;
};

var collection = new HandlerCollection { shutdownHandler };
var mediator = new LspRequestRouter(collection, _testLoggerFactory, _handlerMatcherCollection, new Serializer());

JToken @params = JValue.CreateNull(); // If the "params" property present but null, this will be JTokenType.Null.

var id = Guid.NewGuid().ToString();
var request = new Request(id, GeneralNames.Shutdown, @params);

await mediator.RouteRequest(mediator.GetDescriptor(request), request);

Assert.True(wasShutDown, "WasShutDown");
}

[Fact]
public async Task ShouldHandle_Request_WithMissingParameters()
{
bool wasShutDown = false;

ShutdownHandler shutdownHandler = new ShutdownHandler();
shutdownHandler.Shutdown += shutdownRequested =>
{
wasShutDown = true;
};

var collection = new HandlerCollection { shutdownHandler };
var mediator = new LspRequestRouter(collection, _testLoggerFactory, _handlerMatcherCollection, new Serializer());

JToken @params = null; // If the "params" property was missing entirely, this will be null.

var id = Guid.NewGuid().ToString();
var request = new Request(id, GeneralNames.Shutdown, @params);

await mediator.RouteRequest(mediator.GetDescriptor(request), request);

Assert.True(wasShutDown, "WasShutDown");
}
}
}

0 comments on commit 0fef034

Please sign in to comment.