Skip to content

Fix for clear special characters#4463

Open
Anushka1783 wants to merge 2 commits intomasterfrom
bugfix/clearspecialcharacters
Open

Fix for clear special characters#4463
Anushka1783 wants to merge 2 commits intomasterfrom
bugfix/clearspecialcharacters

Conversation

@Anushka1783
Copy link
Copy Markdown
Collaborator

@Anushka1783 Anushka1783 commented Mar 11, 2026

Description

Type of Change

  • Bug fix - [ ] New feature - [ ] Breaking change - [ ] Plugin update

Checklist

  • PR description clearly describes the changes
  • Target branch is correct (master for features, Releases/* for fixes)
  • Latest code from target branch merged
  • No commented/junk code included
  • No new build warnings or errors
  • All existing unit tests pass
  • New unit tests added for new functionality
  • Cross-platform compatibility verified (Windows/Linux/macOS)
  • CI/CD pipeline passes
  • Code follows project conventions (Act{Platform}{Type}, {Platform}Driver)
  • Repository objects use [IsSerializedForLocalRepository] where needed
  • Error handling uses Reporter.ToLog() pattern
  • Documentation updated for user-facing changes
  • Self-review completed and code review comments addressed

Summary by CodeRabbit

  • Refactor
    • Improved performance and consistency of the special-character removal step by switching to a pattern-based cleaning approach that operates on the variable value directly, preserving external behavior and public interfaces.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 38c49234-0041-4647-a599-aa05ab78115e

📥 Commits

Reviewing files that changed from the base of the PR and between 45c10bd and f497ed8.

📒 Files selected for processing (1)
  • Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs

Walkthrough

Replaces iterative character removal in ActSetVariableValue.ClearSpecialChar with a single Regex.Replace call, adding a System.Text.RegularExpressions using and operating on a local VariableString reference. No public API or control flow changes.

Changes

Cohort / File(s) Summary
ClearSpecialChar Logic Refactor
Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs
Replaced per-character removal with Regex.Replace using pattern [^A-Za-z0-9\s]. Added using System.Text.RegularExpressions;. Uses a local VariableString variable for replacement; no public signature changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A regex hop to trim the fluff,
One swift replace, no loops too rough,
Special chars vanish in a blink,
This rabbit cheers — no more ink! 🎉

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is incomplete with no actual content filled in—the Description section is empty, no Type of Change is selected, and all checklist items remain unchecked. Fill in the Description section with details about the regex-based special character removal, select 'Bug fix' as the Type of Change, and mark relevant checklist items as completed.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix for clear special characters' directly addresses the main change: refactoring special character removal in ActSetVariableValue.Execute using regex instead of per-character removal.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/clearspecialcharacters

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Refactor regex to remove non-alphanumeric characters from variable value.
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs`:
- Around line 215-217: The comment and implementation disagree: the
Regex.Replace call (the assignment to variable.Value using Regex.Replace) claims
to exclude Unicode letters/digits but uses ASCII-only character classes
[A-Za-z0-9]; either replace the pattern with a Unicode-aware pattern using \p{L}
and \p{N} (i.e., use [^\p{L}\p{N}\s]) to truly allow all Unicode letters/digits,
or if ASCII-only behavior is intended, update the comment to state "ASCII
letters and digits only" so the comment matches the Regex.Replace usage.
- Line 211: The code directly casts Var to VariableString with
(VariableString)Var which can throw if Var isn't that type; update the usage in
ActSetVariableValue (and the line where variable = (VariableString)Var is set)
to perform a type guard (e.g., if (Var is VariableString variable) { ... } or
var variable = Var as VariableString; if (variable == null) { /* handle: skip
ClearSpecialChar, log warning or throw a descriptive exception */ }) and only
call VariableString-specific members like ClearSpecialChar when the guard
succeeds to match the existing pattern used elsewhere in this class.
- Around line 209-219: The ClearSpecialChar branch uses an unsafe cast
(VariableString)Var and unconditionally strips all non-alphanumeric chars;
change it to mirror the earlier pattern by first checking if (Var.GetType() ==
typeof(VariableString)) before casting to VariableString, and then restore the
original selective-removal behavior by building the Regex using the
user-specified characters from variable.Value (escape them for regex) rather
than using @"[^A-Za-z0-9\s]"; ensure you only remove the characters listed in
variable.Value when SetVariableValueOption ==
VariableBase.eSetValueOptions.ClearSpecialChar and leave other characters
intact.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 011c88ba-aabf-445d-89bf-e20c10d69a3c

📥 Commits

Reviewing files that changed from the base of the PR and between 6f5e65a and 45c10bd.

📒 Files selected for processing (1)
  • Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs

Comment on lines 209 to 219
else if (SetVariableValueOption == VariableBase.eSetValueOptions.ClearSpecialChar)
{
string specChar = ValueExpression.Calculate(this.Value);
if (string.IsNullOrEmpty(specChar))
{
specChar = @"{}(),\""";
}
if (!string.IsNullOrEmpty(((VariableString)Var).Value))
var variable = (VariableString)Var;

if (!string.IsNullOrEmpty(variable.Value))
{
foreach (char c in specChar)
{
((VariableString)Var).Value = ((VariableString)Var).Value.Replace(c.ToString(), "");
}
// Remove any character that is NOT a Unicode letter, NOT a Unicode digit, and not whitespace.
// Example: "Wlcome@#\"$%Amdocs123" -> "WlcomeAmdocs123"
variable.Value = Regex.Replace(variable.Value, @"[^A-Za-z0-9\s]", string.Empty);

}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Find and display the test that exercises ClearSpecialChar to verify behavioral expectations

# Search for the test method
rg -n -A 30 'TestVariable_StringClearSpecialChar' --type cs

Repository: Ginger-Automation/Ginger

Length of output: 3596


🏁 Script executed:

#!/bin/bash
# Check for type casting patterns in ActSetVariableValue.cs to see if casts are typically guarded

rg -n '\(VariableString\)Var|\(VariableString\)|\bVar\s+as\s+VariableString' Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs -B 2 -A 0

Repository: Ginger-Automation/Ginger

Length of output: 505


Fix unsafe cast and behavioral inconsistency with ClearSpecialChar option.

Two issues in lines 209-219:

  1. Unsafe type cast (line 211): The direct cast (VariableString)Var bypasses type validation. Just 85 lines earlier, line 124 demonstrates the pattern used in this same file: guard with if (Var.GetType() == typeof(VariableString)) before casting.

  2. Breaking behavioral change: The new regex [^A-Za-z0-9\s] removes all non-alphanumeric characters, ignoring the Value property entirely. The test TestVariable_StringClearSpecialChar() passes only by coincidence—the test input "S$tr!ing@123#" contains exactly the special chars "$@#!" specified for removal, so both approaches yield "String123". The underlying feature contract has changed: selective removal of user-specified characters → unconditional removal of all special characters. If this is intentional, the test expectations and documentation need updating. If selective removal should be preserved, the regex must incorporate the user-specified characters from Value.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs` around lines
209 - 219, The ClearSpecialChar branch uses an unsafe cast (VariableString)Var
and unconditionally strips all non-alphanumeric chars; change it to mirror the
earlier pattern by first checking if (Var.GetType() == typeof(VariableString))
before casting to VariableString, and then restore the original
selective-removal behavior by building the Regex using the user-specified
characters from variable.Value (escape them for regex) rather than using
@"[^A-Za-z0-9\s]"; ensure you only remove the characters listed in
variable.Value when SetVariableValueOption ==
VariableBase.eSetValueOptions.ClearSpecialChar and leave other characters
intact.

specChar = @"{}(),\""";
}
if (!string.IsNullOrEmpty(((VariableString)Var).Value))
var variable = (VariableString)Var;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add type check before casting to VariableString.

The direct cast (VariableString)Var will throw InvalidCastException if Var is not a VariableString. While VariableString is the only type that declares ClearSpecialChar as supported, this file consistently uses explicit type checks before casting (see lines 124, 128, 138, 151, 155, 177, 232, 245, 258, 270, 287).

🛡️ Proposed fix to add type guard
 else if (SetVariableValueOption == VariableBase.eSetValueOptions.ClearSpecialChar)
 {
-    var variable = (VariableString)Var;
-
-    if (!string.IsNullOrEmpty(variable.Value))
+    if (Var.GetType() == typeof(VariableString))
     {
-        // Remove any character that is NOT a Unicode letter, NOT a Unicode digit, and not whitespace.
-        // Example: "Wlcome@#\"$%Amdocs123" -> "WlcomeAmdocs123"
-        variable.Value = Regex.Replace(variable.Value, @"[^A-Za-z0-9\s]", string.Empty);
-        
+        var variable = (VariableString)Var;
+        if (!string.IsNullOrEmpty(variable.Value))
+        {
+            // Remove characters based on user specification or all special chars
+            variable.Value = Regex.Replace(variable.Value, @"[^A-Za-z0-9\s]", string.Empty);
+        }
+    }
+    else
+    {
+        Error = "Operation type " + SetVariableValueOption + " is not supported for " + GingerDicser.GetTermResValue(eTermResKey.Variable) + " of type " + Var.GetType();
+        return;
     }
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs` at line 211,
The code directly casts Var to VariableString with (VariableString)Var which can
throw if Var isn't that type; update the usage in ActSetVariableValue (and the
line where variable = (VariableString)Var is set) to perform a type guard (e.g.,
if (Var is VariableString variable) { ... } or var variable = Var as
VariableString; if (variable == null) { /* handle: skip ClearSpecialChar, log
warning or throw a descriptive exception */ }) and only call
VariableString-specific members like ClearSpecialChar when the guard succeeds to
match the existing pattern used elsewhere in this class.

Comment on lines +215 to +217
// Remove any character that is NOT a Unicode letter, NOT a Unicode digit, and not whitespace.
// Example: "Wlcome@#\"$%Amdocs123" -> "WlcomeAmdocs123"
variable.Value = Regex.Replace(variable.Value, @"[^A-Za-z0-9\s]", string.Empty);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Comment is inaccurate: regex matches ASCII only, not Unicode.

The comment states "NOT a Unicode letter, NOT a Unicode digit" but the regex [A-Za-z0-9] only matches ASCII letters and digits. For true Unicode support, use \p{L} (Unicode letters) and \p{N} (Unicode digits):

[^\p{L}\p{N}\s]

If ASCII-only is intentional, update the comment to reflect that.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/ActionsLib/General/ActSetVariableValue.cs` around lines
215 - 217, The comment and implementation disagree: the Regex.Replace call (the
assignment to variable.Value using Regex.Replace) claims to exclude Unicode
letters/digits but uses ASCII-only character classes [A-Za-z0-9]; either replace
the pattern with a Unicode-aware pattern using \p{L} and \p{N} (i.e., use
[^\p{L}\p{N}\s]) to truly allow all Unicode letters/digits, or if ASCII-only
behavior is intended, update the comment to state "ASCII letters and digits
only" so the comment matches the Regex.Replace usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant