#1 2015-04-01 12:25:52

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

MVC - Problem with Number-Detection in Edit Fields

Hi Arnaud,

we discovered a little Problem with the interpretation of Contents of Edit Fields.
If we enter e.g. 123-456 and post this value (You can enter the Value in MVCServer - Search Field for example)
only the 123 is used.


Rad Studio 12.1 Santorini

Offline

#2 2015-04-01 14:02:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: MVC - Problem with Number-Detection in Edit Fields

URL encoding issue?

Offline

#3 2015-04-02 08:27:16

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

Re: MVC - Problem with Number-Detection in Edit Fields

I don't think so but to answer exact i have to take a deeper look.
As stated above you can easily test it by yourself using your mvcserver sample


Rad Studio 12.1 Santorini

Offline

#4 2015-04-03 09:21:28

itSDS
Member
From: Germany
Registered: 2014-04-24
Posts: 506

Re: MVC - Problem with Number-Detection in Edit Fields

Hi Arnaud,
I debugged the following Test-Case

1. Start MVCServer.exe
2. http://localhost:8092
3. Search for: 123-456

In THttpServerGeneric.Request Ctxt.fInContent = 'match=123-456'  --> The Parameter goes to mORMot
In TSQLRestServer.URI, call.InBody = 'match=123-456' -> everything fine
In TMVCRunOnRestServer.InternalRunOnRestServer Line 1621 Ctxt.InputAsTDocVariant returns 123 the reason is in
GetVariantFromJSON('123-456', false, ..) which assumes '123-456' is a number returning 123. -> here is the Error

I modified the GetVariantFromJSON to work:

procedure GetVariantFromJSON(JSON: PUTF8Char; wasString: Boolean; var Value: variant;
  TryCustomVariants: PDocVariantOptions);
var Dot: PUTF8Char;
    err: integer;
    bFirstChar : boolean;
begin
  // first handle any strict-JSON syntax objects or arrays into custom variants
  // (e.g. when called directly from TSQLPropInfoRTTIVariant.SetValue)
  if (TryCustomVariants<>nil) and (JSON<>nil) and (JSON^ in ['{','[']) then begin
    GetJSONToAnyVariant(Value,JSON,nil,TryCustomVariants);
    exit;
  end;
  // handle simple text or numerical values
  with TVarData(Value) do begin
    if VType in VTYPE_STATIC then
      VType := varEmpty else
      VarClear(Value);
    if (JSON=nil) or ((PInteger(JSON)^=NULL_LOW) and not wasString) then begin
      VType := varNull;
      exit;
    end else
    if (PInteger(JSON)^=FALSE_LOW) and (JSON[4]='e') and
       (JSON[5] in EndOfJSONValueField) then begin
      VType := varBoolean;
      VBoolean := false;
      exit;
    end else
    if (PInteger(JSON)^=TRUE_LOW) and (JSON[4] in EndOfJSONValueField) then begin
      VType := varBoolean;
      VBoolean := true;
      exit;
    end else
    if not wasString then begin // try if not a number
      Dot := JSON;
      bFirstChar := true;
      repeat
        case Dot^ of
/// itSDS: +,- are only allowed at first position, else it is no number
        '+','-':
          if bFirstChar then
            inc(Dot)
          else
            break;
        '0'..'9':
          inc(Dot);
        '.':
          if (Dot[2]<>#0) and (Dot[3]<>#0) and (Dot[4]<>#0) and (Dot[5]<>#0) then
            break else begin // currency ###.1234 number
            VType := varCurrency;
            VInt64 := StrToCurr64(JSON);
            exit;
          end;
        #0: begin // integer number
          VInt64 := GetInt64(JSON);
          if (VInt64<=high(integer)) and (VInt64>=low(integer)) then
            VType := varInteger else
            VType := varInt64;
          exit;
        end;
        else break;
        end;
        bFirstChar := false;
      until false;
      VDouble := GetExtended(JSON,err);
      if err=0 then begin // floating-point number
        VType := varDouble;
        exit;
      end;
    end;
    // found no numerical value -> return a string in the expected format
    VType := varString;
    VAny := nil; // avoid GPF below when assigning a string variable to VAny
    SetString(RawUTF8(VAny),PAnsiChar(JSON),StrLen(JSON));
  end;
end;

Rad Studio 12.1 Santorini

Offline

#5 2015-04-04 08:49:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: MVC - Problem with Number-Detection in Edit Fields

Should be fixed by http://synopse.info/fossil/info/f709fcbaa4

I've also fixed GetVariantFromJSON() currency recognition pattern - see http://synopse.info/fossil/info/e57924aaf6

Thanks a lot for the very valuable feedback, and providing a solution!

Offline

Board footer

Powered by FluxBB