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

Fix to #21006 - Support a default value for non-nullable properties #35746

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -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 entity (status code: {response.StatusCode}) for json: {json}");
}
}
}
Loading