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

Return the correct type mapping for enum properties. #16925

Merged
merged 1 commit into from
Aug 3, 2019
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 @@ -181,6 +181,12 @@ private static readonly MethodInfo _getItemMethodInfo
= typeof(JObject).GetTypeInfo().GetRuntimeProperties()
.Single(pi => pi.Name == "Item" && pi.GetIndexParameters()[0].ParameterType == typeof(string))
.GetMethod;
private static readonly PropertyInfo _jTokenTypePropertyInfo
= typeof(JToken).GetTypeInfo().GetRuntimeProperties()
.Single(mi => mi.Name == nameof(JToken.Type));
private static readonly MethodInfo _jTokenToObjectMethodInfo
= typeof(JToken).GetTypeInfo().GetRuntimeMethods()
.Single(mi => mi.Name == nameof(JToken.ToObject) && mi.GetParameters().Length == 0);
private static readonly MethodInfo _toObjectMethodInfo
= typeof(CosmosProjectionBindingRemovingExpressionVisitor).GetTypeInfo().GetRuntimeMethods()
.Single(mi => mi.Name == nameof(SafeToObject));
Expand All @@ -199,8 +205,8 @@ private readonly IDictionary<ParameterExpression, Expression> _materializationCo
= new Dictionary<ParameterExpression, Expression>();
private readonly IDictionary<Expression, ParameterExpression> _projectionBindings
= new Dictionary<Expression, ParameterExpression>();
private readonly IDictionary<Expression, (IEntityType EntityType, ParameterExpression JObjectVariable)> _ownerMappings
= new Dictionary<Expression, (IEntityType EntityType, ParameterExpression JObjectVariable)>();
private readonly IDictionary<Expression, (IEntityType EntityType, Expression JObjectExpression)> _ownerMappings
= new Dictionary<Expression, (IEntityType, Expression)>();
private (IEntityType EntityType, ParameterExpression JObjectVariable) _ownerInfo;
private ParameterExpression _ordinalParameter;

Expand Down Expand Up @@ -329,14 +335,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
(ParameterExpression)((MethodCallExpression)methodCallExpression.Arguments[0]).Object];
}

var readExpression = CreateGetValueExpression(innerExpression, property);
if (readExpression.Type.IsValueType
&& methodCallExpression.Type == typeof(object))
{
readExpression = Expression.Convert(readExpression, typeof(object));
}

return readExpression;
return CreateGetValueExpression(innerExpression, property, methodCallExpression.Type);
}

return base.VisitMethodCall(methodCallExpression);
Expand Down Expand Up @@ -381,6 +380,10 @@ protected override Expression VisitExtension(Expression extensionExpression)
{
_ownerMappings[objectArrayProjection.InnerProjection.AccessExpression] = _ownerInfo;
}
else
{
_ownerMappings[objectArrayProjection.InnerProjection.AccessExpression] = (objectArrayProjection.Navigation.DeclaringEntityType, objectArrayProjection.AccessExpression);
}

var previousOrdinalParameter = _ordinalParameter;
_ordinalParameter = ordinalParameter;
Expand Down Expand Up @@ -676,7 +679,8 @@ private static Expression CreateReadJTokenExpression(Expression jObjectExpressio

private Expression CreateGetValueExpression(
Expression jObjectExpression,
IProperty property)
IProperty property,
Type clrType)
{
if (property.Name == StoreKeyConvention.JObjectPropertyName)
{
Expand All @@ -697,7 +701,13 @@ private Expression CreateGetValueExpression(
&& !property.IsForeignKey()
&& property.ClrType == typeof(int))
{
return _ordinalParameter;
Expression readExpression = _ordinalParameter;
if (readExpression.Type != clrType)
{
readExpression = Expression.Convert(readExpression, clrType);
}

return readExpression;
}

var principalProperty = property.FindFirstPrincipal();
Expand All @@ -708,7 +718,7 @@ private Expression CreateGetValueExpression(
{
Debug.Assert(principalProperty.DeclaringEntityType.IsAssignableFrom(ownerInfo.EntityType));

ownerJObjectExpression = ownerInfo.JObjectVariable;
ownerJObjectExpression = ownerInfo.JObjectExpression;
}
else if (jObjectExpression is RootReferenceExpression rootReferenceExpression)
{
Expand All @@ -721,15 +731,15 @@ private Expression CreateGetValueExpression(

if (ownerJObjectExpression != null)
{
return CreateGetValueExpression(ownerJObjectExpression, principalProperty);
return CreateGetValueExpression(ownerJObjectExpression, principalProperty, clrType);
}
}
}

return Expression.Default(property.ClrType);
return Expression.Default(clrType);
}

return CreateGetValueExpression(jObjectExpression, storeName, property.ClrType, property.GetTypeMapping());
return CreateGetValueExpression(jObjectExpression, storeName, clrType, property.GetTypeMapping());
}

private Expression CreateGetValueExpression(
Expand Down Expand Up @@ -762,21 +772,39 @@ private Expression CreateGetValueExpression(
var converter = typeMapping?.Converter;
if (converter != null)
{
valueExpression = ConvertJTokenToType(jTokenExpression, converter.ProviderClrType);
var jTokenParameter = Expression.Parameter(typeof(JToken));

valueExpression = ReplacingExpressionVisitor.Replace(
converter.ConvertFromProviderExpression.Parameters.Single(),
valueExpression,
converter.ConvertFromProviderExpression.Body);
var body
= ReplacingExpressionVisitor.Replace(
converter.ConvertFromProviderExpression.Parameters.Single(),
Expression.Call(
jTokenParameter,
_jTokenToObjectMethodInfo.MakeGenericMethod(converter.ProviderClrType)),
converter.ConvertFromProviderExpression.Body);

if (valueExpression.Type != clrType)
if (body.Type != clrType)
{
valueExpression = Expression.Convert(valueExpression, clrType);
body = Expression.Convert(body, clrType);
}

body = Expression.Condition(
Expression.OrElse(
Expression.Equal(jTokenParameter, Expression.Default(typeof(JToken))),
Expression.Equal(Expression.MakeMemberAccess(jTokenParameter, _jTokenTypePropertyInfo),
Expression.Constant(JTokenType.Null))),
Expression.Default(clrType),
body);

valueExpression = Expression.Invoke(Expression.Lambda(body, jTokenParameter), jTokenExpression);
}
else
{
valueExpression = ConvertJTokenToType(jTokenExpression, clrType);
valueExpression = ConvertJTokenToType(jTokenExpression, typeMapping?.ClrType.MakeNullable() ?? clrType);

if (valueExpression.Type != clrType)
{
valueExpression = Expression.Convert(valueExpression, clrType);
}
}

return valueExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ protected override CoreTypeMapping FindMapping(in TypeMappingInfo mappingInfo)
return mapping;
}

if (clrType.IsValueType
if ((clrType.IsValueType
&& !clrType.IsEnum)
|| clrType == typeof(string))
{
return new CosmosTypeMapping(clrType);
Expand Down
5 changes: 1 addition & 4 deletions src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,9 @@ public virtual JObject UpdateDocument(JObject document, IUpdateEntry entry)
}

public virtual JObject GetCurrentDocument(IUpdateEntry entry)
{
var document = _jObjectProperty != null
=> _jObjectProperty != null
? (JObject)(entry.SharedIdentityEntry ?? entry).GetCurrentValue(_jObjectProperty)
: null;
return document;
}

private static JToken ConvertPropertyValue(IProperty property, object value)
{
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore/Query/Internal/QueryCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public virtual Func<QueryContext, TResult> CompileQueryCore<TResult>(
Expression query,
IModel model,
bool async)
{
return database.CompileQuery<TResult>(query, async);
}
=> database.CompileQuery<TResult>(query, async);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore/Storage/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ public abstract Task<int> SaveChangesAsync(
CancellationToken cancellationToken = default);

public virtual Func<QueryContext, TResult> CompileQuery<TResult>(Expression query, bool async)
{
return Dependencies.QueryCompilationContextFactory
=> Dependencies.QueryCompilationContextFactory
.Create(async)
.CreateQueryExecutor<TResult>(query);
}
}
}
Loading