Shadowed properties cause errors during FieldDef initialization

Issue #390 resolved
Zane Duffield created an issue

If a child class publishes a property with the same name as a parent class, TObjectDataSet will attempt to add multiple fields with the same name, which leads to an error.

It feels most appropriate to me for the property from the child class to be used. This can be achieved by only including the first property for a given name when enumerating the properties in InitRttiPropertiesFromItemType (thereby excluding the duplicates from fProperties) or by doing a similar thing inside InitRttiPropertiesFromItemType where the TFieldDefs are added.

Comments (3)

  1. Zane Duffield reporter

    I don’t think it’s fixed in the latest develop branch.

    Here’s a minimal reproducible case for this issue which should work on both master and develop.

    program ShadowedPropertyBug;
    
    uses
      Spring.Data.ObjectDataSet, Spring.Collections;
    
    type
      TFoo = class(TObject)
        private
          FBar: Integer;
        published
          property Bar: Integer read FBar write FBar;
      end;
    
      TBar = class(TFoo)
        published
          property Bar: Integer read FBar write FBar;
      end;
    
    var
      ods: TObjectDataSet;
    begin
      ods := TObjectDataSet.Create(nil);
      ods.datalist := TCollections.CreateObjectList<TBar> as IObjectList;
      // error on open: Duplicate name 'Bar' in TFieldDefs
      ods.open;
    end.
    

    It might be useful to add a note to the ‘Know Issues’ section of the README in the master branch that mentions that the latest bug fixes are only found in the develop branch.
    I didn’t think to use anything but master when starting out with Spring4D.

  2. Log in to comment