Skip to content

added validation for filtering and sorting in ReplaceAliasesWithPrope… #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions QueryKit/QueryKitPropertyMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,22 @@ public QueryKitPropertyMapping<TModel> DerivedProperty<TModel>(Expression<Func<T
public string ReplaceAliasesWithPropertyPaths(string input)
{
var operators = ComparisonOperator.List.Select(x => x.Operator()).ToList();
foreach (var alias in _propertyMappings.Values)

foreach (QueryKitPropertyInfo queryKitPropertyInfo in _propertyMappings.Values)
{
var propertyPath = GetPropertyPathByQueryName(alias.QueryName);
var propertyPath = GetPropertyPathByQueryName(queryKitPropertyInfo.QueryName);
if (!string.IsNullOrEmpty(propertyPath))
{
foreach (var op in operators)
{
// Use regular expression to isolate left side of the expression
var regex = new Regex($@"\b{alias.QueryName}\b(?=\s*{op})", RegexOptions.IgnoreCase);
var regex = new Regex($@"\b{queryKitPropertyInfo.QueryName}\b(?=\s*{op})", RegexOptions.IgnoreCase);

if (queryKitPropertyInfo is { CanSort: false, CanFilter: false} && regex.IsMatch(input))
{
throw new InvalidOperationException($"'{queryKitPropertyInfo.Name}' is not allowed for filtering or sorting.");
}

input = regex.Replace(input, propertyPath);
}
}
Expand Down