Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2020-10

issue with json null values in fcl-web sqldbrestdata.pp

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Simone Giacomelli

Dear coders,

I think I found a fix for a problem (?) in the function SetParamFromData below.

The problem is that when one has a json null in D:TJSONData, the procedure will raise an exception and halt the post for the record. See the comments below in the code for further explanation.

This is the link to the trunk:
https://svn.freepascal.org/svn/fpc/trunk/packages/fcl-web/src/restbridge/sqldbrestdata.pp so you can check the original code.

Is this an advisable and acceptable change?

Best regards
Simone Giacomelli

[code=pascal]

procedure TSQLDBRestDBHandler.SetParamFromData(P: TParam; F:
TSQLDBRestField;
  D: TJSONData);
Var
  S : String;
begin
//  if Assigned(D) then    // <-------
//    S:=D.AsString;         // <------- if it's JSON null it will raise an
exception
  if not Assigned(D) or D.IsNull then // <------- added "D.IsNull" because
of JSON null
    P.Clear
  else if Assigned(F) then
    Case F.FieldType of
      rftInteger : P.AsInteger:=D.AsInteger;
      rftLargeInt : P.AsLargeInt:=D.AsInt64;
      rftFloat : P.AsFloat:=D.AsFloat;
      rftDate :
P.AsDateTime:=ScanDateTime(GetString(rpDateFormat),D.AsString);
      rftTime :
P.AsDateTime:=ScanDateTime(GetString(rpTimeFormat),D.AsString);
      rftDateTime :
P.AsDateTime:=ScanDateTime(GetString(rpDateTimeFormat),D.AsString);
      rftString : P.AsString:=D.AsString;
      rftBoolean : P.AsBoolean:=D.AsBoolean;
      rftBlob :
{$IFNDEF VER3_0}
         P.AsBlob:=BytesOf(DecodeStringBase64(D.AsString));
{$ELSE}
         P.AsBlob:=DecodeStringBase64(D.AsString);
{$ENDIF}
    else
      P.AsString:=D.AsString;
    end
  else
    P.AsString:=D.AsString;
end;

[/code]

Simone -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20201006/d7c1f79a/attachment.htm>

Reply by email

Michael Van Canneyt
to Simone Giacomelli: I think I found a fix for a problem (?) in the function SetParamFromData below. The…
Simone Giacomelli wrote 14 lines
Dear coders, I think I found a fix for a problem (?) in the function SetParamFromData below. The problem is that when one has a json null in D:TJSONData, the procedure will raise an exception and halt the post for the record. See the comments below in the code for further explanation. This is the link to the trunk: https://svn.freepascal.org/svn/fpc/trunk/packages/fcl-web/src/restbridge/sqldbrestdata.pp so you can check the original code. Is this an advisable and acceptable change?

Yes, it totally is.
I am surprised to see this popping up, because I distinctly remember I had alread fixed this in a production system. (although not in the exact same manner)

I will check the code again, and adapt it.

Thank you for pointing it out !

Michael.

Show quoted text 51 lines
Best regards Simone Giacomelli [code=pascal] procedure TSQLDBRestDBHandler.SetParamFromData(P: TParam; F: TSQLDBRestField; D: TJSONData); Var S : String; begin // if Assigned(D) then // <------- // S:=D.AsString; // <------- if it's JSON null it will raise an exception if not Assigned(D) or D.IsNull then // <------- added "D.IsNull" because of JSON null P.Clear else if Assigned(F) then Case F.FieldType of rftInteger : P.AsInteger:=D.AsInteger; rftLargeInt : P.AsLargeInt:=D.AsInt64; rftFloat : P.AsFloat:=D.AsFloat; rftDate : P.AsDateTime:=ScanDateTime(GetString(rpDateFormat),D.AsString); rftTime : P.AsDateTime:=ScanDateTime(GetString(rpTimeFormat),D.AsString); rftDateTime : P.AsDateTime:=ScanDateTime(GetString(rpDateTimeFormat),D.AsString); rftString : P.AsString:=D.AsString; rftBoolean : P.AsBoolean:=D.AsBoolean; rftBlob : {$IFNDEF VER3_0} P.AsBlob:=BytesOf(DecodeStringBase64(D.AsString)); {$ELSE} P.AsBlob:=DecodeStringBase64(D.AsString); {$ENDIF} else P.AsString:=D.AsString; end else P.AsString:=D.AsString; end; [/code] -- Simone

Reply by email