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

Adding a condition to cast QueryNode to SingleResourceCastNode for Unquoted Type Parameter #1313

Merged
merged 14 commits into from
Oct 30, 2024
16 changes: 15 additions & 1 deletion src/Microsoft.AspNetCore.OData/Query/Expressions/QueryBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,21 @@ public virtual Expression BindSingleResourceCastFunctionCall(SingleResourceFunct

IEdmModel model = context.Model;

string targetEdmTypeName = (string)((ConstantNode)node.Parameters.Last()).Value;
string targetEdmTypeName = null;
QueryNode queryNode = node.Parameters.Last();
if (queryNode is ConstantNode constantNode)
{
targetEdmTypeName = constantNode.Value as string;
}
else if (queryNode is SingleResourceCastNode singleResourceCastNode)
{
targetEdmTypeName = singleResourceCastNode.TypeReference.FullName();
}
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
else
{
throw Error.NotSupported(SRResources.QueryNodeBindingNotSupported, queryNode.Kind, "BindSingleResourceCastFunctionCall");
}

IEdmType targetEdmType = model.FindType(targetEdmTypeName);
Type targetClrType = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public async Task EntityTypeSerializesAsODataEntry()
"\"BaseSalary\":0," +
"\"Birthday\":\"2020-09-10T01:02:03Z\"," +
"\"WorkCompanyId\":0," +
"\"HomeAddress\":null" +
"\"HomeAddress\":null," +
"\"Location\":null" +
"}", actual);
}

Expand Down
2 changes: 2 additions & 0 deletions test/Microsoft.AspNetCore.OData.Tests/Models/Employee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class Employee

public Address HomeAddress { get; set; }

public Address Location { get; set; }

public IList<Employee> DirectReports { get; set; }
}

Expand Down
14 changes: 14 additions & 0 deletions test/Microsoft.AspNetCore.OData.Tests/Models/WorkAddress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//-----------------------------------------------------------------------------
// <copyright file="WorkAddress.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.AspNetCore.OData.Tests.Models;

public class WorkAddress : Address
{
public string OfficeNumber { get; set; }
}

Loading