Skip to content

Commit

Permalink
Misc Fixes (#108)
Browse files Browse the repository at this point in the history
* Fixes #105   Fixing missing long

* #104 Adding NonRpcMethod attribute

* Fixes #107
  • Loading branch information
Ethan Celletti authored Jan 31, 2023
1 parent ba63269 commit f17eba0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/EdjCase.JsonRpc.Router/Defaults/DefaultRpcInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ private object[] ParseParameters(TopLevelRpcParameters? requestParameters, IRead

private bool TryParseParameterList(IReadOnlyList<IRpcParameterInfo> methodParameters, Dictionary<string, RpcParameter> requestParameters, out RpcParameter[]? parameterList)
{
if (methodParameters.Count > requestParameters.Count)
{
parameterList = null;
return false;
}
if (methodParameters.Count > requestParameters.Count)
{
//The method param count can be larger as long as the diff is optional parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ private static bool TryGetNumberInternal(RpcNumber number, Type destinationRawTy
canParse = number.TryGetInteger(out int v);
destinationValue = v;
}
else if (nonNullableType == typeof(long))
{
canParse = number.TryGetLong(out long v);
destinationValue = v;
}
else if (nonNullableType == typeof(float))
{
canParse = number.TryGetFloat(out float v);
Expand Down
20 changes: 18 additions & 2 deletions src/EdjCase.JsonRpc.Router/RpcController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public abstract class RpcController
/// </summary>
/// <param name="obj">Object to return in response</param>
/// <returns>Success result for rpc response</returns>
public virtual RpcMethodSuccessResult Ok(object? obj = null)
#if !NETSTANDARD1_3
[NonRpcMethod]
#endif
protected virtual RpcMethodSuccessResult Ok(object? obj = null)
{
return new RpcMethodSuccessResult(obj);
}
Expand All @@ -27,7 +30,10 @@ public virtual RpcMethodSuccessResult Ok(object? obj = null)
/// <param name="message">(Optional)Error message</param>
/// <param name="data">(Optional)Error data</param>
/// <returns></returns>
public virtual RpcMethodErrorResult Error(int errorCode, string message, object? data = null)
#if !NETSTANDARD1_3
[NonRpcMethod]
#endif
protected virtual RpcMethodErrorResult Error(int errorCode, string message, object? data = null)
{
return new RpcMethodErrorResult(errorCode, message, data);
}
Expand All @@ -36,6 +42,7 @@ public virtual RpcMethodErrorResult Error(int errorCode, string message, object?
#if !NETSTANDARD1_3
/// <summary>
/// Attribute to decorate a derived <see cref="RpcController"/> class
/// Allows setting a custom route name instead of using the controller name
/// </summary>
public class RpcRouteAttribute : Attribute
{
Expand All @@ -53,5 +60,14 @@ public RpcRouteAttribute(string? routeName)
this.RouteName = routeName;
}
}

/// <summary>
/// Attribute to decorate a method from a derived <see cref="RpcController"/> class
/// Allows a method to not be included as a method
/// </summary>
public class NonRpcMethodAttribute : Attribute
{

}
#endif
}
4 changes: 3 additions & 1 deletion src/EdjCase.JsonRpc.Router/RpcEndpointBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ private static IEnumerable<MethodInfo> Extract(Type controllerType)
return controllerType.Assembly.GetTypes()
.Where(t => t == controllerType)
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
.Where(m => m.DeclaringType != typeof(object) && m.DeclaringType != typeof(RpcController));
.Where(m => m.DeclaringType != typeof(object) && m.DeclaringType != typeof(RpcController))
// Skip NonRpcMethod decorated method
.Where(m => m.GetCustomAttribute<NonRpcMethodAttribute>(true) == null);
}

private void Add(RpcPath? path, IRpcMethodInfo methodInfo)
Expand Down
12 changes: 12 additions & 0 deletions test/EdjCase.JsonRpc.Router.Sample/Controllers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public string Method1(string a)
return a;
}
}
public class Test : RpcController
{
public string Method1(string a)
{
return a;
}
[NonRpcMethod]
public string Method2(string a)
{
return a;
}
}

public class NonRpcController
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"profiles": {
"Web": {
"commandName": "Project",
"launchBrowser": false,
"launchBrowser": true,
"launchUrl": "http://localhost:5000/swagger/index.html",
"environmentVariables": {
"ASPNETCORE_URLS": "http://*:5000",
"ASPNETCORE_URLS": "http://localhost:5000",
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Expand Down

0 comments on commit f17eba0

Please sign in to comment.