Skip to content

Commit

Permalink
Cosmos: return null instead of throwing from Find (#25887)
Browse files Browse the repository at this point in the history
Fixes #25115
  • Loading branch information
roji authored Sep 7, 2021
1 parent 3fe9bef commit 8ee4c05
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public override Expression Visit(Expression expression)
var readItemExpression = new ReadItemExpression(entityType, propertyParameterList);

return CreateShapedQueryExpression(entityType, readItemExpression)
.UpdateResultCardinality(ResultCardinality.Single);
.UpdateResultCardinality(ResultCardinality.SingleOrDefault);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public virtual IAsyncEnumerable<JObject> ExecuteSqlQueryAsync(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual JObject ExecuteReadItem(
public virtual JObject? ExecuteReadItem(
string containerId,
string? partitionKey,
string resourceId)
Expand All @@ -577,7 +577,7 @@ public virtual JObject ExecuteReadItem(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual async Task<JObject> ExecuteReadItemAsync(
public virtual async Task<JObject?> ExecuteReadItemAsync(
string containerId,
string? partitionKey,
string resourceId,
Expand Down Expand Up @@ -622,8 +622,13 @@ private static Task<ResponseMessage> CreateSingleItemQueryAsync(
cancellationToken: cancellationToken);
}

private static JObject JObjectFromReadItemResponseMessage(ResponseMessage responseMessage)
private static JObject? JObjectFromReadItemResponseMessage(ResponseMessage responseMessage)
{
if (responseMessage.StatusCode == HttpStatusCode.NotFound)
{
return null;
}

responseMessage.EnsureSuccessStatusCode();

var responseStream = responseMessage.Content;
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Storage/Internal/ICosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Task<bool> DeleteItemAsync(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
JObject ExecuteReadItem(
JObject? ExecuteReadItem(
string containerId,
string? partitionKey,
string resourceId);
Expand All @@ -159,7 +159,7 @@ JObject ExecuteReadItem(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
Task<JObject> ExecuteReadItemAsync(
Task<JObject?> ExecuteReadItemAsync(
string containerId,
string? partitionKey,
string resourceId,
Expand Down
83 changes: 83 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/FindCosmosTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore
{
public abstract class FindCosmosTest : FindTestBase<FindCosmosTest.FindCosmosFixture>
{
protected FindCosmosTest(FindCosmosFixture fixture)
: base(fixture)
{
fixture.TestSqlLoggerFactory.Clear();
}

[ConditionalFact(Skip = "#25886")]
public override void Find_base_type_using_derived_set_tracked() {}

[ConditionalFact(Skip = "#25886")]
public override Task Find_base_type_using_derived_set_tracked_async()
=> Task.CompletedTask;

[ConditionalFact(Skip = "#25886")]
public override void Find_derived_using_base_set_type_from_store() {}

[ConditionalFact(Skip = "#25886")]
public override Task Find_derived_using_base_set_type_from_store_async()
=> Task.CompletedTask;

public class FindCosmosTestSet : FindCosmosTest
{
public FindCosmosTestSet(FindCosmosFixture fixture)
: base(fixture)
{
}

protected override TEntity Find<TEntity>(DbContext context, params object[] keyValues)
=> context.Set<TEntity>().Find(keyValues);

protected override ValueTask<TEntity> FindAsync<TEntity>(DbContext context, params object[] keyValues)
=> context.Set<TEntity>().FindAsync(keyValues);
}

public class FindCosmosTestContext : FindCosmosTest
{
public FindCosmosTestContext(FindCosmosFixture fixture)
: base(fixture)
{
}

protected override TEntity Find<TEntity>(DbContext context, params object[] keyValues)
=> context.Find<TEntity>(keyValues);

protected override ValueTask<TEntity> FindAsync<TEntity>(DbContext context, params object[] keyValues)
=> context.FindAsync<TEntity>(keyValues);
}

public class FindCosmosTestNonGeneric : FindCosmosTest
{
public FindCosmosTestNonGeneric(FindCosmosFixture fixture)
: base(fixture)
{
}

protected override TEntity Find<TEntity>(DbContext context, params object[] keyValues)
=> (TEntity)context.Find(typeof(TEntity), keyValues);

protected override async ValueTask<TEntity> FindAsync<TEntity>(DbContext context, params object[] keyValues)
=> (TEntity)await context.FindAsync(typeof(TEntity), keyValues);
}

public class FindCosmosFixture : FindFixtureBase
{
public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;

protected override ITestStoreFactory TestStoreFactory
=> CosmosTestStoreFactory.Instance;
}
}
}

0 comments on commit 8ee4c05

Please sign in to comment.