-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for some minor bugs around JSON mappings
#29214 - Json column: constructor with optional enum-parameter throws ArgumentException When producing shaper code for json nullable properties with converters we check if the converter handles null, if not we need to peek into the json value and only pass it thru converter if it's not null, and return null otherwise #29217 - Json: nullable enums on entities mapped to json don't use enum-to-int converter, rather than string, like for non-nullable enums In convention code unwrap nullable type when testing for enum properties that we then apply converter to string #29225 - Json: projecting nullable scalar produces debug.assert in SqlExpression ctor Unwrap nullable type from property before passing it as type to SqlExpression ctor #29219 - Json: error when trying to materialize json entity with nullable property that is missing in the json string - should materialize the property as null instead Modify ExtractJsonProperty method so that it used TryGetProperty when trying to extract nullable properties. Fixes #29214 Fixes #29217 Fixes #29225 Fixes #29219
- Loading branch information
Showing
15 changed files
with
918 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
test/EFCore.Relational.Specification.Tests/Query/JsonQueryAdHocTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query; | ||
|
||
public abstract class JsonQueryAdHocTestBase : NonSharedModelTestBase | ||
{ | ||
protected JsonQueryAdHocTestBase(ITestOutputHelper testOutputHelper) | ||
{ | ||
//TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper); | ||
} | ||
|
||
protected override string StoreName | ||
=> "JsonQueryAdHocTest"; | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public virtual async Task Optional_json_properties_materialized_as_null_when_the_element_in_json_is_not_present(bool async) | ||
{ | ||
var contextFactory = await InitializeAsync<MyContext29219>(seed: Seed29219); | ||
using (var context = contextFactory.CreateContext()) | ||
{ | ||
var query = context.Entities.Where(x => x.Id == 3); | ||
|
||
var result = async | ||
? await query.SingleAsync() | ||
: query.Single(); | ||
|
||
Assert.Equal(3, result.Id); | ||
Assert.Equal(null, result.Reference.NullableScalar); | ||
Assert.Equal(null, result.Collection[0].NullableScalar); | ||
} | ||
} | ||
|
||
[ConditionalTheory] | ||
[MemberData(nameof(IsAsyncData))] | ||
public virtual async Task Can_project_nullable_json_property_when_the_element_in_json_is_not_present(bool async) | ||
{ | ||
var contextFactory = await InitializeAsync<MyContext29219>(seed: Seed29219); | ||
using (var context = contextFactory.CreateContext()) | ||
{ | ||
var query = context.Entities.OrderBy(x => x.Id).Select(x => x.Reference.NullableScalar); | ||
|
||
var result = async | ||
? await query.ToListAsync() | ||
: query.ToList(); | ||
|
||
Assert.Equal(3, result.Count); | ||
Assert.Equal(11, result[0]); | ||
Assert.Equal(null, result[1]); | ||
Assert.Equal(null, result[2]); | ||
} | ||
} | ||
|
||
protected abstract void Seed29219(MyContext29219 ctx); | ||
|
||
protected class MyContext29219 : DbContext | ||
{ | ||
public MyContext29219(DbContextOptions options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
public DbSet<MyEntity29219> Entities { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<MyEntity29219>().Property(x => x.Id).ValueGeneratedNever(); | ||
modelBuilder.Entity<MyEntity29219>().OwnsOne(x => x.Reference).ToJson(); | ||
modelBuilder.Entity<MyEntity29219>().OwnsMany(x => x.Collection).ToJson(); | ||
} | ||
} | ||
|
||
public class MyEntity29219 | ||
{ | ||
public int Id { get; set; } | ||
public MyJsonEntity29219 Reference { get; set; } | ||
public List<MyJsonEntity29219> Collection { get; set; } | ||
} | ||
|
||
public class MyJsonEntity29219 | ||
{ | ||
public int NonNullableScalar { get; set; } | ||
public int? NullableScalar { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.