JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.77k stars 1.07k forks source link

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

Open miklcct opened 2 months ago

miklcct commented 2 months ago

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()
JoshClose commented 1 month ago

What are DropOffPickupType and RouteType?

miklcct commented 1 month ago

What are DropOffPickupType and RouteType?

They are enums.