Skip to content
Closed
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
29 changes: 29 additions & 0 deletions AustinHarris.JsonRpcTestN/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,35 @@ public void TestPostProcessOnSession()
Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result));
}

[Test()]
public void TestCustomParameterName()
{
Func<string, string> request = (string paramName) => String.Format("{{method:'TestCustomParameterName',params:{{ {0}:'some string'}},id:1}}", paramName);
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":1}";

// Check custom param name specified in attribute works
var result = JsonRpcProcessor.Process(request("myCustomParameter"));
result.Wait();
Assert.AreEqual(JObject.Parse(expectedResult), JObject.Parse(result.Result));

// Check method can't be used with its actual parameter name
result = JsonRpcProcessor.Process(request("arg"));
result.Wait();
StringAssert.Contains("-32602", result.Result); // check for 'invalid params' error code
}

[Test()]
public void TestCustomParameterWithNoSpecificName()
{
Func<string, string> request = (string paramName) => String.Format("{{method:'TestCustomParameterWithNoSpecificName',params:{{ {0}:'some string'}},id:1}}", paramName);
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":1}";

// Check method can be used with its parameter name
var result = JsonRpcProcessor.Process(request("arg"));
result.Wait();
Assert.AreEqual(JObject.Parse(expectedResult), JObject.Parse(result.Result));
}

private static void AssertJsonAreEqual(string expectedJson, string actualJson)
{
Newtonsoft.Json.Linq.JObject expected = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson);
Expand Down
13 changes: 13 additions & 0 deletions AustinHarris.JsonRpcTestN/service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ private string devideByZero(string s)
return s + j / i;
}

[JsonRpcMethod]
private bool TestCustomParameterName([JsonRpcParam("myCustomParameter")] string arg)
{
return true;
}

[JsonRpcMethod]
private bool TestCustomParameterWithNoSpecificName([JsonRpcParam] string arg)
{
return true;
}


[JsonRpcMethod]
private string StringToRefException(string s, ref JsonRpcException refException)
{
Expand Down
23 changes: 23 additions & 0 deletions Json-Rpc/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,27 @@ public string JsonMethodName
get { return jsonMethodName; }
}
}

/// <summary>
/// Used to assign JsonRpc parameter name to method argument.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
public sealed class JsonRpcParamAttribute : Attribute
{
readonly string jsonParamName;

/// <summary>
/// Used to assign JsonRpc parameter name to method argument.
/// </summary>
/// <param name="jsonParamName">Lets you specify the parameter name as it will be referred to by JsonRpc.</param>
public JsonRpcParamAttribute(string jsonParamName = "")
{
this.jsonParamName = jsonParamName;
}

public string JsonParamName
{
get { return jsonParamName; }
}
}
}
17 changes: 15 additions & 2 deletions Json-Rpc/ServiceBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@ public static void bindService<T>(string sessionID, Func<T> serviceFactory)
List<Type> parameterTypeArray = new List<Type>();
for (int i = 0; i < paramzs.Length; i++)
{
string paramName;
var paramAttrs = paramzs[i].GetCustomAttributes(typeof(JsonRpcParamAttribute), false);
if (paramAttrs.Length > 0)
{
paramName = ((JsonRpcParamAttribute)paramAttrs[0]).JsonParamName;
if (string.IsNullOrEmpty(paramName))
paramName = paramzs[i].Name;
}
else
{
paramName = paramzs[i].Name;
}

// reflection attribute information for optional parameters
//http://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection
paras.Add(paramzs[i].Name, paramzs[i].ParameterType);
paras.Add(paramName, paramzs[i].ParameterType);

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

var resType = meth.ReturnType;
Expand Down