CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.08k stars 175 forks source link

Invalid cast from 'System.String' to 'System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'. #198

Closed abazanov closed 5 years ago

abazanov commented 5 years ago

Fails to bind a string to bool. Blows up with the error:

Invalid cast from 'System.String' to 'System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'.

image

abazanov commented 5 years ago

If I add this:

// Convert.ChangeType (below) chokes on Booleans, so doing it here instead.
if (propertyType == typeof(bool) || propertyType == typeof(bool?))
{
    if (bool.TryParse(val.Value[0], out bool result))
    {
        return result;
    }
    return false;
}

Then it works.

jchannon commented 5 years ago

We probably need to expand that out, Form binding wasn't really that well tested tbh. We may need to add TypeDescriptors instead. See here for example: https://www.hanselman.com/blog/TypeConvertersTheresNotEnoughTypeDescripterGetConverterInTheWorld.aspx

abazanov commented 5 years ago

It then goes on to fail on DateTime? and int? 😭

abazanov commented 5 years ago

@jchannon do you think you will get some time to do that soon, or not really?

jchannon commented 5 years ago

Potentially, if you need it sooner, feel free to take a crack at it :)

abazanov commented 5 years ago

It is stopping me from progressing. If you can't do it in the next couple of days then I will have to give it a go. Do you think in the next couple of days is doable?

jchannon commented 5 years ago

Probably not

abazanov commented 5 years ago

Ok, let me have a quick look at the link you posted. Will get back to you. Cheers.

abazanov commented 5 years ago

I am curious though how other people are using it currently ❓ Unless they do the binding themselves. 😕

jchannon commented 5 years ago

Most I imagine are using JSON bodies not forms

On Mon, 8 Jul 2019 at 16:33, Andrei Bazanov notifications@github.com wrote:

I am curious though how other people are using it currently ❓ Unless they do the binding themselves. 😕

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/198?email_source=notifications&email_token=AAAZVJQLT2HESSH6SIABW63P6NM33A5CNFSM4H64L6X2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZNO3AI#issuecomment-509275521, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAZVJSO43CNAVZ4QR5CUIDP6NM33ANCNFSM4H64L6XQ .

abazanov commented 5 years ago

Good point.

abazanov commented 5 years ago

@jchannon I will give this a miss for now. I was using Postman which makes it very easy to fire forms at an API. With JSON, you have to type it all out. My assumption was that it would make no difference. Since it does, I will stop using forms. Thanks for your help.

jchannon commented 5 years ago

I'll keep the issue open so it can be fixed at some point

jchannon commented 5 years ago

For anyone wanting to pick this up the offending code sits in this block:

https://github.com/CarterCommunity/Carter/blob/master/src/ModelBinding/BindExtensions.cs#L48

abazanov commented 5 years ago

If I was not running a bit late on this project, I would have. Maybe I will get some time, but for now, I don't, sorry.