Skip to content

Commit

Permalink
Merge pull request #281 from LuccaSA/FIX_null_ref_on_filter
Browse files Browse the repository at this point in the history
FIX rare null refex coming from EF
  • Loading branch information
Poltuu authored Nov 19, 2018
2 parents 6a8d497 + 1bfe5a2 commit e812358
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Rdd.Infra/Helpers/ExpressionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public static Expression ExtractExpression<T>(this T value)
{
throw new NotSupportedException("Only use typed values");
}

if (value == null)
{
return Expression.Constant(null, typeof(T));
}

return ((Expression<Func<T>>)(() => value)).Body;
}

Expand All @@ -20,4 +26,4 @@ public static Expression ExtractTypedExpression(this object value, Type type)

private static readonly MethodInfo _extractMethodInfo = typeof(ExpressionExtension).GetMethod(nameof(ExtractExpression), BindingFlags.Public | BindingFlags.Static);
}
}
}
3 changes: 3 additions & 0 deletions test/Rdd.Domain.Tests/Models/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public DbSet<Department> Departments { get; }
public DbSet<UserWithParameters> UserWithParameters { get; }

public DbSet<Parent> Parents { get; }
public DbSet<OptionalChild> OptionalChildren { get; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ConcreteClassThree>();
Expand Down
11 changes: 11 additions & 0 deletions test/Rdd.Domain.Tests/Models/OptionalChild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Rdd.Domain.Tests.Models
{
public class OptionalChild
{
public int Id { get; set; }
public int? ParentId { get; set; }
public Parent Parent { get; set; }

public string Name { get; set; }
}
}
14 changes: 14 additions & 0 deletions test/Rdd.Domain.Tests/Models/Parent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Rdd.Domain.Tests.Models
{
public class Parent : IEntityBase<int>
{
public int Id { get; set; }
public string Name { get; set; }

public OptionalChild OptionalChild { get; set; }

public string Url { get; }

public object GetId() => Id;
}
}
31 changes: 31 additions & 0 deletions test/Rdd.Web.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Rdd.Domain.Helpers;
using Rdd.Domain.Helpers.Reflection;
using Rdd.Domain.Models;
using Rdd.Domain.Patchers;
using Rdd.Domain.Tests;
using Rdd.Domain.Tests.Models;
using Rdd.Infra.Storage;
Expand Down Expand Up @@ -39,5 +42,33 @@ await RunCodeInsideIsolatedDatabaseAsync(async (context) =>
Assert.Equal("John Doe 3", result.FirstOrDefault().Name);
}, true);
}

//ce test vérifie que la manière dont est convertie les filtres menant à un left join + test de nullité de clé fonctionne jusque dans EF
[Theory]
[InlineData(nameof(Parent.OptionalChild), "notequal,null")]
[InlineData(nameof(Parent.OptionalChild), "null")]
[InlineData(nameof(Parent.OptionalChild) + "." + nameof(OptionalChild.Name), "notequal,null")]
[InlineData(nameof(Parent.OptionalChild) + "." + nameof(OptionalChild.Name), "null")]
[InlineData(nameof(Parent.OptionalChild) + "." + nameof(OptionalChild.Name), "value")]
public async void LeftJoinShouldWork(string key, string value)
{
await RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var unitOfWork = new UnitOfWork(context);
context.Add(new Parent { OptionalChild = new OptionalChild { Name = "value" } });
context.Add(new Parent());
await unitOfWork.SaveChangesAsync();

var request = HttpVerbs.Get.NewRequest((key, value));
var query = QueryParserHelper.GetQueryParser<Parent>().Parse(request, true);

var storage = new EFStorageService(context);
var repo = new OpenRepository<Parent>(storage, null);
var collection = new RestCollection<Parent, int>(repo, new ObjectPatcher<Parent>(_fixture.PatcherProvider, new ReflectionHelper()), new DefaultInstanciator<Parent>());
var result = (await collection.GetAsync(query)).Items;

Assert.Single(result);
}, true);
}
}
}

0 comments on commit e812358

Please sign in to comment.