CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

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

Open kikaragyozov opened this issue 2 years ago • 2 comments

Is this intentional?

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

kikaragyozov avatar Nov 17 '21 07:11 kikaragyozov

@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" />

anato-s avatar Apr 25 '23 02:04 anato-s

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).

Rob-Hague avatar Apr 27 '23 08:04 Rob-Hague