Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
test(types): add unit test for JSON unmarshalling and validation
wizardchenf3635f313584
diff --git a/internal/types/json.go b/internal/types/json.goindex 98756520c..695728b9b 100644--- a/internal/types/json.go+++ b/internal/types/json.go@@ -59,7 +59,9 @@ func (j *JSON) UnmarshalJSON(data []byte) error {if j == nil {return errors.New("JSON: UnmarshalJSON on nil pointer")}- *j = JSON(data)+ copied := make([]byte, len(data))+ copy(copied, data)+ *j = JSON(copied)return nil}diff --git a/internal/types/json_test.go b/internal/types/json_test.gonew file mode 100644index 000000000..5e4284c0f--- /dev/null+++ b/internal/types/json_test.go@@ -0,0 +1,37 @@+package types++import (+ "encoding/json"+ "testing"+)++func TestJSONUnmarshalCopiesInput(t *testing.T) {+ type wrapper struct {+ Value JSON `json:"value"`+ }++ input := []byte(`{"value":{"generated_questions":[{"id":"q1","question":"今晚吃啥"}]}}`)++ var got wrapper+ if err := json.Unmarshal(input, &got); err != nil {+ t.Fatalf("unmarshal failed: %v", err)+ }++ // Simulate decoder buffer reuse/caller mutation after UnmarshalJSON returns.+ for i := range input {+ input[i] = 'x'+ }++ if !json.Valid(got.Value) {+ t.Fatalf("stored JSON became invalid after input mutation: %q", string(got.Value))+ }++ marshaled, err := json.Marshal(got)+ if err != nil {+ t.Fatalf("marshal failed: %v", err)+ }++ if len(marshaled) == 0 {+ t.Fatal("expected marshaled output")+ }+}