WebApiContrib / WebAPIContrib.Core

Community Contributions for ASP.NET Core
MIT License
461 stars 116 forks source link

Simple CSV with comas can not be read #146

Open antonioortizpola opened 6 years ago

antonioortizpola commented 6 years ago

If i generate a string field with comas, it can be serialized fine:

With this class

public class CsvPackage
{
    public int PackageCode { get; set; }

    public int ActivationCode { get; set; }

    public string PackageType { get; set; }
}

And this data:

new CsvPackage()
{
    ActivationCode = 11,
    PackageCode = 1,
    PackageName = "Comas, breaking, things"
},
new CsvPackage()
{
    ActivationCode = 22,
    PackageCode = 2
}

The write controller gets the right output:

PackageCode,ActivationCode,PackageType 1,11,"Comas, breaking, things" 2,22,

But, inserting the same test in a request gives the error:

{ "": [ "The supplied value is invalid." ] }

If i remove the ",", everything works fine again.

Of course i had to change my config to use commas as separators:

 .AddCsvSerializerFormatters(new CsvFormatterOptions
{
    UseSingleLineHeaderInCsv = true,
    CsvDelimiter = ","
});

On the other hand, it would be great if there could be a way to set a "non-strict mode", where the found properties are mapped, and the ones that does not belong to the class could be just ignored.

This is because we have a set of common fields that are using for our system, but the sources of the csv could contain some more fields, sometimes it does not include all the fields.

Newtonsoft does the right thing filling the properties that finds, leaving the others alone, is there a reason why the behavior is not like this?

damienbod commented 6 years ago

@antonioortizpola Thanks for reporting. I'll see if there is an easy way of doing this.

Greetings Damien