Skip to content

Fix invalid cast on nullable custom type with Linq #2438

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

Merged
merged 3 commits into from
Jul 22, 2020

Conversation

maca88
Copy link
Contributor

@maca88 maca88 commented Jul 20, 2020

This is a quick fix for the regression introduced by #2036, which restores the previous behavior for custom types. A proper fix where the user can control whether a cast is required can be made for the next release.

Fixes #2437

@fredericDelaporte fredericDelaporte changed the base branch from master to 5.3.x July 20, 2020 16:42
@fredericDelaporte fredericDelaporte added this to the 5.3.1 milestone Jul 20, 2020
@hazzik
Copy link
Member

hazzik commented Jul 20, 2020

I have a different idea how to fix it. I'll raise a PR to @maca88's fork.

The problem is upcast to nullable. IsCastRequired should check the unwrapped types because SQL does not care about nullability on the class level.

The correct fix, in my opinion is to include expression.Type.UnwrapIfNullable() != toType.UnwrapIfNullable() check after toType != typeof(object) here:

private bool IsCastRequired(Expression expression, System.Type toType, out bool existType)
{
existType = false;
return toType != typeof(object) &&
IsCastRequired(ExpressionsHelper.GetType(_parameters, expression), TypeFactory.GetDefaultTypeFor(toType), out existType);
}

@hazzik
Copy link
Member

hazzik commented Jul 20, 2020

Actually. I've just pushed it here. Feel free to drop/revert the last commit if you disagree.

@hazzik hazzik requested a review from fredericDelaporte July 20, 2020 23:45
@hazzik hazzik changed the title Fix CustomType cast for Linq provider Fix invalid cast on nullable custom type with Linq Jul 22, 2020
@hazzik hazzik merged commit 5993542 into nhibernate:5.3.x Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid cast on nullable custom type with Linq
3 participants