Skip to content

Commit

Permalink
Merge pull request #275 from AArnott/fix272
Browse files Browse the repository at this point in the history
Allow non-array lists of arguments
  • Loading branch information
AArnott authored May 13, 2019
2 parents c9b6e8f + de6868f commit 6b2bcb9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 12 deletions.
13 changes: 11 additions & 2 deletions src/StreamJsonRpc.Tests/JsonRpcTests.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.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -80,7 +81,7 @@ await helperHandler.WriteAsync(
new JsonRpcRequest
{
Method = nameof(Server.NotificationMethod),
ArgumentsArray = new[] { "hello" },
ArgumentsList = new[] { "hello" },
},
this.TimeoutToken);

Expand All @@ -98,7 +99,7 @@ await helperHandler.WriteAsync(
{
Id = 1,
Method = nameof(Server.MethodThatAccceptsAndReturnsNull),
ArgumentsArray = new object[] { null },
ArgumentsList = new object[] { null },
},
this.TimeoutToken);

Expand Down Expand Up @@ -616,6 +617,14 @@ public async Task InvokeWithCancellationAsync_CanCallCancellableMethodWithNoArgs
}
}

[Fact]
public async Task InvokeAsync_PassArgsAsNonArrayList()
{
var args = new List<object> { 1, 2 };
int result = await this.clientRpc.InvokeWithCancellationAsync<int>(nameof(Server.MethodWithDefaultParameter), args, this.TimeoutToken);
Assert.Equal(3, result);
}

[Fact]
public async Task CancelMessageSentWhileAwaitingResponse()
{
Expand Down
2 changes: 1 addition & 1 deletion src/StreamJsonRpc.Tests/MessagePackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public JsonRpcMessage Deserialize(ReadOnlySequence<byte> contentBuffer)
/// <inheritdoc/>
public void Serialize(IBufferWriter<byte> contentBuffer, JsonRpcMessage message)
{
if (message is JsonRpcRequest request && request.Arguments != null && request.ArgumentsArray == null && !(request.Arguments is IReadOnlyDictionary<string, object>))
if (message is JsonRpcRequest request && request.Arguments != null && request.ArgumentsList == null && !(request.Arguments is IReadOnlyDictionary<string, object>))
{
// This request contains named arguments, but not using a standard dictionary. Convert it to a dictionary so that
// the parameters can be matched to the method we're invoking.
Expand Down
8 changes: 4 additions & 4 deletions src/StreamJsonRpc.Tests/MessagePackFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public void JsonRpcRequest_ArgsArray()
{
Id = 5,
Method = "test",
ArgumentsArray = new object[] { 5, "hi", new CustomType { Age = 8 } },
ArgumentsList = new object[] { 5, "hi", new CustomType { Age = 8 } },
};

var actual = this.Roundtrip(original);
Assert.Equal(original.Id, actual.Id);
Assert.Equal(original.Method, actual.Method);
Assert.Equal(original.ArgumentsArray[0], actual.ArgumentsArray[0]);
Assert.Equal(original.ArgumentsArray[1], actual.ArgumentsArray[1]);
Assert.Equal(((CustomType)original.ArgumentsArray[2]).Age, ((CustomType)actual.ArgumentsArray[2]).Age);
Assert.Equal(original.ArgumentsList[0], actual.ArgumentsList[0]);
Assert.Equal(original.ArgumentsList[1], actual.ArgumentsList[1]);
Assert.Equal(((CustomType)original.ArgumentsList[2]).Age, ((CustomType)actual.ArgumentsList[2]).Age);
}

[Fact]
Expand Down
77 changes: 77 additions & 0 deletions src/StreamJsonRpc.Tests/Protocol/JsonRpcRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using StreamJsonRpc.Protocol;
using Xunit;

public class JsonRpcRequestTests
{
private static readonly IReadOnlyList<object> ArgumentsAsList = new List<object> { 4, 6, 8 };
private static readonly IReadOnlyList<object> ArgumentsAsArray = new object[] { 4, 6, 8 };
private static readonly object ArgumentsAsObject = new Dictionary<string, object> { { "Foo", 4 }, { "bar", 6 } };

[Fact]
public void ArgumentsCount_WithNamedArguments()
{
var request = new JsonRpcRequest
{
Arguments = ArgumentsAsObject,
};
Assert.Equal(2, request.ArgumentCount);
}

[Fact]
public void ArgumentsCount_WithList()
{
var request = new JsonRpcRequest
{
Arguments = ArgumentsAsList,
};
Assert.Equal(3, request.ArgumentCount);
}

[Fact]
public void ArgumentsCount_WithArray()
{
var request = new JsonRpcRequest
{
Arguments = ArgumentsAsArray,
};
Assert.Equal(3, request.ArgumentCount);
}

[Fact]
public void ArgumentsList()
{
var request = new JsonRpcRequest
{
Arguments = ArgumentsAsList,
};
Assert.Same(request.Arguments, request.ArgumentsList);
Assert.Same(ArgumentsAsList, request.ArgumentsList);
}

#pragma warning disable CS0618 // Type or member is obsolete
[Fact]
public void ArgumentsArray_WithArray()
{
var request = new JsonRpcRequest
{
Arguments = ArgumentsAsArray,
};
Assert.Same(request.Arguments, request.ArgumentsArray);
Assert.Same(ArgumentsAsArray, request.ArgumentsArray);
}

[Fact]
public void ArgumentsArray_WithList()
{
var request = new JsonRpcRequest
{
Arguments = ArgumentsAsList,
};
Assert.Null(request.ArgumentsArray);
}
#pragma warning restore CS0618 // Type or member is obsolete
}
4 changes: 2 additions & 2 deletions src/StreamJsonRpc/JsonMessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ private void TokenizeUserData(JsonRpcMessage jsonRpcMessage)
{
if (jsonRpcMessage is Protocol.JsonRpcRequest request)
{
if (request.ArgumentsArray != null)
if (request.ArgumentsList != null)
{
request.ArgumentsArray = request.ArgumentsArray.Select(this.TokenizeUserData).ToArray();
request.ArgumentsList = request.ArgumentsList.Select(this.TokenizeUserData).ToArray();
}
else if (request.Arguments != null)
{
Expand Down
17 changes: 14 additions & 3 deletions src/StreamJsonRpc/Protocol/JsonRpcRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public enum ArgumentMatchResult
/// Gets the number of arguments supplied in the request.
/// </summary>
[IgnoreDataMember]
public int ArgumentCount => this.NamedArguments?.Count ?? this.ArgumentsArray?.Length ?? 0;
public int ArgumentCount => this.NamedArguments?.Count ?? this.ArgumentsList?.Count ?? 0;

/// <summary>
/// Gets or sets the dictionary of named arguments, if applicable.
Expand All @@ -102,12 +102,23 @@ public IReadOnlyDictionary<string, object> NamedArguments
/// Gets or sets an array of arguments, if applicable.
/// </summary>
[IgnoreDataMember]
[Obsolete("Use " + nameof(ArgumentsList) + " instead.")]
public object[] ArgumentsArray
{
get => this.Arguments as object[];
set => this.Arguments = value;
}

/// <summary>
/// Gets or sets a read only list of arguments, if applicable.
/// </summary>
[IgnoreDataMember]
public IReadOnlyList<object> ArgumentsList
{
get => this.Arguments as IReadOnlyList<object>;
set => this.Arguments = value;
}

/// <summary>
/// Gets the string to display in the debugger for this instance.
/// </summary>
Expand Down Expand Up @@ -189,9 +200,9 @@ public virtual bool TryGetArgumentByNameOrIndex(string name, int position, Type
{
return this.NamedArguments.TryGetValue(name, out value);
}
else if (this.ArgumentsArray != null && position < this.ArgumentsArray.Length && position >= 0)
else if (this.ArgumentsList != null && position < this.ArgumentsList.Count && position >= 0)
{
value = this.ArgumentsArray[position];
value = this.ArgumentsList[position];
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/StreamJsonRpc/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
StreamJsonRpc.Protocol.JsonRpcRequest.ArgumentsList.get -> System.Collections.Generic.IReadOnlyList<object>
StreamJsonRpc.Protocol.JsonRpcRequest.ArgumentsList.set -> void

0 comments on commit 6b2bcb9

Please sign in to comment.