Optional parameters
MODIFY
WalletWasabi.Gui/Rpc/JsonRpcMethodMetadata.cs
WalletWasabi.Gui/Rpc/JsonRpcMethodMetadata.cs
@@ -9,7 +9,7 @@ namespace WalletWasabi.Gui.Rpc
/// </summary>
public class JsonRpcMethodMetadata
{
- public JsonRpcMethodMetadata(string name, MethodInfo mi, List<(string name, Type type)> parameters)
+ public JsonRpcMethodMetadata(string name, MethodInfo mi, List<(string name, Type type, bool isOptional, object defaultValue)> parameters)
{
Name = name;
MethodInfo = mi;
@@ -20,6 +20,6 @@ namespace WalletWasabi.Gui.Rpc
public string Name { get; }
public MethodInfo MethodInfo { get; }
- public List<(string name, Type type)> Parameters { get; }
+ public List<(string name, Type type, bool isOptional, object defaultValue)> Parameters { get; }
}
}

MODIFY
WalletWasabi.Gui/Rpc/JsonRpcRequestHandler.cs
WalletWasabi.Gui/Rpc/JsonRpcRequestHandler.cs
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@@ -95,11 +96,14 @@ namespace WalletWasabi.Gui.Rpc
parameters.Insert(position, cancellationToken);
}
}
- if (parameters.Count != methodParameters.Count)
+ if (parameters.Count < methodParameters.Count( x => !x.isOptional ))
{
return Error(JsonRpcErrorCodes.InvalidParams,
$"{methodParameters.Count} parameters were expected but {parameters.Count} were received.", jsonRpcRequest.Id);
}
+
+ var missingParameters = methodParameters.Count() - parameters.Count();
+ parameters.AddRange( methodParameters.TakeLast(missingParameters).Select(x => x.defaultValue) );
var result = prodecureMetadata.MethodInfo.Invoke(Service, parameters.ToArray());
if (jsonRpcRequest.IsNotification) // the client is not interested in getting a response

MODIFY
WalletWasabi.Gui/Rpc/JsonRpcService.cs
WalletWasabi.Gui/Rpc/JsonRpcService.cs
@@ -63,10 +63,10 @@ namespace WalletWasabi.Gui.Rpc
{
if (attr is JsonRpcMethodAttribute)
{
- var parameters = new List<(string name, Type type)>();
+ var parameters = new List<(string name, Type type, bool isOptional, object defaultValue)>();
foreach (var p in methodInfo.GetParameters())
{
- parameters.Add((p.Name, p.ParameterType));
+ parameters.Add((p.Name, p.ParameterType, p.IsOptional, p.DefaultValue));
}
var jsonRpcMethodAttr = (JsonRpcMethodAttribute)attr;
yield return new JsonRpcMethodMetadata(jsonRpcMethodAttr.Name, methodInfo, parameters);

Optional parameters