Skip to content

Commit

Permalink
Query: Make ExtractJsonProperty generic on return type and resilient …
Browse files Browse the repository at this point in the history
…with missing properties

Resolves #29219
  • Loading branch information
smitpatel committed Sep 29, 2022
1 parent 98bd7ab commit 05b5c4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ private static TValue ThrowExtractJsonPropertyException<TValue>(
exception);
}

private static object? ExtractJsonProperty(JsonElement element, string propertyName, Type returnType)
{
var jsonElementProperty = element.GetProperty(propertyName);

return jsonElementProperty.Deserialize(returnType);
}
private static T? ExtractJsonProperty<T>(JsonElement element, string propertyName, bool nullable)
=> nullable
? element.TryGetProperty(propertyName, out var jsonValue)
? jsonValue.Deserialize<T>()
: default
: element.GetProperty(propertyName).Deserialize<T>();

private static void IncludeReference<TEntity, TIncludingEntity, TIncludedEntity>(
QueryContext queryContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1501,19 +1501,15 @@ private Expression CreateExtractJsonPropertyExpression(
ParameterExpression jsonElementParameter,
IProperty property)
{
var nullable = property.IsNullable;
Expression resultExpression;
if (property.GetTypeMapping().Converter is ValueConverter converter)
{
resultExpression = Expression.Call(
ExtractJsonPropertyMethodInfo,
ExtractJsonPropertyMethodInfo.MakeGenericMethod(converter.ProviderClrType.MakeNullable(nullable)),
jsonElementParameter,
Expression.Constant(property.GetJsonPropertyName()),
Expression.Constant(converter.ProviderClrType));

if (resultExpression.Type != converter.ProviderClrType)
{
resultExpression = Expression.Convert(resultExpression, converter.ProviderClrType);
}
Expression.Constant(nullable));

resultExpression = ReplacingExpressionVisitor.Replace(
converter.ConvertFromProviderExpression.Parameters.Single(),
Expand All @@ -1522,13 +1518,11 @@ private Expression CreateExtractJsonPropertyExpression(
}
else
{
resultExpression = Expression.Convert(
Expression.Call(
ExtractJsonPropertyMethodInfo,
jsonElementParameter,
Expression.Constant(property.GetJsonPropertyName()),
Expression.Constant(property.ClrType)),
property.ClrType);
resultExpression = Expression.Call(
ExtractJsonPropertyMethodInfo.MakeGenericMethod(property.ClrType),
jsonElementParameter,
Expression.Constant(property.GetJsonPropertyName()),
Expression.Constant(nullable));
}

if (_detailedErrorsEnabled)
Expand Down

0 comments on commit 05b5c4a

Please sign in to comment.