(bugfix): fix possible null ref exception

This commit is contained in:
Maksym Sadovnychyy 2024-10-18 20:35:40 +02:00
parent f463862b7c
commit fd799615f7
4 changed files with 16 additions and 25 deletions

View File

@ -10,7 +10,7 @@ public class ExpressionExtensionsTests {
public void CombineWith_ShouldCombineTwoPredicates() {
// Arrange
Expression<Func<TestEntity, bool>> firstPredicate = x => x.Age > 18;
Expression<Func<TestEntity, bool>> secondPredicate = x => x.Name.StartsWith("A");
Expression<Func<TestEntity, bool>> secondPredicate = x => (x.Name ?? "").StartsWith("A");
// Act
var combinedPredicate = firstPredicate.CombineWith(secondPredicate);
@ -25,7 +25,7 @@ public class ExpressionExtensionsTests {
private class TestEntity {
public Guid Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public string? Name { get; set; }
}
}

View File

@ -4,17 +4,6 @@ using MaksIT.Core.Webapi.Models;
namespace MaksIT.Core.Tests.Webapi.Models;
public class PagedRequestTests {
[Fact]
public void BuildFilterExpression_ShouldReturnNull_WhenFilterIsEmpty() {
// Arrange
var request = new PagedRequest();
// Act
var result = request.BuildFilterExpression<TestEntity>(null);
// Assert
Assert.Null(result);
}
[Fact]
public void BuildFilterExpression_ShouldHandleEqualsOperator() {
@ -131,6 +120,6 @@ public class PagedRequestTests {
// Helper class for testing purposes
public class TestEntity {
public string Name { get; set; }
public string? Name { get; set; }
public int Age { get; set; }
}

View File

@ -8,7 +8,7 @@
<!-- NuGet package metadata -->
<PackageId>MaksIT.Core</PackageId>
<Version>1.1.3</Version>
<Version>1.1.4</Version>
<Authors>Maksym Sadovnychyy</Authors>
<Company>MAKS-IT</Company>
<Product>MaksIT.Core</Product>

View File

@ -91,7 +91,8 @@ namespace MaksIT.Core.Webapi.Models {
expression = Expression.Not(expression);
}
// Combine the current expression with the previous one using the correct operator
// Only combine expressions if the new expression is not null
if (expression != null) {
if (combinedExpression == null) {
combinedExpression = expression;
}
@ -103,6 +104,7 @@ namespace MaksIT.Core.Webapi.Models {
: Expression.OrElse(combinedExpression, expression);
}
}
}
return combinedExpression != null
? Expression.Lambda<Func<T, bool>>(combinedExpression, parameter)