Skip to content

Commit b6c3958

Browse files
author
Martin Daetz
committed
fix optional parameter order issue.
1 parent 76fb4fb commit b6c3958

3 files changed

Lines changed: 52 additions & 24 deletions

File tree

AustinHarris.JsonRpcTest/UnitTest1.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,18 @@ public void TestOptionalParametersStrings_BothExists()
13201320
result.Wait();
13211321
Assert.IsFalse(result.Result.Contains("error"));
13221322
Assert.AreEqual(expectedResult, result.Result);
1323-
}
1323+
}
1324+
[TestMethod]
1325+
public void TestOptionalParametersBoolsAndStrings()
1326+
{
1327+
string request =
1328+
"{\"jsonrpc\":\"2.0\",\"method\":\"TestOptionalParametersBoolsAndStrings\",\"params\":{\"input1\":\"murkel\"},\"Id\":1}";
1329+
string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":true,\"id\":1}";
1330+
1331+
var result = JsonRpcProcessor.Process(request);
1332+
result.Wait();
1333+
Assert.IsFalse(result.Result.Contains("error"));
1334+
Assert.AreEqual(expectedResult, result.Result);
1335+
}
13241336
}
13251337
}

AustinHarris.JsonRpcTest/service.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,12 @@ private IList<string> TestOptionalParameters_Strings(string input1 = null, strin
291291
input1,
292292
input2
293293
};
294-
}
294+
}
295+
[JsonRpcMethod]
296+
public bool TestOptionalParametersBoolsAndStrings(string input1, bool input2 = true, string input3 = "")
297+
{
298+
return input2;
299+
}
295300

296301
}
297302
}

Json-Rpc/Handler.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,28 +266,39 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null)
266266
}
267267
parameters[i] = CleanUpParameter(jo[metadata.parameters[i].Name], metadata.parameters[i]);
268268
}
269-
}
270-
271-
// Optional Parameter support
272-
// check if we still miss parameters compared to metadata which may include optional parameters.
273-
// if the rpc-call didn't supply a value for an optional parameter, we should be assinging the default value of it.
274-
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.
275-
{
276-
var paramIndex = parameters.Length; // the index we should start storing default values of optional parameters.
277-
var missingParamsCount = metaDataParamCount - parameters.Length; // the amount of optional parameters without a value set by rpc-call.
278-
Array.Resize(ref parameters, parameters.Length + missingParamsCount); // resize the array to include all optional parameters.
279-
280-
// we need to add in reverse order as parameters can appear after all required parameters.
281-
// as some of the optional parameters could already have assigned their values in rpc-call,
282-
// by starting from the end we can make sure we only add the required default values.
283-
for (int k = missingParamsCount; k > 0; k--)
284-
{
285-
var optionalParamIndex = k - 1; // the index of the optional parameter we will be currently setting a default value.
286-
parameters[paramIndex] = metadata.defaultValues[optionalParamIndex].Value; // set the default value for the optional parameter that rpc-call didn't set a value for.
287-
paramIndex++;
288-
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.
289-
}
290-
}
269+
}
270+
271+
// Optional Parameter support
272+
// check if we still miss parameters compared to metadata which may include optional parameters.
273+
// if the rpc-call didn't supply a value for an optional parameter, we should be assinging the default value of it.
274+
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.
275+
{
276+
var suppliedParamsCount = parameters.Length; // the index we should start storing default values of optional parameters.
277+
var missingParamsCount = metaDataParamCount - parameters.Length; // the amount of optional parameters without a value set by rpc-call.
278+
Array.Resize(ref parameters, parameters.Length + missingParamsCount); // resize the array to include all optional parameters.
279+
280+
for (int paramIndex = parameters.Length - 1, defaultIndex = metadata.defaultValues.Length - 1; // fill missing parameters from the back
281+
paramIndex >= suppliedParamsCount && defaultIndex >= 0; // to don't overwrite supplied ones.
282+
paramIndex--, defaultIndex--)
283+
{
284+
parameters[paramIndex] = metadata.defaultValues[defaultIndex].Value;
285+
}
286+
287+
if (missingParamsCount > metadata.defaultValues.Length)
288+
{
289+
return new JsonResponse
290+
{
291+
Error = ProcessException(Rpc,
292+
new JsonRpcException(-32602,
293+
"Invalid params",
294+
string.Format(
295+
"Number of default parameters {0} not sufficient to fill all missing parameters {1}",
296+
metadata.defaultValues.Length, missingParamsCount)
297+
)),
298+
Id = Rpc.Id
299+
};
300+
}
301+
}
291302

292303
if (parameters.Length != metaDataParamCount)
293304
{

0 commit comments

Comments
 (0)