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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 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,17 @@ 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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;

namespace Microsoft.AspNetCore.OData.Tests.Query.Expressions
{
public class FakeSingleEntityNode : SingleEntityNode
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly IEdmEntityTypeReference typeReference;
private readonly IEdmEntitySetBase set;

public FakeSingleEntityNode(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 FakeSingleEntityNode CreateFakeNodeForPerson()
{
var personType = HardCodedTestModel.GetEntityType("Microsoft.FullyQualified.NS.Person");
return new FakeSingleEntityNode(HardCodedTestModel.GetEntityTypeReference(personType), HardCodedTestModel.GetPeopleSet());
}
}

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

public FakeCollectionResourceNode(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 FakeCollectionResourceNode CreateFakeNodeForPerson()
{
var singleEntityNode = FakeSingleEntityNode.CreateFakeNodeForPerson();
return new FakeCollectionResourceNode(
singleEntityNode.EntityTypeReference, singleEntityNode.NavigationSource, singleEntityNode.EntityTypeReference, singleEntityNode.EntityTypeReference.AsCollection());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Microsoft.FullyQualified.NS;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;

namespace Microsoft.AspNetCore.OData.Tests.Query.Expressions
{
internal static class HardCodedTestModel
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved
{
private static readonly IEdmModel Model = BuildAndGetEdmModel();

public static IEdmModel TestModel
{
get { return Model; }
}

public static IEdmEntityType GetEntityType(string entityQualifiedName)
{
return TestModel.FindDeclaredType(entityQualifiedName) as IEdmEntityType;
}

public static IEdmComplexType GetEdmComplexType(string complexTypeQualifiedName)
{
return TestModel.FindDeclaredType(complexTypeQualifiedName) as IEdmComplexType;
}

public static IEdmEntityTypeReference GetEntityTypeReference(IEdmEntityType entityType)
{
return new EdmEntityTypeReference(entityType, false);
}

public static IEdmComplexTypeReference GetComplexTypeReference(IEdmComplexType complexType)
{
// Create a complex type reference using the EdmCoreModel
return new EdmComplexTypeReference(complexType, isNullable: false);
}

public static IEdmProperty GetPersonLocationProperty()
{
return GetEntityType("Microsoft.FullyQualified.NS.Person").FindProperty("Location");
}

/// <summary>
/// Get the entity set from the model
/// </summary>
/// <returns>People Set</returns>
public static IEdmEntitySet GetPeopleSet()
{
return TestModel.EntityContainer.FindEntitySet("People");
}

private static IEdmModel BuildAndGetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = "Microsoft.FullyQualified.NS";
builder.EntitySet<Person>("People");
builder.ComplexType<MyAddress>();
builder.ComplexType<WorkAddress>();
builder.EntitySet<Employee>("Employees");

return builder.GetEdmModel();
}
}
}

namespace Microsoft.FullyQualified.NS
{
public class Person
{
public int Id { get; set; }
public string FullName { get; set; }
public MyAddress Location { get; set; }
}

public class Employee : Person
{
public string EmployeeNumber { get; set; }
}

public class MyAddress
{
public string Street { get; set; }
public AddressType AddressType { get; set; }
}

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

public enum AddressType
{
Home,
Work,
Other
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNetCore.OData.Edm;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Query.Expressions;
using Microsoft.AspNetCore.OData.Query.Wrapper;
using Microsoft.AspNetCore.OData.Tests.Commons;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using Microsoft.OData.UriParser;
using Microsoft.FullyQualified.NS;
using Moq;
using Xunit;

Expand Down Expand Up @@ -100,6 +105,134 @@ public void BindSingleResourceFunctionCallNode_ThrowsNotSupported_ForNotAcceptNo
"Unknown function 'anyUnknown'.");
}

[Theory]
[InlineData(typeof(ConstantNode))]
[InlineData(typeof(SingleResourceCastNode))]
public void BindSingleResourceFunctionCallNode_CastingEntityType_ReturnsExpression(Type queryNodeType)
{
// Arrange
var binder = new MyQueryBinder();

var model = HardCodedTestModel.TestModel;
WanjohiSammy marked this conversation as resolved.
Show resolved Hide resolved

// Create the type reference and navigation source
var personType = HardCodedTestModel.GetEntityType("Microsoft.FullyQualified.NS.Person");
var personTypeRef = HardCodedTestModel.GetEntityTypeReference(personType);
var collectionNode = FakeCollectionResourceNode.CreateFakeNodeForPerson();

var employeeType = HardCodedTestModel.GetEntityType("Microsoft.FullyQualified.NS.Employee");

// Create a ResourceRangeVariableReferenceNode for the Person entity
var rangeVariable = new ResourceRangeVariable("$it", personTypeRef, collectionNode);
var personNode = new ResourceRangeVariableReferenceNode(rangeVariable.Name, rangeVariable) as SingleValueNode;

// Create the parameters list
int capacity = 2;
var parameters = new List<QueryNode>(capacity)
{
personNode // First parameter is the Person entity
};

if (queryNodeType == typeof(SingleResourceCastNode))
{
// Create a SingleResourceCastNode to cast Person to NS.Employee
var singleResourceCastNode = new SingleResourceCastNode(personNode as SingleResourceNode, employeeType);
parameters.Add(singleResourceCastNode); // Second parameter is the SingleResourceCastNode
}
else if (queryNodeType == typeof(ConstantNode))
{
// Create a ConstantNode to cast Person to NS.Employee
var constantNode = new ConstantNode("Microsoft.FullyQualified.NS.Employee");
parameters.Add(constantNode); // Second parameter is the ConstantNode
}

// Create the SingleResourceFunctionCallNode
var node = new SingleResourceFunctionCallNode("cast", parameters, collectionNode.ItemStructuredType, collectionNode.NavigationSource);

// Create an instance of QueryBinderContext using the real model and settings
Type clrType = model.GetClrType(personType);
var context = new QueryBinderContext(model, new ODataQuerySettings(), clrType);

// Act
Expression expression = binder.BindSingleResourceFunctionCallNode(node, context);

// Assert
Assert.NotNull(expression);
Assert.Equal("($it As Employee)", expression.ToString());
Assert.Equal("Microsoft.FullyQualified.NS.Employee", expression.Type.ToString());
Assert.Equal(typeof(Employee), expression.Type);
Assert.Equal(ExpressionType.TypeAs, expression.NodeType);
Assert.Equal(personType.FullName(), (expression as UnaryExpression).Operand.Type.FullName);
Assert.Equal(employeeType.FullName(), (expression as UnaryExpression).Type.FullName);
}

[Theory]
[InlineData(typeof(ConstantNode))]
[InlineData(typeof(SingleResourceCastNode))]
public void BindSingleResourceFunctionCallNode_PropertyCasting_ReturnsExpression(Type queryNodeType)
{
// Arrange
var binder = new MyQueryBinder();

var model = HardCodedTestModel.TestModel;

// Create the type reference and navigation source
var personType = HardCodedTestModel.GetEntityType("Microsoft.FullyQualified.NS.Person");
var personTypeRef = HardCodedTestModel.GetEntityTypeReference(personType);
var collectionNode = FakeCollectionResourceNode.CreateFakeNodeForPerson();

var myAddressType = HardCodedTestModel.GetEdmComplexType("Microsoft.FullyQualified.NS.MyAddress");
var workAddressType = HardCodedTestModel.GetEdmComplexType("Microsoft.FullyQualified.NS.WorkAddress");

// Create a ResourceRangeVariableReferenceNode for the Person entity
var rangeVariable = new ResourceRangeVariable("$it", personTypeRef, collectionNode);
var personNode = new ResourceRangeVariableReferenceNode(rangeVariable.Name, rangeVariable) as SingleValueNode;

// Create a SingleComplexNode for the Location property of the Person entity
var locationProperty = HardCodedTestModel.GetPersonLocationProperty();
var locationNode = new SingleComplexNode(personNode as SingleResourceNode, locationProperty);

// Create the parameters list
int capacity = 2;
var parameters = new List<QueryNode>(capacity)
{
locationNode // First parameter is the Location property
};

if(queryNodeType == typeof(SingleResourceCastNode))
{
// Create a SingleResourceCastNode to cast Location to NS.WorkAddress
var singleResourceCastNode = new SingleResourceCastNode(locationNode, workAddressType);
parameters.Add(singleResourceCastNode); // Second parameter is the SingleResourceCastNode
}
else if (queryNodeType == typeof(ConstantNode))
{
// Create a ConstantNode to cast Location to NS.WorkAddress
var constantNode = new ConstantNode("Microsoft.FullyQualified.NS.WorkAddress");
parameters.Add(constantNode); // Second parameter is the ConstantNode
}

// Create the SingleResourceFunctionCallNode
var node = new SingleResourceFunctionCallNode("cast", parameters, collectionNode.ItemStructuredType, collectionNode.NavigationSource);

// Create an instance of QueryBinderContext using the real model and settings
Type clrType = model.GetClrType(personType);
var context = new QueryBinderContext(model, new ODataQuerySettings(), clrType);

// Act
Expression expression = binder.BindSingleResourceFunctionCallNode(node, context);

// Assert
Assert.NotNull(expression);
Assert.Equal("($it.Location As WorkAddress)", expression.ToString());
Assert.Equal("Microsoft.FullyQualified.NS.WorkAddress", expression.Type.ToString());
Assert.Equal("Location", ((expression as UnaryExpression).Operand as MemberExpression).Member.Name);
Assert.Equal(typeof(WorkAddress), expression.Type);
Assert.Equal(ExpressionType.TypeAs, expression.NodeType);
Assert.Equal(workAddressType.FullName(), (expression as UnaryExpression).Type.FullName);
Assert.Equal(myAddressType.FullName(), (expression as UnaryExpression).Operand.Type.FullName);
}

[Fact]
public void BindSingleValueFunctionCallNode_ThrowsArgumentNull_ForInputs()
{
Expand All @@ -111,9 +244,9 @@ public void BindSingleValueFunctionCallNode_ThrowsArgumentNull_ForInputs()
typeRef.Setup(t => t.Definition).Returns(type.Object);
SingleValueFunctionCallNode node = new SingleValueFunctionCallNode("any", null, typeRef.Object);

// Act & Assert
ExceptionAssert.ThrowsArgumentNull(() => binder.BindSingleValueFunctionCallNode(null, null), "node");
ExceptionAssert.ThrowsArgumentNull(() => binder.BindSingleValueFunctionCallNode(node, null), "context");
// Act & Assert
ExceptionAssert.ThrowsArgumentNull(() => binder.BindSingleValueFunctionCallNode(null, null), "node");
ExceptionAssert.ThrowsArgumentNull(() => binder.BindSingleValueFunctionCallNode(node, null), "context");
}

[Fact]
Expand Down