From fd799615f79d82f2276a3f17af325fe5ac1871ad Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Fri, 18 Oct 2024 20:35:40 +0200 Subject: [PATCH] (bugfix): fix possible null ref exception --- .../Extensions/ExpressionExtensionsTests.cs | 4 ++-- .../Webapi/Models/PagedRequestTests.cs | 13 +---------- src/MaksIT.Core/MaksIT.Core.csproj | 2 +- src/MaksIT.Core/Webapi/Models/PagedRequest.cs | 22 ++++++++++--------- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/MaksIT.Core.Tests/Extensions/ExpressionExtensionsTests.cs b/src/MaksIT.Core.Tests/Extensions/ExpressionExtensionsTests.cs index 3a1b58f..7998c52 100644 --- a/src/MaksIT.Core.Tests/Extensions/ExpressionExtensionsTests.cs +++ b/src/MaksIT.Core.Tests/Extensions/ExpressionExtensionsTests.cs @@ -10,7 +10,7 @@ public class ExpressionExtensionsTests { public void CombineWith_ShouldCombineTwoPredicates() { // Arrange Expression> firstPredicate = x => x.Age > 18; - Expression> secondPredicate = x => x.Name.StartsWith("A"); + Expression> 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; } } } diff --git a/src/MaksIT.Core.Tests/Webapi/Models/PagedRequestTests.cs b/src/MaksIT.Core.Tests/Webapi/Models/PagedRequestTests.cs index 685d632..d84b14d 100644 --- a/src/MaksIT.Core.Tests/Webapi/Models/PagedRequestTests.cs +++ b/src/MaksIT.Core.Tests/Webapi/Models/PagedRequestTests.cs @@ -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(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; } } \ No newline at end of file diff --git a/src/MaksIT.Core/MaksIT.Core.csproj b/src/MaksIT.Core/MaksIT.Core.csproj index 35944e1..d28e5ee 100644 --- a/src/MaksIT.Core/MaksIT.Core.csproj +++ b/src/MaksIT.Core/MaksIT.Core.csproj @@ -8,7 +8,7 @@ MaksIT.Core - 1.1.3 + 1.1.4 Maksym Sadovnychyy MAKS-IT MaksIT.Core diff --git a/src/MaksIT.Core/Webapi/Models/PagedRequest.cs b/src/MaksIT.Core/Webapi/Models/PagedRequest.cs index a8d5541..bb3b4df 100644 --- a/src/MaksIT.Core/Webapi/Models/PagedRequest.cs +++ b/src/MaksIT.Core/Webapi/Models/PagedRequest.cs @@ -91,16 +91,18 @@ namespace MaksIT.Core.Webapi.Models { expression = Expression.Not(expression); } - // Combine the current expression with the previous one using the correct operator - if (combinedExpression == null) { - combinedExpression = expression; - } - else if (i - 1 < operators.Count) // Ensure we don't exceed the operators list size - { - var operatorType = operators[i - 1]; - combinedExpression = operatorType == "&&" - ? Expression.AndAlso(combinedExpression, expression) - : Expression.OrElse(combinedExpression, expression); + // Only combine expressions if the new expression is not null + if (expression != null) { + if (combinedExpression == null) { + combinedExpression = expression; + } + else if (i - 1 < operators.Count) // Ensure we don't exceed the operators list size + { + var operatorType = operators[i - 1]; + combinedExpression = operatorType == "&&" + ? Expression.AndAlso(combinedExpression, expression) + : Expression.OrElse(combinedExpression, expression); + } } }