Skip to content

Commit

Permalink
Handle projections of nested owned entity in Cosmos
Browse files Browse the repository at this point in the history
Fixes #34067
  • Loading branch information
ajcvickers committed Aug 2, 2024
1 parent 41e511f commit a63802b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
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

0 comments on commit a63802b

Please sign in to comment.