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

Handle projections of nested owned entity in Cosmos #34315

Merged
merged 1 commit into from
Aug 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
break;
}

Expression innerAccessExpression;
Expression valueExpression;
switch (projectionExpression)
{
case ObjectArrayAccessExpression objectArrayProjectionExpression:
innerAccessExpression = objectArrayProjectionExpression.Object;
_projectionBindings[objectArrayProjectionExpression] = parameterExpression;
valueExpression = CreateGetValueExpression(objectArrayProjectionExpression.Object, storeName, parameterExpression.Type);
break;

case EntityProjectionExpression entityProjectionExpression:
Expand All @@ -123,13 +123,27 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)

switch (accessExpression)
{
case ObjectAccessExpression innerObjectAccessExpression:
innerAccessExpression = innerObjectAccessExpression.Object;
_ownerMappings[accessExpression] =
(innerObjectAccessExpression.Navigation.DeclaringEntityType, innerAccessExpression);
break;
case ObjectReferenceExpression:
innerAccessExpression = jTokenParameter;
valueExpression = CreateGetValueExpression(jTokenParameter, storeName, parameterExpression.Type);
break;

case ObjectAccessExpression:
// Access to an owned type may be nested inside another owned type, so collect the store names
// and add owner mappings for each.
var storeNames = new List<string>();
while (accessExpression is ObjectAccessExpression objectAccessExpression)
{
accessExpression = objectAccessExpression.Object;
storeNames.Add(objectAccessExpression.PropertyName);
_ownerMappings[objectAccessExpression]
= (objectAccessExpression.Navigation.DeclaringEntityType, accessExpression);
}

valueExpression = CreateGetValueExpression(accessExpression, (string)null, typeof(JObject));
for (var i = storeNames.Count - 1; i >= 0; i--)
{
valueExpression = CreateGetValueExpression(valueExpression, storeNames[i], typeof(JObject));
}
break;
default:
throw new InvalidOperationException(
Expand All @@ -141,8 +155,6 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
throw new UnreachableException();
}

var valueExpression = CreateGetValueExpression(innerAccessExpression, storeName, parameterExpression.Type);

return MakeBinary(ExpressionType.Assign, binaryExpression.Left, valueExpression);
}

Expand Down
24 changes: 15 additions & 9 deletions test/EFCore.Cosmos.FunctionalTests/Query/OwnedQueryCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,21 @@ ORDER BY c["Id"]
""");
});

// TODO: #34068
public override async Task Project_owned_reference_navigation_which_does_not_own_additional(bool async)
{
// Always throws for sync.
if (async)
{
await Assert.ThrowsAsync<NullReferenceException>(() => base.Project_owned_reference_navigation_which_does_not_own_additional(async));
}
}
// Issue #34068
public override Task Project_owned_reference_navigation_which_does_not_own_additional(bool async)
=> CosmosTestHelpers.Instance.NoSyncTest(
async, async a =>
{
await base.Project_owned_reference_navigation_which_does_not_own_additional(a);

AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE c["Discriminator"] IN ("OwnedPerson", "Branch", "LeafB", "LeafA")
ORDER BY c["Id"]
""");
});

public override Task No_ignored_include_warning_when_implicit_load(bool async)
=> CosmosTestHelpers.Instance.NoSyncTest(
Expand Down
Loading