Add regex match option for StringFilter#8178
Conversation
|
Thanks for your PR, @MaoTouZhu. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a new Sequence diagram for applying Regex StringFiltersequenceDiagram
actor User
participant StringFilter
participant QueryBuilder
participant LambdaExtensions
participant Regex
User->>StringFilter: OnParametersSet()
StringFilter->>StringFilter: SelectedFilterItems add Regex
User->>QueryBuilder: select Regex filter and value
QueryBuilder->>LambdaExtensions: GetExpression(filter)
LambdaExtensions->>LambdaExtensions: RegexMatch(left, right)
LambdaExtensions->>Regex: IsMatch(left, right)
Regex-->>LambdaExtensions: bool
LambdaExtensions-->>QueryBuilder: Expression
QueryBuilder-->>User: filtered results based on Regex
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The new
RegexMatchexpression doesn’t guard againstleftbeing null (unlikeContainsWidthComparison), which could lead toNullReferenceExceptions at runtime; consider adding a null check similar to theContainsimplementation. - Using a user-provided pattern directly in
Regex.IsMatchcan throwArgumentExceptionon invalid patterns; consider validating or safely handling invalid regex patterns before constructing the expression to avoid runtime failures.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `RegexMatch` expression doesn’t guard against `left` being null (unlike `ContainsWidthComparison`), which could lead to `NullReferenceException`s at runtime; consider adding a null check similar to the `Contains` implementation.
- Using a user-provided pattern directly in `Regex.IsMatch` can throw `ArgumentException` on invalid patterns; consider validating or safely handling invalid regex patterns before constructing the expression to avoid runtime failures.
## Individual Comments
### Comment 1
<location path="src/BootstrapBlazor/Extensions/LambdaExtensions.cs" line_range="245-248" />
<code_context>
FilterAction.LessThanOrEqual => Expression.LessThanOrEqual(left, right),
FilterAction.Contains => left.Contains(right, comparison),
FilterAction.NotContains => Expression.Not(left.Contains(right, comparison)),
+ FilterAction.Regex => left.RegexMatch(right),
_ => filter.FieldValue switch
{
</code_context>
<issue_to_address>
**issue (bug_risk):** Regex matching does not guard against null values, unlike the Contains branch.
The `ContainsWidthComparison` helper wraps its call in a null-check (`left != null && ...`), but the new `FilterAction.Regex` path calls `left.RegexMatch(right)` without any guard. If `left` is null or not a string, this can throw at runtime. Please add consistent null-handling (and any needed string conversion) in the regex branch to match the other string-based filters.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| FilterAction.Regex => left.RegexMatch(right), | ||
| _ => filter.FieldValue switch | ||
| { | ||
| LambdaExpression t => Expression.Invoke(t, left), |
There was a problem hiding this comment.
issue (bug_risk): Regex matching does not guard against null values, unlike the Contains branch.
The ContainsWidthComparison helper wraps its call in a null-check (left != null && ...), but the new FilterAction.Regex path calls left.RegexMatch(right) without any guard. If left is null or not a string, this can throw at runtime. Please add consistent null-handling (and any needed string conversion) in the regex branch to match the other string-based filters.
|
@microsoft-github-policy-service agree |
Link issues
fixes #8177
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Add support for regex-based string filtering in query components.
New Features:
Enhancements: