GetRelationsOf now causes Range exception if related list has no items

Issue #106 resolved
Todd Flora created an issue

We have code that looks for the relationships of a given attribute in our PODO by using the TRTTIExplorer.GetRelationsOf method. I just pulled the latest code from the Spring Marshmallow branch and now it seems that this method fails when the PODO Attribute has no members in it's list.

A range check error occurs.

class function TRttiExplorer.GetRelationsOf(const entity: TObject;
  const relationAttributeClass: TAttributeClass): IList<TObject>;
var
  rttiType: TRttiType;
  field: TRttiField;
  prop: TRttiProperty;
begin
  Result := TCollections.CreateList<TObject>;

  rttiType := fRttiCache.GetType(entity.ClassType);
  for field in rttiType.GetFields do
    if field.HasCustomAttribute(relationAttributeClass) then
      Result.AddRange(GetSubEntityFromMemberDeep(entity, prop));  <  - - This line fails with a range error when the GetSubEntityFromMemberDeep returns a List with zero members

  for prop in rttiType.GetProperties do
    if prop.HasCustomAttribute(relationAttributeClass) then
      Result.AddRange(GetSubEntityFromMemberDeep(entity, prop));
end;

This seems to be something new as our code using this method was working before the latest pull. Are we doing something wrong now or is this possibly a bug?

Comments (4)

  1. Todd Flora reporter

    By the way. I looked at the prior code and it did the following before calling the AddRange method:

       LEntities := GetSubEntityFromMemberDeep(AEntity, LField);
       if LEntities.Any then
          Result.AddRange(LEntities);
    

    I have added this check back to the method and now it works fine for us.

  2. Log in to comment