Skip to content

Commit d55392e

Browse files
committed
Fix to #21006 - Support a default value for non-nullable properties
Only for scalar properties when projecting Json-mapped entity. Only need to change code for Cosmos - relational already works in the desired way after the change to streaming (properties that are not encountered maintain their default value) We still throw exception if JSON contains explicit null where non-nullable scalar is expected. Fixes #21006
1 parent ce99d82 commit d55392e

File tree

9 files changed

+1467
-12
lines changed

9 files changed

+1467
-12
lines changed

Diff for: src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.CosmosProjectionBindingRemovingExpressionVisitorBase.cs

+26-5
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,11 @@ private Expression CreateGetValueExpression(
691691
&& !property.IsShadowProperty())
692692
{
693693
var readExpression = CreateGetValueExpression(
694-
jTokenExpression, storeName, type.MakeNullable(), property.GetTypeMapping());
694+
jTokenExpression,
695+
storeName,
696+
type.MakeNullable(),
697+
property.GetTypeMapping(),
698+
isNonNullableScalar: false);
695699

696700
var nonNullReadExpression = readExpression;
697701
if (nonNullReadExpression.Type != type)
@@ -712,15 +716,23 @@ private Expression CreateGetValueExpression(
712716
}
713717

714718
return Convert(
715-
CreateGetValueExpression(jTokenExpression, storeName, type.MakeNullable(), property.GetTypeMapping()),
719+
CreateGetValueExpression(
720+
jTokenExpression,
721+
storeName,
722+
type.MakeNullable(),
723+
property.GetTypeMapping(),
724+
// special case keys - we check them for null to see if the entity needs to be materialized, so we want to keep the null, rather than non-nullable default
725+
// returning defaults is supposed to help with evolving the schema - so this doesn't concern keys anyway (they shouldn't evolve)
726+
isNonNullableScalar: !property.IsNullable && !property.IsKey()),
716727
type);
717728
}
718729

719730
private Expression CreateGetValueExpression(
720731
Expression jTokenExpression,
721732
string storeName,
722733
Type type,
723-
CoreTypeMapping typeMapping = null)
734+
CoreTypeMapping typeMapping = null,
735+
bool isNonNullableScalar = false)
724736
{
725737
Check.DebugAssert(type.IsNullableType(), "Must read nullable type from JObject.");
726738

@@ -763,6 +775,7 @@ var body
763775
Constant(CosmosClientWrapper.Serializer)),
764776
converter.ConvertFromProviderExpression.Body);
765777

778+
var originalBodyType = body.Type;
766779
if (body.Type != type)
767780
{
768781
body = Convert(body, type);
@@ -783,7 +796,11 @@ var body
783796
}
784797
else
785798
{
786-
replaceExpression = Default(type);
799+
replaceExpression = isNonNullableScalar
800+
? Expression.Convert(
801+
Default(originalBodyType),
802+
type)
803+
: Default(type);
787804
}
788805

789806
body = Condition(
@@ -799,7 +816,11 @@ var body
799816
}
800817
else
801818
{
802-
valueExpression = ConvertJTokenToType(jTokenExpression, typeMapping?.ClrType.MakeNullable() ?? type);
819+
valueExpression = ConvertJTokenToType(
820+
jTokenExpression,
821+
(isNonNullableScalar
822+
? typeMapping?.ClrType
823+
: typeMapping?.ClrType.MakeNullable()) ?? type);
803824

804825
if (valueExpression.Type != type)
805826
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Net;
5+
using Microsoft.Azure.Cosmos;
6+
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
10+
namespace Microsoft.EntityFrameworkCore.Query;
11+
12+
public class AdHocCosmosTestHelpers
13+
{
14+
public static async Task CreateCustomEntityHelperAsync(
15+
Container container,
16+
string json,
17+
CancellationToken cancellationToken)
18+
{
19+
var document = JObject.Parse(json);
20+
21+
var stream = new MemoryStream();
22+
await using var __ = stream.ConfigureAwait(false);
23+
var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false);
24+
await using var ___ = writer.ConfigureAwait(false);
25+
using var jsonWriter = new JsonTextWriter(writer);
26+
27+
CosmosClientWrapper.Serializer.Serialize(jsonWriter, document);
28+
await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);
29+
30+
var response = await container.CreateItemStreamAsync(
31+
stream,
32+
PartitionKey.None,
33+
requestOptions: null,
34+
cancellationToken)
35+
.ConfigureAwait(false);
36+
37+
38+
if (response.StatusCode != HttpStatusCode.Created)
39+
{
40+
throw new InvalidOperationException($"Failed to create entitty (status code: {response.StatusCode}) for json: {json}");
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)