Skip to content

Commit

Permalink
Fix to #21006 - Support a default value for non-nullable properties
Browse files Browse the repository at this point in the history
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
  • Loading branch information
maumar committed Mar 8, 2025
1 parent 34ee81a commit 0ea33cb
Show file tree
Hide file tree
Showing 9 changed files with 1,470 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,11 @@ private Expression CreateGetValueExpression(
&& !property.IsShadowProperty())
{
var readExpression = CreateGetValueExpression(
jTokenExpression, storeName, type.MakeNullable(), property.GetTypeMapping());
jTokenExpression,
storeName,
type.MakeNullable(),
property.GetTypeMapping(),
isNonNullableScalar: false);

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

return Convert(
CreateGetValueExpression(jTokenExpression, storeName, type.MakeNullable(), property.GetTypeMapping()),
CreateGetValueExpression(
jTokenExpression,
storeName,
type.MakeNullable(),
property.GetTypeMapping(),
// 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
// returning defaults is supposed to help with evolving the schema - so this doesn't concern keys anyway (they shouldn't evolve)
isNonNullableScalar: !property.IsNullable && !property.IsKey()),
type);
}

private Expression CreateGetValueExpression(
Expression jTokenExpression,
string storeName,
Type type,
CoreTypeMapping typeMapping = null)
CoreTypeMapping typeMapping = null,
bool isNonNullableScalar = false)
{
Check.DebugAssert(type.IsNullableType(), "Must read nullable type from JObject.");

Expand Down Expand Up @@ -763,6 +775,7 @@ var body
Constant(CosmosClientWrapper.Serializer)),
converter.ConvertFromProviderExpression.Body);

var originalBodyType = body.Type;
if (body.Type != type)
{
body = Convert(body, type);
Expand All @@ -783,7 +796,11 @@ var body
}
else
{
replaceExpression = Default(type);
replaceExpression = isNonNullableScalar
? Expression.Convert(
Default(originalBodyType),
type)
: Default(type);
}

body = Condition(
Expand All @@ -799,7 +816,11 @@ var body
}
else
{
valueExpression = ConvertJTokenToType(jTokenExpression, typeMapping?.ClrType.MakeNullable() ?? type);
valueExpression = ConvertJTokenToType(
jTokenExpression,
(isNonNullableScalar
? typeMapping?.ClrType
: typeMapping?.ClrType.MakeNullable()) ?? type);

if (valueExpression.Type != type)
{
Expand Down
43 changes: 43 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/Query/AdHocCosmosTestHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Net;
using Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.EntityFrameworkCore.Query;

public class AdHocCosmosTestHelpers
{
public static async Task CreateCustomEntityHelperAsync(
Container container,
string json,
CancellationToken cancellationToken)
{
var document = JObject.Parse(json);

var stream = new MemoryStream();
await using var __ = stream.ConfigureAwait(false);
var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false);
await using var ___ = writer.ConfigureAwait(false);
using var jsonWriter = new JsonTextWriter(writer);

CosmosClientWrapper.Serializer.Serialize(jsonWriter, document);
await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false);

var response = await container.CreateItemStreamAsync(
stream,
PartitionKey.None,
requestOptions: null,
cancellationToken)
.ConfigureAwait(false);


if (response.StatusCode != HttpStatusCode.Created)
{
throw new InvalidOperationException($"Failed to create entitty (status code: {response.StatusCode}) for json: {json}");
}
}
}
Loading

0 comments on commit 0ea33cb

Please sign in to comment.