From 3d08253192da985829e5f7fdbb13c58ded3195d7 Mon Sep 17 00:00:00 2001 From: Michael Van Canneyt Date: Fri, 31 Mar 2023 20:33:32 +0200 Subject: [PATCH] * JSON-RPC 2.0 allows params to be optional --- packages/fcl-web/src/jsonrpc/fpjsonrpc.pp | 49 ++++++++++++++------ packages/fcl-web/src/jsonrpc/fprpcstrings.pp | 1 + 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/packages/fcl-web/src/jsonrpc/fpjsonrpc.pp b/packages/fcl-web/src/jsonrpc/fpjsonrpc.pp index 114ef7b3d3..47effe86a5 100644 --- a/packages/fcl-web/src/jsonrpc/fpjsonrpc.pp +++ b/packages/fcl-web/src/jsonrpc/fpjsonrpc.pp @@ -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; diff --git a/packages/fcl-web/src/jsonrpc/fprpcstrings.pp b/packages/fcl-web/src/jsonrpc/fprpcstrings.pp index e0c1c62f8c..f6fec34956 100644 --- a/packages/fcl-web/src/jsonrpc/fprpcstrings.pp +++ b/packages/fcl-web/src/jsonrpc/fprpcstrings.pp @@ -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.';