(bugfix): case insensitive comparison predicate should lower case only string type
This commit is contained in:
parent
85f1d7c89f
commit
0c12bdc899
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<!-- NuGet package metadata -->
|
<!-- NuGet package metadata -->
|
||||||
<PackageId>MaksIT.Core</PackageId>
|
<PackageId>MaksIT.Core</PackageId>
|
||||||
<Version>1.3.1</Version>
|
<Version>1.3.2</Version>
|
||||||
<Authors>Maksym Sadovnychyy</Authors>
|
<Authors>Maksym Sadovnychyy</Authors>
|
||||||
<Company>MAKS-IT</Company>
|
<Company>MAKS-IT</Company>
|
||||||
<Product>MaksIT.Core</Product>
|
<Product>MaksIT.Core</Product>
|
||||||
|
|||||||
@ -15,16 +15,35 @@ public class PagedRequest : RequestModelBase {
|
|||||||
if (string.IsNullOrWhiteSpace(Filters))
|
if (string.IsNullOrWhiteSpace(Filters))
|
||||||
return x => true; // Returns an expression that doesn't filter anything.
|
return x => true; // Returns an expression that doesn't filter anything.
|
||||||
|
|
||||||
// Adjust Filters to make Contains, StartsWith, EndsWith, ==, and != case-insensitive
|
// Get the type of T
|
||||||
string adjustedFilters = Filters
|
var type = typeof(T);
|
||||||
.Replace(".Contains(", ".ToLower().Contains(")
|
|
||||||
.Replace(".StartsWith(", ".ToLower().StartsWith(")
|
|
||||||
.Replace(".EndsWith(", ".ToLower().EndsWith(")
|
|
||||||
.Replace("==", ".ToLower() ==")
|
|
||||||
.Replace("!=", ".ToLower() !=");
|
|
||||||
|
|
||||||
// Ensure values are also transformed to lowercase
|
// Adjust Filters to make Contains, StartsWith, EndsWith, ==, and != case-insensitive
|
||||||
adjustedFilters = Regex.Replace(adjustedFilters, "\"([^\"]+)\"", m => $"\"{m.Groups[1].Value.ToLower()}\"");
|
string adjustedFilters = Filters;
|
||||||
|
|
||||||
|
// Regex to find property names and methods
|
||||||
|
adjustedFilters = Regex.Replace(adjustedFilters, @"(\w+)\.(Contains|StartsWith|EndsWith)\(\""(.*?)\""\)", m => {
|
||||||
|
var propertyName = m.Groups[1].Value;
|
||||||
|
var method = m.Groups[2].Value;
|
||||||
|
var value = m.Groups[3].Value;
|
||||||
|
var property = type.GetProperty(propertyName);
|
||||||
|
if (property != null && property.PropertyType == typeof(string)) {
|
||||||
|
return $"{propertyName}.ToLower().{method}(\"{value.ToLower()}\")";
|
||||||
|
}
|
||||||
|
return m.Value;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Regex to find equality and inequality comparisons
|
||||||
|
adjustedFilters = Regex.Replace(adjustedFilters, @"(\w+)\s*(==|!=)\s*\""(.*?)\""", m => {
|
||||||
|
var propertyName = m.Groups[1].Value;
|
||||||
|
var comparison = m.Groups[2].Value;
|
||||||
|
var value = m.Groups[3].Value;
|
||||||
|
var property = type.GetProperty(propertyName);
|
||||||
|
if (property != null && property.PropertyType == typeof(string)) {
|
||||||
|
return $"{propertyName}.ToLower() {comparison} \"{value.ToLower()}\"";
|
||||||
|
}
|
||||||
|
return m.Value;
|
||||||
|
});
|
||||||
|
|
||||||
// Parse the adjusted filter string into a dynamic lambda expression
|
// Parse the adjusted filter string into a dynamic lambda expression
|
||||||
var predicate = DynamicExpressionParser.ParseLambda<T, bool>(
|
var predicate = DynamicExpressionParser.ParseLambda<T, bool>(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user