Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e4c8137
Add model-wide and member JSON reader/writer configuration
AndriySvyryd Oct 31, 2026
6922cb1
Preserve configured JSON readers in routing, parameters, and runtime …
AndriySvyryd Oct 31, 2026
71055c1
Address JSON reader/writer review feedback
AndriySvyryd Nov 2, 2026
489d937
Fix JSON scalar queries, bulk updates, and null partition routing
AndriySvyryd Nov 2, 2026
3cf3ece
Simplify JSON value serialization and cache reader activation
AndriySvyryd Nov 3, 2026
23f8c2a
Fix JSON enum ownership keys and SQLite scalar projections
AndriySvyryd Nov 3, 2026
6000dd1
Document JSON query conversion contract and cover encoded scalars
AndriySvyryd Nov 3, 2026
8fd683c
Fix Cosmos JSON routing and SQLite scalar materialization
AndriySvyryd Nov 3, 2026
d331a0c
Preserve JSON parameter names and refresh bulk-update baselines
AndriySvyryd Nov 3, 2026
825687d
Simplify JSON formatter configuration and provider serialization
AndriySvyryd Nov 3, 2026
0036082
Update SQL Server JSON baselines and remove forced-GC test
AndriySvyryd Nov 3, 2026
c37ee67
Fix JSON parameter collisions and compiled Cosmos keys
AndriySvyryd Nov 13, 2026
7f75cc4
Consolidate Cosmos compiled-model key regression coverage
AndriySvyryd Nov 13, 2026
01f3c5b
Restrict collection JSON configuration and preserve native parameters
AndriySvyryd Nov 13, 2026
e10adba
Resolve JSON scalar parameters through provider mappings
AndriySvyryd Nov 13, 2026
2c500d6
Fix JSON reader composition, quotation and formatting regressions
AndriySvyryd Nov 15, 2026
538f898
Preserve JSON mapping converters and inferred element formatters
AndriySvyryd Nov 16, 2026
ae4c769
Update Cosmos converted collection query baselines
AndriySvyryd Nov 17, 2026
5f9c860
Align upstream JSON reader tests with provider-value configuration
AndriySvyryd Nov 24, 2026
9e44fc0
Refresh owned complex JSON query and compiled-model baselines
AndriySvyryd Nov 24, 2026
7e116ca
Fix inline Cosmos formatter inference and numeric enum JSON assignments
AndriySvyryd Nov 24, 2026
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
@@ -1,6 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Storage.Internal;

namespace Microsoft.EntityFrameworkCore.Cosmos.Extensions.Internal;

/// <summary>
Expand All @@ -19,4 +23,29 @@ public static class CosmosPropertyExtensions
/// </summary>
public static bool IsPersisted(this IReadOnlyProperty property)
=> property.GetJsonPropertyName() != string.Empty;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// 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 static string SerializeJsonId(this IProperty property, object? providerValue)
{
var mapping = property.GetTypeMapping();
var readerWriter = mapping.JsonValueReaderWriter!;
#pragma warning disable EF1001 // Internal EF Core API usage.
if (mapping.Converter is not null
&& readerWriter is IJsonConvertedValueReaderWriter converted)
{
readerWriter = converted.InnerReaderWriter;
}
#pragma warning restore EF1001

using var document = JsonDocument.Parse(readerWriter.ToJsonString(providerValue));
return document.RootElement.ValueKind == JsonValueKind.String
&& document.RootElement.GetString() is { Length: > 0 } id
? id
: throw new InvalidOperationException(CosmosStrings.InvalidResourceId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Storage.Internal;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Internal;
Expand Down Expand Up @@ -32,66 +35,82 @@ public static PartitionKeyBuilder Add(this PartitionKeyBuilder builder, object?
: value;
}

var converter = property?.GetTypeMapping().Converter;
var mapping = property?.GetTypeMapping();
var converter = mapping?.Converter;
var writeNull = value is not null || mapping?.JsonValueReaderWriter?.HandlesNullWrites == true;
if (converter != null)
{
value = converter.ConvertToProvider(value);
}

if (value == null)
if (value != null)
{
builder.AddNullValue();
}
else
{
var expectedType = (converter?.ProviderClrType ?? property?.ClrType)?.UnwrapNullableType();
switch (value)
var valueType = value.GetType();
if (value is not string and not bool && !valueType.IsNumeric())
{
case string stringValue:
if (expectedType != null && expectedType != typeof(string))
{
CheckType(typeof(string));
}

builder.Add(stringValue);
break;

case bool boolValue:
if (expectedType != null && expectedType != typeof(bool))
{
CheckType(typeof(bool));
}

builder.Add(boolValue);
break;

case var _ when value.GetType().IsNumeric():
if (expectedType != null && !expectedType.IsNumeric())
{
CheckType(value.GetType());
}

builder.Add(Convert.ToDouble(value));
break;
throw new InvalidOperationException(CosmosStrings.PartitionKeyBadValue(valueType));
}

default:
throw new InvalidOperationException(CosmosStrings.PartitionKeyBadValue(value.GetType()));
var expectedType = (converter?.ProviderClrType ?? property?.ClrType)?.UnwrapNullableType();
if (expectedType != null
&& expectedType != valueType
&& !(expectedType.IsNumeric() && valueType.IsNumeric()))
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyBadValueType(
expectedType.ShortDisplayName(),
property!.DeclaringType.DisplayName(),
property.Name,
Comment thread
AndriySvyryd marked this conversation as resolved.
Dismissed
valueType.DisplayName()));
}
}

var readerWriter = mapping?.JsonValueReaderWriter;
#pragma warning disable EF1001 // Internal EF Core API usage.
if (converter is not null
&& readerWriter is IJsonConvertedValueReaderWriter converted)
{
readerWriter = converted.InnerReaderWriter;
}
#pragma warning restore EF1001

void CheckType(Type actualType)
if (readerWriter != null
&& writeNull
&& (value != null || readerWriter.HandlesNullWrites))
{
if (value != null
&& !readerWriter.ValueType.IsInstanceOfType(value)
&& value.GetType().IsNumeric()
&& readerWriter.ValueType.IsNumeric())
{
if (expectedType != null && expectedType != actualType)
// Unconfigured numeric keys must not be narrowed to the property's CLR type.
if (property!.GetJsonValueReaderWriter() == null
&& property.DeclaringType.Model.FindTypeMappingConfiguration(property.ClrType.UnwrapNullableType())
Comment thread
AndriySvyryd marked this conversation as resolved.
Dismissed
?.GetJsonValueReaderWriterType() == null)
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyBadValueType(
expectedType.ShortDisplayName(),
property!.DeclaringType.DisplayName(),
property.Name,
actualType.DisplayName()));
return builder.Add(Convert.ToDouble(value));
}

value = Convert.ChangeType(value, readerWriter.ValueType, CultureInfo.InvariantCulture);
}

using var document = JsonDocument.Parse(readerWriter.ToJsonString(value));
value = document.RootElement.ValueKind switch
{
JsonValueKind.Null => null,
JsonValueKind.String => document.RootElement.GetString(),
JsonValueKind.True or JsonValueKind.False => document.RootElement.GetBoolean(),
JsonValueKind.Number => document.RootElement.GetDouble(),
_ => throw new InvalidOperationException(CosmosStrings.PartitionKeyBadValue(typeof(JsonElement)))
};
}

return builder;
return value switch
{
null => builder.AddNullValue(),
string stringValue => builder.Add(stringValue),
bool boolValue => builder.Add(boolValue),
_ => builder.Add(Convert.ToDouble(value))
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal;
/// </summary>
public class CosmosModelValidator(ModelValidatorDependencies dependencies) : ModelValidator(dependencies)
{
/// <inheritdoc />
protected override bool IsJsonProperty(IProperty property)
=> property.IsPersisted();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
19 changes: 10 additions & 9 deletions src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class CosmosQuerySqlGenerator(ITypeMappingSource typeMappingSource, IMode
private List<SqlParameter> _sqlParameters = null!;

/// <summary>
/// Lookup table from <see cref="SqlParameterExpression.Name" /> (the "original" name) to the Cosmos <see cref="SqlParameter" />,
/// whose name has underdone uniquification and prefixing. This is used when the same parameter is referenced multiple times in the query.
/// Lookup table from the original parameter name and type mapping to the Cosmos <see cref="SqlParameter" />,
/// whose name has undergone uniquification and prefixing. This is used when the same parameter is referenced multiple times in the query.
/// </summary>
private Dictionary<string, SqlParameter> _sqlParametersByOriginalName = null!;
private Dictionary<(string, CoreTypeMapping), SqlParameter> _sqlParametersByOriginalName = null!;

/// <summary>
/// Contains final parameter names (prefixed, uniquified) seen so far, for uniquification purposes.
Expand Down Expand Up @@ -815,18 +815,19 @@ protected virtual Expression VisitCase(CaseExpression caseExpression)
/// </summary>
protected virtual Expression VisitSqlParameter(SqlParameterExpression sqlParameterExpression)
{
if (!_sqlParametersByOriginalName.TryGetValue(sqlParameterExpression.Name, out var sqlParameter))
{
Check.DebugAssert(sqlParameterExpression.TypeMapping is not null, "SqlParameterExpression without a type mapping.");
Check.DebugAssert(sqlParameterExpression.TypeMapping is not null, "SqlParameterExpression without a type mapping.");
var typeMapping = (CosmosTypeMapping)sqlParameterExpression.TypeMapping;
var key = (sqlParameterExpression.Name, typeMapping);

if (!_sqlParametersByOriginalName.TryGetValue(key, out var sqlParameter))
{
var parameterName = PrefixAndUniquifyParameterName(sqlParameterExpression.Name);

var value = _parameterValues[sqlParameterExpression.Name];

sqlParameter = ((CosmosTypeMapping)sqlParameterExpression.TypeMapping)
.CreateParameter(parameterName, value);
sqlParameter = typeMapping.CreateParameter(parameterName, value);

_sqlParametersByOriginalName[sqlParameterExpression.Name] = sqlParameter;
_sqlParametersByOriginalName[key] = sqlParameter;
_sqlParameters.Add(sqlParameter);
}

Expand Down
Loading
Loading