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

Fixed issue with assigning values to nullable types #1071

Merged
merged 1 commit into from Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/DotVVM.Framework.Tests/Binding/BindingCompilationTests.cs
Expand Up @@ -194,6 +194,14 @@ public void BindingCompiler_Valid_InterpolatedString_NestedExpressions(string ex
Assert.AreEqual(evaluated, binding);
}

[TestMethod]
public void BindingCompiler_Valid_AssignNullables()
{
var viewModel = new TestViewModel() { DateTime = DateTime.Now };
ExecuteBinding("DateFrom = DateTime", viewModel);
Assert.AreEqual(viewModel.DateTime, viewModel.DateFrom.Value);
}

[TestMethod]
[DataRow(@"$'Interpolated {IntProp < LongProperty}'", "Interpolated True")]
[DataRow(@"$'Interpolated {StringProp ?? 'StringPropWasNull'}'", "Interpolated StringPropWasNull")]
Expand Down
Expand Up @@ -42,12 +42,19 @@ protected override Expression VisitLambda<T>(Expression<T> expression)

protected override Expression VisitBinary(BinaryExpression node)
{

Expression createExpr(Expression left)
{
return CheckForNull(Visit(node.Right), right =>
Expression.MakeBinary(node.NodeType, left, right, false, node.Method, node.Conversion),
checkReferenceTypes: false);
{
// When assigning values to nullable types, convert value to nullable first
if (node.NodeType == ExpressionType.Assign)
{
if (ReflectionUtils.IsNullableType(left.Type) && !ReflectionUtils.IsNullableType(right.Type))
right = Expression.Convert(right, left.Type);
}

return Expression.MakeBinary(node.NodeType, left, right, false, node.Method, node.Conversion);
}, checkReferenceTypes: false);
}

if (node.NodeType.ToString().EndsWith("Assign"))
Expand Down