Skip to content

CsvInputFormatter throws exception for nullable reference types  #211

@patzila

Description

@patzila

The exception is thrown everytime csv data is received if the model it's trying to parse contains nullable reference types.

I have a model like this

public class Item
{
    public string ItemID { get; set; } = "";
    public string ItemNumber { get; set; } = "";
    public string? Description { get; set; }
    public int Active { get; set; }
    public float? Weight { get; set; }
}

sample CSV to parse

ItemID,ItemNumber,Description,Active,Weight
10002-18985,10002-18985,3M 5952,1,0

The exception thrown

System.InvalidCastException: Invalid cast from 'System.String' to 'System.Nullable`1[[System.Single, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at WebApiContrib.Core.Formatter.Csv.CsvInputFormatter.ReadStreamAsync(Type type, Stream stream)
   at WebApiContrib.Core.Formatter.Csv.CsvInputFormatter.ReadRequestBodyAsync(InputFormatterContext context)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()

Can support for nullable reference types be added?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions