Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more liberal with the accepted type for a message id #291

Merged
merged 3 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/StreamJsonRpc.Tests/JsonRpcClient20InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
Expand All @@ -22,7 +23,14 @@ public class JsonRpcClient20InteropTests : InteropTestBase
public JsonRpcClient20InteropTests(ITestOutputHelper logger)
: base(logger, serverTest: false)
{
this.clientRpc = new JsonRpc(this.messageHandler);
this.clientRpc = new JsonRpc(this.messageHandler)
{
TraceSource =
{
Switch = { Level = SourceLevels.Verbose },
Listeners = { new XunitTraceListener(logger) },
},
};
this.clientRpc.StartListening();
}

Expand Down Expand Up @@ -324,4 +332,24 @@ public async Task ErrorResponseOmitsDataObject()
});
await Assert.ThrowsAsync<RemoteInvocationException>(() => requestTask);
}

/// <summary>
/// Some lesser JSON-RPC servers may convert the request ID from the JSON number that we sent to a string.
/// Reproduce that to verify that our client functionality is resilient enough to withstand that bad behavior.
/// </summary>
[Fact]
public async Task ServerReturnsOurRequestIdAsString()
{
var invokeTask = this.clientRpc.InvokeWithCancellationAsync<string>("test", cancellationToken: this.TimeoutToken);
dynamic request = await this.ReceiveAsync();
this.Send(new
{
jsonrpc = "2.0",
id = request.id.ToString(), // deliberately return the request id as a string instead of an integer.
result = "pass",
});

string result = await invokeTask;
Assert.Equal("pass", result);
}
}
2 changes: 1 addition & 1 deletion src/StreamJsonRpc/JsonRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,7 @@ private async Task HandleRpcAsync(JsonRpcMessage rpc)
OutstandingCallData data = null;
lock (this.dispatcherMapLock)
{
long id = (long)resultOrError.Id;
long id = Convert.ToInt64(resultOrError.Id);
if (this.resultDispatcherMap.TryGetValue(id, out data))
{
this.resultDispatcherMap.Remove(id);
Expand Down