CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Incorrect number of constructor arguments on a record type despite a default value is specified for missing arguments.

Open miklcct opened this issue 1 year ago • 2 comments

Describe the bug The class cannot load CSVs with missing columns into record types, despite the record type having a default argument for optional columns.

To Reproduce

public record Route(
    string route_id, string agency_id, RouteType route_type,
    string? route_short_name = null, string? route_long_name = null, string? route_desc = null,
    string? route_url = null, string? route_color = null, string? route_text_color = null,
    DropOffPickupType? continuous_pickup = null, DropOffPickupType? continuous_drop_off = null
);
route_id,agency_id,route_short_name,route_long_name,route_type,route_text_color,route_color,route_url,route_desc
1,=GW,GWR,Great Western Railway,2,,0a493e,,

Expected behaviour A record should be loaded, with the default values used for missing columns.

Actual behaviour An exception is thrown.

 ---> System.ArgumentException: Incorrect number of arguments for constructor
   at System.Dynamic.Utils.ExpressionUtils.ValidateArgumentCount(MethodBase method, ExpressionType nodeKind, Int32 count, ParameterInfo[] pis)
   at System.Dynamic.Utils.ExpressionUtils.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments, String methodParamName)
   at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments)
   at CsvHelper.Expressions.ObjectRecordCreator.CreateCreateRecordDelegate(Type recordType)
   at CsvHelper.Expressions.RecordCreator.GetCreateRecordDelegate[T](Type recordType)
   at CsvHelper.Expressions.RecordManager.GetReadDelegate[T](Type recordType)
   at CsvHelper.CsvReader.GetRecordsAsync[T](CancellationToken cancellationToken)+MoveNext()

miklcct avatar Sep 11 '24 14:09 miklcct

What are DropOffPickupType and RouteType?

JoshClose avatar Sep 20 '24 20:09 JoshClose

What are DropOffPickupType and RouteType?

They are enums.

miklcct avatar Sep 20 '24 20:09 miklcct