Skip to content

Commit 17b6341

Browse files
authored
Merge pull request Astn#73 from Astn/rommar-param_attributes
Thanks @rommar - Possibility to specify a method parameter name as it will be referred to by JsonRpc (with unit test
2 parents 905feaa + 8bc1ee1 commit 17b6341

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

AustinHarris.JsonRpcTestN/Test.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,32 @@ public void TestExtraParameters()
18551855
Assert.IsTrue(result.Result.Contains("\"code\":-32602"));
18561856
}
18571857

1858+
[Test()]
1859+
public void TestCustomParameterName()
1860+
{
1861+
Func<string, string> request = (string paramName) => String.Format("{{method:'TestCustomParameterName',params:{{ {0}:'some string'}},id:1}}", paramName);
1862+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":1}";
1863+
// Check custom param name specified in attribute works
1864+
var result = JsonRpcProcessor.Process(request("myCustomParameter"));
1865+
result.Wait();
1866+
Assert.AreEqual(JObject.Parse(expectedResult), JObject.Parse(result.Result));
1867+
// Check method can't be used with its actual parameter name
1868+
result = JsonRpcProcessor.Process(request("arg"));
1869+
result.Wait();
1870+
StringAssert.Contains("-32602", result.Result); // check for 'invalid params' error code
1871+
}
1872+
1873+
[Test()]
1874+
public void TestCustomParameterWithNoSpecificName()
1875+
{
1876+
Func<string, string> request = (string paramName) => String.Format("{{method:'TestCustomParameterWithNoSpecificName',params:{{ {0}:'some string'}},id:1}}", paramName);
1877+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":1}";
1878+
// Check method can be used with its parameter name
1879+
var result = JsonRpcProcessor.Process(request("arg"));
1880+
result.Wait();
1881+
Assert.AreEqual(JObject.Parse(expectedResult), JObject.Parse(result.Result));
1882+
}
1883+
18581884
[Test]
18591885
public void TestNestedReturnType()
18601886
{

AustinHarris.JsonRpcTestN/service.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ private string devideByZero(string s)
7979
return s + j / i;
8080
}
8181

82+
83+
[JsonRpcMethod]
84+
private bool TestCustomParameterName([JsonRpcParam("myCustomParameter")] string arg)
85+
{
86+
return true;
87+
}
88+
89+
[JsonRpcMethod]
90+
private bool TestCustomParameterWithNoSpecificName([JsonRpcParam] string arg)
91+
{
92+
return true;
93+
}
94+
8295
[JsonRpcMethod]
8396
private string StringToRefException(string s, ref JsonRpcException refException)
8497
{

Json-Rpc/Attributes.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,27 @@ public string JsonMethodName
2424
get { return jsonMethodName; }
2525
}
2626
}
27+
28+
/// <summary>
29+
/// Used to assign JsonRpc parameter name to method argument.
30+
/// </summary>
31+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
32+
public sealed class JsonRpcParamAttribute : Attribute
33+
{
34+
readonly string jsonParamName;
35+
36+
/// <summary>
37+
/// Used to assign JsonRpc parameter name to method argument.
38+
/// </summary>
39+
/// <param name="jsonParamName">Lets you specify the parameter name as it will be referred to by JsonRpc.</param>
40+
public JsonRpcParamAttribute(string jsonParamName = "")
41+
{
42+
this.jsonParamName = jsonParamName;
43+
}
44+
45+
public string JsonParamName
46+
{
47+
get { return jsonParamName; }
48+
}
49+
}
2750
}

Json-Rpc/ServiceBinder.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,26 @@ public static void bindService<T>(string sessionID, Func<T> serviceFactory)
2424
List<Type> parameterTypeArray = new List<Type>();
2525
for (int i = 0; i < paramzs.Length; i++)
2626
{
27+
string paramName;
28+
var paramAttrs = paramzs[i].GetCustomAttributes(typeof(JsonRpcParamAttribute), false);
29+
if (paramAttrs.Length > 0)
30+
{
31+
paramName = ((JsonRpcParamAttribute)paramAttrs[0]).JsonParamName;
32+
if (string.IsNullOrEmpty(paramName))
33+
{
34+
paramName = paramzs[i].Name;
35+
}
36+
}
37+
else
38+
{
39+
paramName = paramzs[i].Name;
40+
}
2741
// reflection attribute information for optional parameters
2842
//http://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection
29-
paras.Add(paramzs[i].Name, paramzs[i].ParameterType);
43+
paras.Add(paramName, paramzs[i].ParameterType);
3044

3145
if (paramzs[i].IsOptional) // if the parameter is an optional, add the default value to our default values dictionary.
32-
defaultValues.Add(paramzs[i].Name, paramzs[i].DefaultValue);
46+
defaultValues.Add(paramName, paramzs[i].DefaultValue);
3347
}
3448

3549
var resType = meth.ReturnType;
@@ -44,6 +58,7 @@ public static void bindService<T>(string sessionID, Func<T> serviceFactory)
4458
handlerSession.MetaData.AddService(methodName, paras, defaultValues, newDel);
4559
}
4660
}
61+
4762
}
4863
public static void bindService<T>(string sessionID) where T : new()
4964
{

0 commit comments

Comments
 (0)