Skip to content

Commit

Permalink
Fix issue with ToLowerInvariant() method in LiteDBRepository.cs Setup…
Browse files Browse the repository at this point in the history
… failed: Method ToLowerInvariant() in String are not supported when convert to BsonExpression #479
  • Loading branch information
support committed Apr 7, 2024
1 parent a864d1c commit e0c1d67
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ public virtual async Task<T> GetByIdAsync(string id)
/// <returns>Entity</returns>
public virtual Task<T> GetOneAsync(Expression<Func<T, bool>> predicate)
{
return Task.FromResult(_collection.Find(predicate).FirstOrDefault());
var visitor = new ToLowerInvariantVisitor();
//WORKAROUND Issue #479 - Method ToLowerInvariant() in String are not supported when convert to BsonExpression
var modifiedExpression = (Expression<Func<T, bool>>)visitor.Visit(predicate);
return Task.FromResult(_collection.Find(modifiedExpression).FirstOrDefault());
}

/// <summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Core/Grand.Data/LiteDb/ToLowerInvariantVisitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq.Expressions;

namespace Grand.Data.LiteDb;
public class ToLowerInvariantVisitor : ExpressionVisitor
{
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method == typeof(string).GetMethod("ToLowerInvariant", Type.EmptyTypes))
{
var invariantMethod = typeof(string).GetMethod("ToLower", Type.EmptyTypes);
if (invariantMethod != null)
{
return Expression.Call(node.Object, invariantMethod);
}

}
return base.VisitMethodCall(node);
}
}
12 changes: 11 additions & 1 deletion src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task GetByIdAsync_LiteRepository_Success()
}

[TestMethod()]
public async Task GetOneAsyncAsync_LiteRepository_Success()
public async Task GetOneAsync_LiteRepository_Success()
{
var myRepository = new LiteDBRepositoryMock<SampleCollection>();
var product = new SampleCollection { Id = "1" };
Expand All @@ -76,7 +76,17 @@ public async Task GetOneAsyncAsync_LiteRepository_Success()

Assert.IsNotNull(p);
}
[TestMethod()]
public async Task GetOneAsync_ToLowerInvariant_LiteRepository_Success()
{
var myRepository = new LiteDBRepositoryMock<SampleCollection>();
var product = new SampleCollection { Id = "1" };
await myRepository.InsertAsync(product);

var p = await myRepository.GetOneAsync(x => x.Id == "1".ToLowerInvariant());

Assert.IsNotNull(p);
}
[TestMethod()]
public async Task ClearAsync_LiteRepository_Success()
{
Expand Down

0 comments on commit e0c1d67

Please sign in to comment.