JoshClose / CsvHelper

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

BooleanTrueValues ​​and BooleanFalseValues attributes don't work when you have a ClassMap defined as well? #1907

Open kikaragyozov opened 2 years ago

kikaragyozov commented 2 years ago

Is this intentional?

I'll be able to provide a MRE at the weekend.

anato-s commented 1 year ago

@JoshClose I don't want to replicate this bug but definitely attributes don't work at all - throws error when Map(p => p.UseTitle) is added

[CsvHelper.Configuration.Attributes.Index(3)]
[CsvHelper.Configuration.Attributes.BooleanTrueValues("Y", "y")]
[CsvHelper.Configuration.Attributes.BooleanFalseValues("N", "n")]
public bool UseTitle { get; set; }

If the Map(p => p.UseTitle) completely removed - no error but all values are false.

It works only when this configuration is done

Map(p => p.UseTitle)
    .TypeConverterOption.BooleanValues(true, true, "Y", "y")
    .TypeConverterOption.BooleanValues(false, true, "N", "n")
    .Index(3);

Nuget version <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

Rob-Hague commented 1 year ago

Attributes are only used in automatic mapping. They are not used if you are defining your own ClassMap.

Perhaps MemberMap should expose an ApplyAttributes with the logic from:

https://github.com/JoshClose/CsvHelper/blob/7b3ed4d45af8385e732a42eb161b0d129736edb3/src/CsvHelper/Configuration/ClassMap.cs#L617-L631

This would allow a hybrid approach e.g. Map(p => p.UseTitle).ApplyAttributes().

In the meantime you could literally take the above code and make it an extension method public static void ApplyAttributes(this MemberMap memberMap).