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
{
internal class WorkAddress : Address
{
public string OfficeNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//-----------------------------------------------------------------------------
// <copyright file="MockEdmNodesHelper.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------

using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;

namespace Microsoft.AspNetCore.OData.Tests.Query.Expressions
{
public class MockSingleEntityNode : SingleEntityNode
{
private readonly IEdmEntityTypeReference typeReference;
private readonly IEdmEntitySetBase set;

public MockSingleEntityNode(IEdmEntityTypeReference type, IEdmEntitySetBase set)
{
this.typeReference = type;
this.set = set;
}

public override IEdmTypeReference TypeReference
{
get { return this.typeReference; }
}

public override IEdmNavigationSource NavigationSource
{
get { return this.set; }
}

public override IEdmStructuredTypeReference StructuredTypeReference
{
get { return this.typeReference; }
}

public override IEdmEntityTypeReference EntityTypeReference
{
get { return this.typeReference; }
}

public static MockSingleEntityNode CreateFakeNodeForEmployee()
{
var employeeType = HardCodedTestModel.GetEntityTypeFor("Microsoft.AspNetCore.OData.Tests.Models.Employee");
return new MockSingleEntityNode(HardCodedTestModel.GetEntityTypeReferenceFor(employeeType), HardCodedTestModel.GetEntitySetFor("Employees"));
}
}

public class MockCollectionResourceNode : CollectionResourceNode
{
private readonly IEdmStructuredTypeReference _typeReference;
private readonly IEdmNavigationSource _source;
private readonly IEdmTypeReference _itemType;
private readonly IEdmCollectionTypeReference _collectionType;

public MockCollectionResourceNode(IEdmStructuredTypeReference type, IEdmNavigationSource source, IEdmTypeReference itemType, IEdmCollectionTypeReference collectionType)
{
_typeReference = type;
_source = source;
_itemType = itemType;
_collectionType = collectionType;
}

public override IEdmStructuredTypeReference ItemStructuredType => _typeReference;

public override IEdmNavigationSource NavigationSource => _source;

public override IEdmTypeReference ItemType => _itemType;

public override IEdmCollectionTypeReference CollectionType => _collectionType;

public static MockCollectionResourceNode CreateFakeNodeForEmployee()
{
var singleEntityNode = MockSingleEntityNode.CreateFakeNodeForEmployee();
return new MockCollectionResourceNode(
singleEntityNode.EntityTypeReference, singleEntityNode.NavigationSource, singleEntityNode.EntityTypeReference, singleEntityNode.EntityTypeReference.AsCollection());
}
}
}
Loading