* JSON-RPC 2.0 allows params to be optional

This commit is contained in:
Michael Van Canneyt 2023-03-31 20:33:32 +02:00
parent f2696ef1c8
commit 3d08253192
2 changed files with 37 additions and 13 deletions

View File

@ -759,6 +759,11 @@ begin
end;
procedure TCustomJSONRPCHandler.DoCheckParams(const Params: TJSONData);
Var
I,ReqdCount : Integer;
def: TJSONParamDef;
begin
if (Params is TJSONObject) then
begin
@ -773,7 +778,19 @@ begin
JSONRPCParamError(SErrParamsMustBeArray);
DoCheckParamArray(Params as TJSONArray);
end;
end
else
begin
if (Params=Nil) then
begin
ReqdCount:=0;
for TCollectionItem(def) in ParamDefs do
if Def.Required then
Inc(ReqdCount);
if ReqdCount>0 then
JSONRPCParamError(SErrParamsRequiredParams,[ReqdCount])
end;
end;
end;
procedure TCustomJSONRPCHandler.DoCheckParamDefsOnObject(
@ -1318,21 +1335,27 @@ begin
end;
// Get params, if they exist
I:=O.IndexOfName(ParamsProperty);
If (I<>-1) then
D:=O.Items[i]
else
Exit(CreateJSON2Error(SErrNoParams,[ParamsProperty],EJSONRPCInvalidParams,ID,transactionproperty));
if OJ2 then
If (I=-1) then
begin
// Allow array or object
If Not (D.JSONType in [jtArray,jtObject]) then
Exit(CreateJSON2Error(SErrParamsMustBeArrayorObject,EJSONRPCInvalidParams,ID,transactionproperty));
D:=Nil;
if Not OJ2 then
Exit(CreateJSON2Error(SErrNoParams,[ParamsProperty],EJSONRPCInvalidParams,ID,transactionproperty));
end
else if not (jdoJSONRPC2 in Options) then
else
begin
// Allow only array
If Not (D.JSONType in [jtArray]) then
Exit(CreateJSON2Error(SErrParamsMustBeArray,EJSONRPCInvalidParams,ID,transactionproperty));
D:=O.Items[i];
if OJ2 then
begin
// Allow array or object
If Not (D.JSONType in [jtArray,jtObject]) then
Exit(CreateJSON2Error(SErrParamsMustBeArrayorObject,EJSONRPCInvalidParams,ID,transactionproperty));
end
else if not (jdoJSONRPC2 in Options) then
begin
// Allow only array
If Not (D.JSONType in [jtArray]) then
Exit(CreateJSON2Error(SErrParamsMustBeArray,EJSONRPCInvalidParams,ID,transactionproperty));
end;
end;
Params:=D;
end;

View File

@ -18,6 +18,7 @@ Resourcestring
SErrParamsMustBeObject = 'Parameters must be passed in an object.';
SErrParamsMustBeArray = 'Parameters must be passed in an array.';
SErrParamsRequiredParamNotFound = 'Required parameter "%s" not found.';
SErrParamsRequiredParams = '%d parameter(s) required, but no parameters found in request.';
SErrParamsDataTypeMismatch = 'Expected parameter "%s" having type "%s", got "%s".';
SErrParamsNotAllowd = 'Parameter "%s" is not allowed.';
SErrParamsOnlyObjectsInArray = 'Array elements must be objects, got %s at position %d.';