Skip to content
Closed
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
11 changes: 11 additions & 0 deletions pkg/protocols/common/expressions/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func EvaluateByte(data []byte, base map[string]interface{}) ([]byte, error) {
func evaluate(data string, base map[string]interface{}) (string, error) {
expressions := FindExpressions(data, marker.ParenthesisOpen, marker.ParenthesisClose, base)

// Filter out expressions that are exact variable names in base
// these are simple variable references handled by replacer.Replace and
// evaluating them as govaluate expressions can misparse hyphens as subtraction
Comment on lines +48 to +50
filtered := make([]string, 0, len(expressions))
for _, expr := range expressions {
if _, ok := base[expr]; !ok {
filtered = append(filtered, expr)
}
}
expressions = filtered
Comment on lines +48 to +57
Comment on lines +48 to +57
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.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add test coverage for hyphenated variable names to prevent regression.

While the fix correctly addresses issue #7395, the PR checklist indicates no tests were added. Since this fixes a regression introduced between v3.7.1 and v3.8.0, a test case specifically covering hyphenated variable names (e.g., request-id, nextjs-html) would prevent future regressions.

🧪 Suggested test case structure

Consider adding a test in the expression package that verifies:

// Test that hyphenated variable names are treated as simple variables,
// not parsed as subtraction expressions
func TestEvaluateHyphenatedVariableNames(t *testing.T) {
    base := map[string]interface{}{
        "request-id": "abc123",
        "nextjs-html": "content",
    }
    
    // Test single hyphenated variable
    result, err := Evaluate("{{request-id}}", base)
    // expect: "abc123", no govaluate parse error
    
    // Test hyphenated variable in text
    result, err = Evaluate("ID: {{request-id}}", base)
    // expect: "ID: abc123"
    
    // Test multiple hyphenated variables
    result, err = Evaluate("{{request-id}}/{{nextjs-html}}", base)
    // expect: "abc123/content"
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/protocols/common/expressions/expressions.go` around lines 48 - 57, Add a
unit test in the expressions package that ensures hyphenated variable names are
treated as simple variable references (not parsed as subtraction) by the
replacer/Evaluate flow: create a base map containing keys like "request-id" and
"nextjs-html" and assert that Evaluate (or the public evaluation wrapper that
uses the logic in expressions.go which filters exact variable names from
govaluate parsing) returns "abc123" for "{{request-id}}", "ID: abc123" for "ID:
{{request-id}}", and "abc123/content" for "{{request-id}}/{{nextjs-html}}"; this
will prevent regressions to the filtering logic that skips entries present in
base and relies on replacer.Replace rather than govaluate parsing.

Comment on lines +51 to +57
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see this as nothing more than a temporary workaround because it only addresses the callers of evaluate(). The problem is that any other direct caller of FindExpressions(..., base) is still going to run into issues where they see the exact placeholder keys being treated as expressions.

Also, this approach allocates.


// replace simple placeholders (key => value) MarkerOpen + key + MarkerClose and General + key + General to value
data = replacer.Replace(data, base)

Expand Down
Loading