Skip to content

Problem with TMVCWebRequest.BodyAsListOf <T> #9

@GoogleCodeExporter

Description

@GoogleCodeExporter
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions