linexjlin / delphimvcframework

Automatically exported from code.google.com/p/delphimvcframework
0 stars 0 forks source link

Problem with TMVCWebRequest.BodyAsListOf <T> #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I created a controller with a POST method that processes a list of Users.

procedure TAppController.PostUsers(ctx: TWebContext);
var
  vUsers: TObjectList<TUser>;
begin
  vUsers := ctx.Request.BodyAsListOf<TUser>();
  vUsers.OwnsObjects := True;

  if (vUsers.Count > 0) then
    Render('Sucess!')
  else
    Render('Error!');

  FreeAndNil(vUsers);
end;

In this condition the destructor method is called TMVCWebRequest.Destroy causes 
an Access Violation error in FreeAndNil(FBodyAsJSONValue).

Changing the TMVCWebRequest.BodyAsListOf <T> method could solve the problem:

function TMVCWebRequest.BodyAsListOf<T>(const RootProperty: string)
  : TObjectList<T>;
var
  S: string;
begin
  if ContentType.Equals(TMVCMimeType.APPLICATION_JSON) then
  begin
    if RootProperty = '' then
      Result := Mapper.JSONArrayToObjectList<T>((BodyAsJSONValue as TJSONArray), False, True) //Ezequiel J. Müller
    else
    begin
      S := Mapper.GetStringDef(BodyAsJSONObject, RootProperty, '');
      if not S.IsEmpty then
        Result := Mapper.JSONArrayToObjectList<T>((BodyAsJSONObject.Get(S).JsonValue as TJSONArray), False, True) //Ezequiel J. Müller
      else
        raise EMVCException.CreateFmt('Body property %s not valid',
          [RootProperty]);
    end;
  end
  else
    raise EMVCException.CreateFmt('Body ContentType %s not supported',
      [ContentType]);
end;

Original issue reported on code.google.com by ezequiel...@gmail.com on 21 Oct 2014 at 11:39

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Thank you for your fix. Has been merged into the feature branch 
"authentication_authorization" and will be released in the main trunk ASAP.

Original comment by daniele....@gmail.com on 26 Mar 2015 at 4:32