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

[6.0.1] Cosmos: Use configured serializer when reading #26702

Merged
merged 1 commit into from
Nov 16, 2021
Merged
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 @@ -9,6 +9,7 @@
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down Expand Up @@ -39,6 +40,10 @@ private static readonly MethodInfo _jTokenToObjectMethodInfo
= typeof(JToken).GetRuntimeMethods()
.Single(mi => mi.Name == nameof(JToken.ToObject) && mi.GetParameters().Length == 0);

private static readonly MethodInfo _jTokenToObjectWithSerializerMethodInfo
= typeof(JToken).GetRuntimeMethods()
.Single(mi => mi.Name == nameof(JToken.ToObject) && mi.GetParameters().Length == 1 && mi.IsGenericMethodDefinition);

private static readonly MethodInfo _collectionAccessorAddMethodInfo
= typeof(IClrCollectionAccessor).GetTypeInfo()
.GetDeclaredMethod(nameof(IClrCollectionAccessor.Add));
Expand All @@ -65,16 +70,23 @@ private readonly IDictionary<Expression, Expression> _ordinalParameterBindings
private List<IncludeExpression> _pendingIncludes
= new();

private readonly bool _useOldBehavior;

private static readonly MethodInfo _toObjectMethodInfo
= typeof(CosmosProjectionBindingRemovingExpressionVisitorBase)
.GetRuntimeMethods().Single(mi => mi.Name == nameof(SafeToObject));

private static readonly MethodInfo _toObjectWithSerializerMethodInfo
= typeof(CosmosProjectionBindingRemovingExpressionVisitorBase)
.GetRuntimeMethods().Single(mi => mi.Name == nameof(SafeToObjectWithSerializer));

public CosmosProjectionBindingRemovingExpressionVisitorBase(
ParameterExpression jObjectParameter,
bool trackQueryResults)
{
_jObjectParameter = jObjectParameter;
_trackQueryResults = trackQueryResults;
_useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue26690", out var enabled) && enabled;
}

protected override Expression VisitBinary(BinaryExpression binaryExpression)
Expand Down Expand Up @@ -704,9 +716,14 @@ private Expression CreateGetValueExpression(
var body
= ReplacingExpressionVisitor.Replace(
converter.ConvertFromProviderExpression.Parameters.Single(),
Expression.Call(
jTokenParameter,
_jTokenToObjectMethodInfo.MakeGenericMethod(converter.ProviderClrType)),
_useOldBehavior
? Expression.Call(
jTokenParameter,
_jTokenToObjectMethodInfo.MakeGenericMethod(converter.ProviderClrType))
: Expression.Call(
jTokenParameter,
_jTokenToObjectWithSerializerMethodInfo.MakeGenericMethod(converter.ProviderClrType),
Expression.Constant(CosmosClientWrapper.Serializer)),
converter.ConvertFromProviderExpression.Body);

if (body.Type != type)
Expand Down Expand Up @@ -760,11 +777,16 @@ private Expression ConvertJTokenToType(Expression jTokenExpression, Type type)
=> type == typeof(JToken)
? jTokenExpression
: Expression.Call(
_toObjectMethodInfo.MakeGenericMethod(type),
_useOldBehavior
? _toObjectMethodInfo.MakeGenericMethod(type)
: _toObjectWithSerializerMethodInfo.MakeGenericMethod(type),
jTokenExpression);

private static T SafeToObject<T>(JToken token)
=> token == null || token.Type == JTokenType.Null ? default : token.ToObject<T>();

private static T SafeToObjectWithSerializer<T>(JToken token)
=> token == null || token.Type == JTokenType.Null ? default : token.ToObject<T>(CosmosClientWrapper.Serializer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public TestSqlLoggerFactory TestSqlLoggerFactory
public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;

protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public override bool SupportsDecimalComparisons
public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;

public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;

Expand Down
3 changes: 0 additions & 3 deletions test/EFCore.Cosmos.FunctionalTests/EndToEndCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Serialization.HybridRow;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public override bool SupportsDecimalComparisons

public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public override bool SupportsDecimalComparisons

public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public override bool SupportsDecimalComparisons

public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;
}
}
}
34 changes: 26 additions & 8 deletions test/EFCore.Specification.Tests/BuiltInDataTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -261,7 +262,7 @@ private void QueryBuiltInDataTypesTest<TEntity>(EntityEntry<TEntity> source)

if (entityType.FindProperty(nameof(BuiltInDataTypes.TestDateTimeOffset)) != null)
{
var param7 = new DateTimeOffset(new DateTime(), TimeSpan.Zero);
var param7 = new DateTimeOffset(new DateTime(), TimeSpan.FromHours(-8.0));
Assert.Same(
entity,
set.Where(e => e.Id == 11 && EF.Property<DateTimeOffset>(e, nameof(BuiltInDataTypes.TestDateTimeOffset)) == param7)
Expand Down Expand Up @@ -495,7 +496,7 @@ protected EntityEntry<TEntity> AddTestBuiltInDataTypes<TEntity>(DbSet<TEntity> s
TestDouble = -1.23456789,
TestDecimal = -1234567890.01M,
TestDateTime = Fixture.DefaultDateTime,
TestDateTimeOffset = new DateTimeOffset(new DateTime(), TimeSpan.Zero),
TestDateTimeOffset = new DateTimeOffset(new DateTime(), TimeSpan.FromHours(-8.0)),
TestTimeSpan = new TimeSpan(0, 10, 9, 8, 7),
TestSingle = -1.234F,
TestBoolean = true,
Expand Down Expand Up @@ -1296,7 +1297,7 @@ public virtual void Can_insert_and_read_back_all_non_nullable_data_types()
TestInt64 = -1234567890123456789L,
TestDouble = -1.23456789,
TestDecimal = -1234567890.01M,
TestDateTime = DateTime.Parse("01/01/2000 12:34:56"),
TestDateTime = DateTime.Parse("01/01/2000 12:34:56", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal),
TestDateTimeOffset = new DateTimeOffset(DateTime.Parse("01/01/2000 12:34:56"), TimeSpan.FromHours(-8.0)),
TestTimeSpan = new TimeSpan(0, 10, 9, 8, 7),
TestSingle = -1.234F,
Expand Down Expand Up @@ -1330,7 +1331,8 @@ public virtual void Can_insert_and_read_back_all_non_nullable_data_types()
AssertEqualIfMapped(entityType, -1234567890123456789L, () => dt.TestInt64);
AssertEqualIfMapped(entityType, -1.23456789, () => dt.TestDouble);
AssertEqualIfMapped(entityType, -1234567890.01M, () => dt.TestDecimal);
AssertEqualIfMapped(entityType, DateTime.Parse("01/01/2000 12:34:56"), () => dt.TestDateTime);
AssertEqualIfMapped(entityType, DateTime.Parse("01/01/2000 12:34:56", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal),
() => dt.TestDateTime);
AssertEqualIfMapped(
entityType, new DateTimeOffset(DateTime.Parse("01/01/2000 12:34:56"), TimeSpan.FromHours(-8.0)),
() => dt.TestDateTimeOffset);
Expand Down Expand Up @@ -1547,6 +1549,14 @@ private void AssertEqualIfMapped<T>(IEntityType entityType, T expected, Expressi
{
Assert.True(Equal(Convert.ToUInt64(expected), Convert.ToUInt64(actual)), $"Expected:\t{expected}\r\nActual:\t{actual}");
}
else if(type == typeof(DateTime))
{
Assert.True(Equal((DateTime)(object)expected, (DateTime)(object)actual), $"Expected:\t{expected:O}\r\nActual:\t{actual:O}");
}
else if (type == typeof(DateTimeOffset))
{
Assert.True(Equal((DateTimeOffset)(object)expected, (DateTimeOffset)(object)actual), $"Expected:\t{expected:O}\r\nActual:\t{actual:O}");
}
else
{
Assert.Equal(expected, actual);
Expand Down Expand Up @@ -1586,6 +1596,12 @@ private bool Equal(ulong left, ulong right)
return left == right;
}

private bool Equal(DateTime left, DateTime right)
=> left.Equals(right) && (!Fixture.PreservesDateTimeKind || left.Kind == right.Kind);

private bool Equal(DateTimeOffset left, DateTimeOffset right)
=> left.EqualsExact(right);

private static Type UnwrapNullableType(Type type)
=> type == null ? null : Nullable.GetUnderlyingType(type) ?? type;

Expand Down Expand Up @@ -1674,9 +1690,9 @@ public virtual void Can_insert_and_read_back_all_nullable_data_types_with_values
TestNullableInt64 = -1234567890123456789L,
TestNullableDouble = -1.23456789,
TestNullableDecimal = -1234567890.01M,
TestNullableDateTime = DateTime.Parse("01/01/2000 12:34:56"),
TestNullableDateTime = DateTime.Parse("01/01/2000 12:34:56").ToUniversalTime(),
TestNullableDateTimeOffset =
new DateTimeOffset(DateTime.Parse("01/01/2000 12:34:56"), TimeSpan.FromHours(-8.0)),
new DateTimeOffset(DateTime.Parse("01/01/2000 12:34:56"), TimeSpan.FromHours(-8.0)).ToUniversalTime(),
TestNullableTimeSpan = new TimeSpan(0, 10, 9, 8, 7),
TestNullableSingle = -1.234F,
TestNullableBoolean = false,
Expand Down Expand Up @@ -1711,9 +1727,9 @@ public virtual void Can_insert_and_read_back_all_nullable_data_types_with_values
AssertEqualIfMapped(entityType, -1234567890123456789L, () => dt.TestNullableInt64);
AssertEqualIfMapped(entityType, -1.23456789, () => dt.TestNullableDouble);
AssertEqualIfMapped(entityType, -1234567890.01M, () => dt.TestNullableDecimal);
AssertEqualIfMapped(entityType, DateTime.Parse("01/01/2000 12:34:56"), () => dt.TestNullableDateTime);
AssertEqualIfMapped(entityType, DateTime.Parse("01/01/2000 12:34:56").ToUniversalTime(), () => dt.TestNullableDateTime);
AssertEqualIfMapped(
entityType, new DateTimeOffset(DateTime.Parse("01/01/2000 12:34:56"), TimeSpan.FromHours(-8.0)),
entityType, new DateTimeOffset(DateTime.Parse("01/01/2000 12:34:56"), TimeSpan.FromHours(-8.0)).ToUniversalTime(),
() => dt.TestNullableDateTimeOffset);
AssertEqualIfMapped(entityType, new TimeSpan(0, 10, 9, 8, 7), () => dt.TestNullableTimeSpan);
AssertEqualIfMapped(entityType, -1.234F, () => dt.TestNullableSingle);
Expand Down Expand Up @@ -2392,6 +2408,8 @@ public virtual int IntegerPrecision
public abstract bool SupportsDecimalComparisons { get; }

public abstract DateTime DefaultDateTime { get; }

public abstract bool PreservesDateTimeKind { get; }
}

protected class BuiltInDataTypesBase
Expand Down
6 changes: 3 additions & 3 deletions test/EFCore.Specification.Tests/CustomConvertersTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con

b.Property(e => e.TestDateTimeOffset).HasConversion(
v => v.ToUnixTimeMilliseconds(),
v => DateTimeOffset.FromUnixTimeMilliseconds(v));
v => DateTimeOffset.FromUnixTimeMilliseconds(v).ToOffset(TimeSpan.FromHours(-8.0)));

b.Property(e => e.TestDouble).HasConversion(
new ValueConverter<double, decimal>(
Expand Down Expand Up @@ -941,7 +941,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con

b.Property(e => e.TestNullableDateTimeOffset).HasConversion(
v => v.Value.ToUnixTimeMilliseconds(),
v => (DateTimeOffset?)DateTimeOffset.FromUnixTimeMilliseconds(v));
v => (DateTimeOffset?)DateTimeOffset.FromUnixTimeMilliseconds(v).ToOffset(TimeSpan.FromHours(-8.0)));

b.Property(e => e.TestNullableDouble).HasConversion(
new ValueConverter<double?, decimal?>(
Expand Down Expand Up @@ -1005,7 +1005,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b.Property(nameof(BuiltInDataTypes.TestDateTimeOffset)).HasConversion(
new ValueConverter<DateTimeOffset, long>(
v => v.ToUnixTimeMilliseconds(),
v => DateTimeOffset.FromUnixTimeMilliseconds(v)));
v => DateTimeOffset.FromUnixTimeMilliseconds(v).ToOffset(TimeSpan.FromHours(-8.0))));

b.Property(nameof(BuiltInDataTypes.TestDouble)).HasConversion(
new ValueConverter<double, decimal>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3538,6 +3538,9 @@ public override bool SupportsLargeStringComparisons
public override bool SupportsDecimalComparisons
=> true;

public override bool PreservesDateTimeKind
=> false;

protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public override bool SupportsDecimalComparisons
public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> false;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base
.AddOptions(builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ public override bool SupportsDecimalComparisons
public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> false;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base
.AddOptions(builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ public override bool SupportsDecimalComparisons
public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> false;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base
.AddOptions(builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public override bool SupportsUnicodeToAnsiConversion
public override bool SupportsLargeStringComparisons
=> true;

public override bool PreservesDateTimeKind
=> false;

protected override string StoreName { get; } = "EverythingIsStrings";

protected override ITestStoreFactory TestStoreFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,9 @@ public override bool SupportsLargeStringComparisons
public override bool SupportsDecimalComparisons
=> false;

public override bool PreservesDateTimeKind
=> false;

protected override ITestStoreFactory TestStoreFactory
=> SqliteTestStoreFactory.Instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public override bool SupportsBinaryKeys

public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public override bool SupportsBinaryKeys

public override DateTime DefaultDateTime
=> new();

public override bool PreservesDateTimeKind
=> true;
}
}
}