Skip to content

Commit 99911fb

Browse files
three-foxes-in-a-trenchcoatThree Foxes (in a Trenchcoat)
andauthored
fix: use proper JSON marshaling for customrest prompt to handle special characters (#1615)
This fixes issue #1556 where the customrest backend fails when error messages contain quotes or other special characters. The root cause was that fmt.Sprintf was used to construct JSON, which doesn't escape special characters like quotes, newlines, or tabs. When Kubernetes error messages contain image names with quotes (e.g., "nginx:1.a.b.c"), the resulting JSON was malformed and failed to parse. The fix uses json.Marshal to properly construct the JSON payload, which automatically handles all special character escaping according to JSON spec. Fixes #1556 Signed-off-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com> Co-authored-by: Three Foxes (in a Trenchcoat) <threefoxes53235@gmail.com>
1 parent abc4647 commit 99911fb

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

pkg/analysis/analysis.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package analysis
1616
import (
1717
"context"
1818
"encoding/base64"
19+
"encoding/json"
1920
"errors"
2021
"fmt"
2122
"reflect"
@@ -526,7 +527,22 @@ func (a *Analysis) getAIResultForSanitizedFailures(texts []string, promptTmpl st
526527
// Process template.
527528
prompt := fmt.Sprintf(strings.TrimSpace(promptTmpl), a.Language, inputKey)
528529
if a.AIClient.GetName() == ai.CustomRestClientName {
529-
prompt = fmt.Sprintf(ai.PromptMap["raw"], a.Language, inputKey, prompt)
530+
// Use proper JSON marshaling to handle special characters in error messages
531+
// This fixes issues with quotes, newlines, and other special chars in inputKey
532+
customRestPrompt := struct {
533+
Language string `json:"language"`
534+
Message string `json:"message"`
535+
Prompt string `json:"prompt"`
536+
}{
537+
Language: a.Language,
538+
Message: inputKey,
539+
Prompt: prompt,
540+
}
541+
promptBytes, err := json.Marshal(customRestPrompt)
542+
if err != nil {
543+
return "", fmt.Errorf("failed to marshal customrest prompt: %w", err)
544+
}
545+
prompt = string(promptBytes)
530546
}
531547
response, err := a.AIClient.GetCompletion(a.Context, prompt)
532548
if err != nil {

0 commit comments

Comments
 (0)