Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CsvInputFormatter throws exception for nullable reference types #211

Open
patzila opened this issue Sep 10, 2020 · 2 comments
Open

CsvInputFormatter throws exception for nullable reference types #211

patzila opened this issue Sep 10, 2020 · 2 comments

Comments

@patzila
Copy link

patzila commented Sep 10, 2020

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?

@damienbod
Copy link
Member

@patzila yes, this sounds like a good idea. Will see if I can add this, unless you would like to do a PR youself.

Greetings Damien

@patzila
Copy link
Author

patzila commented Sep 11, 2020

If you can take a crack at it first that would be great :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants