Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Json-Rpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/obj/
20 changes: 20 additions & 0 deletions Json-Rpc/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
{
parameters[i] = CleanUpParameter(jarr[i], metadata.parameters[i]);
}

// check if we still miss parameters compared to metadata which may include optional parameters.
// if the rpc-call didn't supply a value for an optional parameter, we should be assinging the default value of it.
if (parameters.Length < metaDataParamCount && metadata.defaultValues.Length > 0) // rpc call didn't set values for all optional parameters, so we need to assign the default values for them.
{
var paramIndex = parameters.Length; // the index we should start storing default values of optional parameters.
var missingParamsCount = metaDataParamCount - parameters.Length; // the amount of optional parameters without a value set by rpc-call.
Array.Resize(ref parameters, parameters.Length + metadata.defaultValues.Length); // resize the array to include all optional parameters.

// we need to add in reverse order as parameters can appear after all required parameters.
// as some of the optional parameters could already have assigned their values in rpc-call,
// by starting from the end we can make sure we only add the required default values.
for (int k = missingParamsCount; k > 0; k--)
{
var optionalParamIndex = k - 1; // the index of the optional parameter we will be currently setting a default value.
parameters[paramIndex] = metadata.defaultValues[optionalParamIndex].Value; // set the default value for the optional parameter that rpc-call didn't set a value for.
paramIndex++;
paramCount++; // we need to increase the paramCount by one each time we add default-value for an optional parameter that rpc-call didn't set a value for.
}
}
}
else if (isJObject)
{
Expand Down
11 changes: 8 additions & 3 deletions Json-Rpc/JsonRpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ private void buildService(string sessionID)
foreach (var meth in methods)
{
Dictionary<string, Type> paras = new Dictionary<string, Type>();
Dictionary<string, object> defaultValues = new Dictionary<string, object>(); // dictionary that holds default values for optional params.

var paramzs = meth.GetParameters();

List<Type> parameterTypeArray = new List<Type>();
for (int i = 0; i < paramzs.Length; i++)
List<Type> parameterTypeArray = new List<Type>();
for (int i = 0; i < paramzs.Length; i++)
{
// 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);

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);
}

var resType = meth.ReturnType;
Expand All @@ -52,7 +57,7 @@ private void buildService(string sessionID)
var newDel = Delegate.CreateDelegate(System.Linq.Expressions.Expression.GetDelegateType(paras.Values.ToArray()), this /*Need to add support for other methods outside of this instance*/, meth);
var handlerSession = Handler.GetSessionHandler(sessionID);
regMethod.Invoke(handlerSession, new object[] { methodName, newDel });
handlerSession.MetaData.AddService(methodName, paras);
handlerSession.MetaData.AddService(methodName, paras, defaultValues);
}
}
}
Expand Down
41 changes: 38 additions & 3 deletions Json-Rpc/SMDService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public SMD ()
TypeHashes = new List<string>();
}

public void AddService(string method, Dictionary<string,Type> parameters)
public void AddService(string method, Dictionary<string,Type> parameters, Dictionary<string, object> defaultValues)
{
var newService = new SMDService(transport,"JSON-RPC-2.0",parameters);
var newService = new SMDService(transport,"JSON-RPC-2.0",parameters, defaultValues);
Services.Add(method,newService);
}

Expand Down Expand Up @@ -65,7 +65,8 @@ public class SMDService
/// <param name="transport">POST, GET, REST, JSONP, TCP/IP</param>
/// <param name="envelope">URL, PATH, JSON, JSON-RPC-1.0, JSON-RPC-1.1, JSON-RPC-2.0</param>
/// <param name="parameters"></param>
public SMDService(string transport, string envelope, Dictionary<string, Type> parameters)
/// <param name="defaultValues"></param>
public SMDService(string transport, string envelope, Dictionary<string, Type> parameters, Dictionary<string, object> defaultValues )
{
// TODO: Complete member initialization
this.transport = transport;
Expand All @@ -80,6 +81,14 @@ public SMDService(string transport, string envelope, Dictionary<string, Type> pa
}
}

// create the default values storage for optional parameters.
this.defaultValues = new ParameterDefaultValue[defaultValues.Count];
int counter = 0;
foreach (var item in defaultValues)
{
this.defaultValues[counter++] = new ParameterDefaultValue(item.Key, item.Value);
}

// this is getting the return type from the end of the param list
this.returns = new SMDResult(parameters.Values.LastOrDefault());
}
Expand All @@ -95,6 +104,11 @@ public SMDService(string transport, string envelope, Dictionary<string, Type> pa
/// JSON Schema property definition with the additional properties:
/// </summary>
public SMDAdditionalParameters[] parameters { get; private set; }

/// <summary>
/// Stores default values for optional parameters.
/// </summary>
public ParameterDefaultValue[] defaultValues { get; private set; }
}

public class SMDResult
Expand All @@ -108,6 +122,27 @@ public SMDResult(System.Type type)
}
}

/// <summary>
/// Holds default value for parameters.
/// </summary>
public class ParameterDefaultValue
{
/// <summary>
/// Name of the parameter.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Default value for the parameter.
/// </summary>
public object Value { get; private set; }

public ParameterDefaultValue(string name, object value)
{
this.Name = name;
this.Value = value;
}
}

public class SMDAdditionalParameters
{
public SMDAdditionalParameters(string parametername, System.Type type)
Expand Down