Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(ingestion): preserve typed metadata during merges (#20808)

jay777215fc413b957d1
diff --git a/internal/ingestion/service/doc_state.go b/internal/ingestion/service/doc_state.go
index a953f09c8a..31184c8fdb 100644
--- a/internal/ingestion/service/doc_state.go
+++ b/internal/ingestion/service/doc_state.go
@@ -24,7 +24,6 @@ import (
"ragflow/internal/common"
taskpkg "ragflow/internal/ingestion/task"
documentpkg "ragflow/internal/service/document"
- "ragflow/internal/utility"
)
// docStateSvc is the subset of *service.DocumentService needed to finalize a
@@ -38,7 +37,7 @@ type docStateSvc interface {
}
// docStateUpdater applies a pipeline run's results to document state: it
-// merges the pipeline-produced metadata (filling only keys not already present)
+// replaces metadata keys produced by the pipeline while preserving other keys,
// and bumps the document/dataset chunk and token counters. Both steps are
// best-effort; failures are logged and do not fail the task.
type docStateUpdater struct {
@@ -77,23 +76,22 @@ func (u *docStateUpdater) apply(ctx context.Context, r *taskpkg.PipelineResult)
}
}
-// mergeDocMetadata reads existing metadata, unions it with the freshly
-// aggregated doc metadata (list values merged + de-duplicated, scalars from the
-// stored map winning — matching Python task_executor.py:572
-// update_metadata_to(metadata, existing_meta)), then splits combined values
-// before writing the merged map back (Python doc_metadata_service.py:468
-// _split_combined_values). A read failure aborts the merge: SetDocumentMetadata
-// is a full overwrite, so writing with an empty baseline would destroy existing
-// keys.
+// mergeDocMetadata replaces stored keys with the freshly aggregated values,
+// regardless of value type, and retains stored keys absent from this run.
+// It splits combined string-list values before writing the complete map back.
+// A read failure aborts the merge to avoid losing keys during replacement.
func mergeDocMetadata(ctx context.Context, svc docStateSvc, docID string, metadata map[string]any) error {
existing, err := svc.GetDocumentMetadataByID(ctx, docID)
if err != nil {
return err
}
- if existing == nil {
- existing = map[string]any{}
+ merged := make(map[string]any, len(existing)+len(metadata))
+ for key, value := range existing {
+ merged[key] = value
+ }
+ for key, value := range metadata {
+ merged[key] = value
}
- merged := utility.UpdateMetadataTo(metadata, existing)
merged = common.SplitCombinedMetadataValues(merged)
return svc.SetDocumentMetadata(ctx, docID, merged)
}
@@ -123,14 +121,5 @@ func applyBuiltInMetadata(ctx context.Context, svc docStateSvc, docID, docName s
if len(builtIn) == 0 {
return nil
}
- existing, err := svc.GetDocumentMetadataByID(ctx, docID)
- if err != nil {
- return err
- }
- if existing == nil {
- existing = map[string]any{}
- }
- merged := utility.UpdateMetadataTo(existing, builtIn)
- merged = common.SplitCombinedMetadataValues(merged)
- return svc.SetDocumentMetadata(ctx, docID, merged)
+ return mergeDocMetadata(ctx, svc, docID, builtIn)
}
diff --git a/internal/ingestion/service/doc_state_test.go b/internal/ingestion/service/doc_state_test.go
index f47ebc1c5c..91c5678199 100644
--- a/internal/ingestion/service/doc_state_test.go
+++ b/internal/ingestion/service/doc_state_test.go
@@ -18,6 +18,7 @@ package service
import (
"context"
+ "reflect"
"testing"
"time"
@@ -44,15 +45,35 @@ func (s *stubDocStateSvc) GetDocumentMetadataByID(ctx context.Context, docID str
func (s *stubDocStateSvc) SetDocumentMetadata(ctx context.Context, docID string, meta map[string]any) error {
s.setCalled = true
- if s.metaData == nil {
- s.metaData = make(map[string]any)
- }
+ s.metaData = make(map[string]any, len(meta))
for k, v := range meta {
s.metaData[k] = v
}
return nil
}
+func TestDocStateUpdater_PreservesTypedMetadataOnReplacement(t *testing.T) {
+ svc := &stubDocStateSvc{metaData: map[string]any{
+ "year": float64(2024), "verified": false, "null": nil,
+ "tags": []any{"contract", float64(123)},
+ "outline": []any{map[string]any{"title": "Intro", "depth": float64(1)}},
+ }}
+ u := &docStateUpdater{docSvc: svc}
+ u.apply(t.Context(), &taskpkg.PipelineResult{
+ DocID: "doc-1", Metadata: map[string]any{
+ "author": "Alice", "year": map[string]any{"value": float64(2025)},
+ },
+ })
+ want := map[string]any{
+ "author": "Alice", "year": map[string]any{"value": float64(2025)}, "verified": false, "null": nil,
+ "tags": []any{"contract", float64(123)},
+ "outline": []any{map[string]any{"title": "Intro", "depth": float64(1)}},
+ }
+ if !svc.setCalled || !reflect.DeepEqual(svc.metaData, want) {
+ t.Fatalf("stored metadata = %#v, want %#v", svc.metaData, want)
+ }
+}
+
func (s *stubDocStateSvc) ApplyDocCounts(ctx context.Context, docID, kbID string, chunkNum, tokenNum int, duration float64) error {
s.incrementCalled = true
s.gotDocID = docID
@@ -102,20 +123,17 @@ func TestDocStateUpdater_MergesNewKeys(t *testing.T) {
}
}
-func TestDocStateUpdater_PreservesExistingKey(t *testing.T) {
+func TestDocStateUpdater_ReplacesExistingKey(t *testing.T) {
svc := &stubDocStateSvc{metaData: map[string]any{"author": "Alice"}}
u := &docStateUpdater{docSvc: svc}
ctx := t.Context()
u.apply(ctx, &taskpkg.PipelineResult{DocID: "doc-1", Metadata: map[string]any{"author": "Bob"}, ChunkCount: 1, TokenConsumption: 10})
- if svc.metaData["author"] != "Alice" {
- t.Errorf("existing key must NOT be overwritten: got %q", svc.metaData["author"])
+ if svc.metaData["author"] != "Bob" {
+ t.Errorf("new value must replace existing key: got %q", svc.metaData["author"])
}
}
-func TestDocStateUpdater_UnionsListValues(t *testing.T) {
- // Stored doc metadata already has a list value; a new run extracts more
- // (possibly overlapping) values. The merge must union + de-dupe, matching
- // Python update_metadata_to(metadata, existing_meta).
+func TestDocStateUpdater_ReplacesListValues(t *testing.T) {
svc := &stubDocStateSvc{metaData: map[string]any{"people": []string{"关羽", "张辽"}}}
u := &docStateUpdater{docSvc: svc}
ctx := t.Context()
@@ -128,37 +146,40 @@ func TestDocStateUpdater_UnionsListValues(t *testing.T) {
if !ok {
t.Fatalf("people should be []string, got %T", svc.metaData["people"])
}
- want := []string{"张辽", "刘备", "关羽"}
- if len(got) != len(want) {
- t.Fatalf("people=%v, want union %v", got, want)
- }
- seen := map[string]bool{}
- for _, p := range got {
- if seen[p] {
- t.Fatalf("duplicate %q in %v", p, got)
- }
- seen[p] = true
- }
- for _, w := range want {
- if !seen[w] {
- t.Fatalf("missing %q in %v", w, got)
- }
+ want := []string{"张辽", "刘备"}
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("people=%v, want replacement %v", got, want)
}
}
-func TestDocStateUpdater_PreservesExistingScalar(t *testing.T) {
- // Python update_metadata_to keeps the stored scalar when both sides carry
- // a scalar for the same key (stored wins).
- svc := &stubDocStateSvc{metaData: map[string]any{"author": "Alice"}}
- u := &docStateUpdater{docSvc: svc}
- ctx := t.Context()
- u.apply(ctx, &taskpkg.PipelineResult{
- DocID: "doc-1",
- Metadata: map[string]any{"author": "Bob"},
- ChunkCount: 1, TokenConsumption: 10,
- })
- if svc.metaData["author"] != "Alice" {
- t.Fatalf("stored scalar must win: got %q", svc.metaData["author"])
+func TestDocStateUpdater_NewValuesReplaceStoredValues(t *testing.T) {
+ tests := []struct {
+ name string
+ stored, extracted any
+ }{
+ {"number", float64(2024), float64(2025)},
+ {"false", true, false},
+ {"zero", float64(42), float64(0)},
+ {"null", "old", nil},
+ {"object to scalar", map[string]any{"x": 1}, "new"},
+ {"scalar to object", "old", map[string]any{"x": 2}},
+ {"structured list to scalar", []any{1, 2}, float64(42)},
+ {"scalar to structured list", "old", []any{1, 2}},
+ {"structured list", []any{1, 2}, []any{3, false}},
+ {"object", map[string]any{"x": 1}, map[string]any{"x": 2}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ svc := &stubDocStateSvc{metaData: map[string]any{"key": tt.stored, "keep": false}}
+ u := &docStateUpdater{docSvc: svc}
+ u.apply(t.Context(), &taskpkg.PipelineResult{
+ DocID: "doc-1", Metadata: map[string]any{"key": tt.extracted},
+ })
+ want := map[string]any{"key": tt.extracted, "keep": false}
+ if !svc.setCalled || !reflect.DeepEqual(svc.metaData, want) {
+ t.Fatalf("stored metadata = %#v, want %#v", svc.metaData, want)
+ }
+ })
}
}
@@ -224,15 +245,22 @@ func TestDocStateUpdater_BuiltInMetadataGatedOff(t *testing.T) {
}
func TestDocStateUpdater_BuiltInMetadataOverwritesExisting(t *testing.T) {
- svc := &stubDocStateSvc{metaData: map[string]any{"update_time": "2020-01-01 00:00:00"}}
+ svc := &stubDocStateSvc{metaData: map[string]any{
+ "update_time": []any{float64(2020)},
+ "file_name": map[string]any{"old": true},
+ "keep": false,
+ }}
u := &docStateUpdater{docSvc: svc}
ctx := t.Context()
u.apply(ctx, &taskpkg.PipelineResult{
- DocID: "doc-1",
- DocName: "annual-report.pdf",
- AutoMetadataEnabled: true,
- BuiltInMetadataConfig: []any{map[string]any{"key": "update_time", "type": "time"}},
- ChunkCount: 1, TokenConsumption: 10,
+ DocID: "doc-1",
+ DocName: "annual-report.pdf",
+ AutoMetadataEnabled: true,
+ BuiltInMetadataConfig: []any{
+ map[string]any{"key": "update_time", "type": "time"},
+ map[string]any{"key": "file_name", "type": "string"},
+ },
+ ChunkCount: 1, TokenConsumption: 10,
})
if svc.metaData["update_time"] == "2020-01-01 00:00:00" {
t.Errorf("built-in update_time must overwrite the stored value: %v", svc.metaData["update_time"])
@@ -240,6 +268,9 @@ func TestDocStateUpdater_BuiltInMetadataOverwritesExisting(t *testing.T) {
if _, ok := svc.metaData["update_time"].(string); !ok {
t.Fatalf("update_time = %T, want string", svc.metaData["update_time"])
}
+ if svc.metaData["file_name"] != "annual-report.pdf" || svc.metaData["keep"] != false {
+ t.Fatalf("built-in fields must overwrite structures and retain other keys: %#v", svc.metaData)
+ }
}
func TestDocStateUpdater_BuiltInMetadataEmptyConfig(t *testing.T) {
diff --git a/internal/ingestion/task/indexdoc/process_test.go b/internal/ingestion/task/indexdoc/process_test.go
index ca453519aa..ce770512a1 100644
--- a/internal/ingestion/task/indexdoc/process_test.go
+++ b/internal/ingestion/task/indexdoc/process_test.go
@@ -1,10 +1,49 @@
package indexdoc
import (
+ "reflect"
"testing"
"time"
)
+func TestProcessChunksForPipeline_PreservesTypedMetadata(t *testing.T) {
+ want := map[string]any{
+ "year": float64(2025), "verified": false, "scalar": map[string]any{"x": 1},
+ "labels": []string{"a", "b", "c"}, "object": map[string]any{"x": 1},
+ "tags": []any{"contract", float64(123)},
+ "outline": []any{map[string]any{"title": "Intro"}},
+ }
+ chunks := []map[string]any{
+ {"text": "first", "metadata": map[string]any{
+ "year": float64(2024), "verified": true, "scalar": map[string]any{"x": 1},
+ "labels": []any{"a", "b"}, "object": map[string]any{"x": 1},
+ "tags": []any{"contract", float64(123)},
+ "outline": []any{map[string]any{"title": "Intro"}},
+ }},
+ {"text": "second", "metadata": map[string]any{
+ "year": float64(2025), "verified": false, "scalar": float64(42),
+ "labels": []any{"b", "c"}, "object": map[string]any{"x": 2},
+ "tags": false, "outline": float64(42),
+ }},
+ {"text": "third", "metadata": map[string]any{
+ "scalar": nil, "labels": "c",
+ "outline": []any{map[string]any{"title": "Replacement"}},
+ }},
+ }
+ got, err := ProcessChunksForPipeline(chunks, "doc-1", "report.pdf", time.Now())
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("metadata = %#v, want %#v", got, want)
+ }
+ for i, chunk := range chunks {
+ if _, exists := chunk["metadata"]; exists {
+ t.Fatalf("consumed metadata must be removed from chunk %d", i)
+ }
+ }
+}
+
// =============================================================================
// RenameTextToContentWithWeight - Python processChunks logic
// =============================================================================
diff --git a/internal/utility/metadata.go b/internal/utility/metadata.go
index 49a034a7f3..59bc450f35 100644
--- a/internal/utility/metadata.go
+++ b/internal/utility/metadata.go
@@ -16,10 +16,12 @@
package utility
+import "reflect"
+
// UpdateMetadataTo merges metadata into an existing metadata map.
-// Only string and []string values are accepted. Existing keys are preserved
-// (not overwritten). List values are merged and deduplicated.
-// Mirrors Python: common.metadata_utils.update_metadata_to()
+// String lists are merged and deduplicated; incoming scalars otherwise win.
+// If either value at an existing key is a map or a non-string list or array,
+// the target value wins. Missing keys retain incoming values intact.
func UpdateMetadataTo(target map[string]any, meta any) map[string]any {
if target == nil {
return nil
@@ -34,8 +36,8 @@ func UpdateMetadataTo(target map[string]any, meta any) map[string]any {
}
for k, v := range metaMap {
- normVal := normalizeMetaValue(v)
- if normVal == nil {
+ normVal, accepted := normalizeMetaValue(v)
+ if !accepted {
continue
}
@@ -45,15 +47,15 @@ func UpdateMetadataTo(target map[string]any, meta any) map[string]any {
continue
}
- // Merge with existing value, mirroring Python
- // common.metadata_utils.update_metadata_to exactly:
- // - both lists: extend + dedupe
- // - target is list, incoming is scalar: append + dedupe
- // - target is scalar (or any non-list): overwrite with the
- // incoming value (the stored/merged-in side wins), NOT a list.
- targetList, targetIsList := existing.([]string)
+ if isStructuredMetadataValue(existing) || isStructuredMetadataValue(normVal) {
+ continue
+ }
+ targetList, targetIsList := metadataStringList(existing)
normList, normIsList := normVal.([]string)
- if targetIsList {
+ _, normIsString := normVal.(string)
+ if targetIsList && (normIsList || normIsString) {
+ // Appending must not write into a backing array shared by the caller.
+ targetList = targetList[:len(targetList):len(targetList)]
if normIsList {
target[k] = dedupeStrings(append(targetList, normList...))
} else if s, ok := normVal.(string); ok {
@@ -61,46 +63,80 @@ func UpdateMetadataTo(target map[string]any, meta any) map[string]any {
}
continue
}
- // target is a scalar: overwrite with the incoming value.
+ // Incoming scalars, including false, zero and null, win.
target[k] = normVal
}
return target
}
-// normalizeMetaValue normalizes a metadata value.
-// Returns a string, []string, or nil if the value is not acceptable.
-func normalizeMetaValue(v any) any {
- switch val := v.(type) {
- case string:
- if val == "" {
- return nil
- }
- return val
- case []string:
- filtered := make([]string, 0, len(val))
- for _, s := range val {
+// isStructuredMetadataValue recognizes maps and non-string slices or arrays.
+// Typed and JSON-decoded string lists use the string-list merge rules instead.
+func isStructuredMetadataValue(v any) bool {
+ if v == nil {
+ return false
+ }
+ if _, ok := metadataStringList(v); ok {
+ return false
+ }
+ switch reflect.TypeOf(v).Kind() {
+ case reflect.Map, reflect.Slice, reflect.Array:
+ return true
+ default:
+ return false
+ }
+}
+
+// normalizeMetaValue filters empty strings without changing other value types.
+// The acceptance flag distinguishes a retained null from a skipped value.
+func normalizeMetaValue(v any) (any, bool) {
+ if s, ok := v.(string); ok {
+ return s, s != ""
+ }
+ if list, ok := metadataStringList(v); ok {
+ filtered := make([]string, 0, len(list))
+ for _, s := range list {
if s != "" {
filtered = append(filtered, s)
}
}
if len(filtered) == 0 {
- return nil
+ return nil, false
}
- return dedupeStrings(filtered)
+ return dedupeStrings(filtered), true
+ }
+ return v, true
+}
+
+// metadataStringList recognizes JSON-decoded string lists and string slices
+// or arrays, including named collection types.
+func metadataStringList(v any) ([]string, bool) {
+ switch list := v.(type) {
+ case []string:
+ return list, true
case []any:
- filtered := make([]string, 0, len(val))
- for _, elem := range val {
- if s, ok := elem.(string); ok && s != "" {
- filtered = append(filtered, s)
+ strings := make([]string, 0, len(list))
+ for _, item := range list {
+ s, ok := item.(string)
+ if !ok {
+ return nil, false
}
+ strings = append(strings, s)
}
- if len(filtered) == 0 {
- return nil
- }
- return dedupeStrings(filtered)
+ return strings, true
default:
- return nil
+ value := reflect.ValueOf(v)
+ if !value.IsValid() || (value.Kind() != reflect.Slice && value.Kind() != reflect.Array) {
+ return nil, false
+ }
+ if value.Type().Elem().Kind() != reflect.String {
+ return nil, false
+ }
+ strings := make([]string, value.Len())
+ for i := range strings {
+ strings[i] = value.Index(i).String()
+ }
+ return strings, true
}
}
diff --git a/internal/utility/metadata_test.go b/internal/utility/metadata_test.go
index fa6179886e..ab4428db5e 100644
--- a/internal/utility/metadata_test.go
+++ b/internal/utility/metadata_test.go
@@ -1,6 +1,7 @@
package utility
import (
+ "reflect"
"testing"
)
@@ -34,13 +35,96 @@ func TestUpdateMetadataTo_ExistingKeyScalarOverwrite(t *testing.T) {
}
}
-func TestUpdateMetadataTo_StringValuesOnly(t *testing.T) {
- result := UpdateMetadataTo(map[string]any{}, map[string]any{"num": 42, "ok": true})
- if _, exists := result["num"]; exists {
- t.Errorf("numeric values should be skipped")
+func TestUpdateMetadataTo_PreservesTypedValues(t *testing.T) {
+ values := map[string]any{
+ "integer": int64(42), "decimal": 1.5, "zero": 0, "false": false,
+ "null": nil, "object": map[string]any{"title": "Intro"},
+ "mixed": []any{"contract", 123, false}, "numbers": []any{1, 2},
+ "outline": []any{map[string]any{"title": "Intro", "depth": 1}},
}
- if _, exists := result["ok"]; exists {
- t.Errorf("bool values should be skipped")
+ got := UpdateMetadataTo(map[string]any{}, values)
+ if !reflect.DeepEqual(got, values) {
+ t.Fatalf("metadata = %#v, want %#v", got, values)
+ }
+}
+
+func TestUpdateMetadataTo_TypedConflicts(t *testing.T) {
+ type tags []string
+ tests := []struct {
+ name string
+ target, incoming, want any
+ }{
+ {"number overwrites scalar", "new", int64(42), int64(42)},
+ {"false overwrites list", []string{"new"}, false, false},
+ {"null overwrites scalar", "new", nil, nil},
+ {"object keeps string", map[string]any{"x": 1}, "old", map[string]any{"x": 1}},
+ {"object keeps number", map[string]any{"x": 1}, int64(42), map[string]any{"x": 1}},
+ {"object keeps float", map[string]any{"x": 1}, 1.5, map[string]any{"x": 1}},
+ {"object keeps bool", map[string]any{"x": 1}, false, map[string]any{"x": 1}},
+ {"object keeps null", map[string]any{"x": 1}, nil, map[string]any{"x": 1}},
+ {"structured list keeps number", []any{1, 2}, int64(42), []any{1, 2}},
+ {"structured list keeps float", []any{1, 2}, 1.5, []any{1, 2}},
+ {"structured list keeps bool", []any{1, 2}, false, []any{1, 2}},
+ {"structured list keeps null", []any{1, 2}, nil, []any{1, 2}},
+ {"typed numeric list keeps number", []int{1, 2}, 42, []int{1, 2}},
+ {"typed numeric list keeps string", []float64{1, 2}, "old", []float64{1, 2}},
+ {"typed map keeps scalar", map[string]int{"x": 1}, false, map[string]int{"x": 1}},
+ {"array keeps scalar", [2]int{1, 2}, 42, [2]int{1, 2}},
+ {"incoming array keeps target", 42, [2]int{1, 2}, 42},
+ {"object conflict keeps first", map[string]any{"x": 1}, map[string]any{"x": 2}, map[string]any{"x": 1}},
+ {"structured list conflict keeps first", []any{1, 2}, []any{3, 4}, []any{1, 2}},
+ {"typed numeric list keeps target", "new", []int{1, 2}, "new"},
+ {"object keeps target", "new", map[string]any{"x": 1}, "new"},
+ {"mixed list keeps target", "new", []any{"old", 1}, "new"},
+ {"structured target keeps list", []any{"new", 1}, []string{"old"}, []any{"new", 1}},
+ {"structured target keeps string", []any{1, 2}, "old", []any{1, 2}},
+ {"decoded string list merges", []any{"a", "b"}, []any{"b", "c"}, []string{"a", "b", "c"}},
+ {"decoded string list appends", []any{"a"}, "b", []string{"a", "b"}},
+ {"named string slice merges", tags{"a", "b"}, []string{"b", "c"}, []string{"a", "b", "c"}},
+ {"incoming named string slice merges", []string{"a"}, tags{"a", "b"}, []string{"a", "b"}},
+ {"named string slice appends", tags{"a"}, "b", []string{"a", "b"}},
+ {"string array merges", [2]string{"a", "b"}, []any{"b", "c"}, []string{"a", "b", "c"}},
+ {"incoming string array merges", []string{"a"}, [2]string{"a", "b"}, []string{"a", "b"}},
+ {"string array appends", [1]string{"a"}, "b", []string{"a", "b"}},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := UpdateMetadataTo(map[string]any{"key": tt.target}, map[string]any{"key": tt.incoming})
+ value, exists := got["key"]
+ if !exists || !reflect.DeepEqual(value, tt.want) {
+ t.Fatalf("value = %#v, want %#v", got["key"], tt.want)
+ }
+ })
+ }
+}
+
+func TestUpdateMetadataTo_NormalizesTypedStringCollections(t *testing.T) {
+ type tags []string
+ for name, incoming := range map[string]any{
+ "named slice": tags{"a", "", "a", "b"},
+ "array": [4]string{"a", "", "a", "b"},
+ } {
+ t.Run(name, func(t *testing.T) {
+ got := UpdateMetadataTo(map[string]any{}, map[string]any{"tags": incoming})
+ if !reflect.DeepEqual(got["tags"], []string{"a", "b"}) {
+ t.Fatalf("tags = %#v, want [a b]", got["tags"])
+ }
+ })
+ }
+}
+
+func TestUpdateMetadataTo_DoesNotModifySharedListBackingArray(t *testing.T) {
+ for _, incoming := range []any{"b", []string{"b"}} {
+ t.Run(reflect.TypeOf(incoming).String(), func(t *testing.T) {
+ backing := []string{"a", "untouched"}
+ got := UpdateMetadataTo(map[string]any{"tags": backing[:1]}, map[string]any{"tags": incoming})
+ if !reflect.DeepEqual(got["tags"], []string{"a", "b"}) {
+ t.Fatalf("tags = %#v, want [a b]", got["tags"])
+ }
+ if !reflect.DeepEqual(backing, []string{"a", "untouched"}) {
+ t.Fatalf("shared backing array changed: %v", backing)
+ }
+ })
}
}
@@ -111,21 +195,3 @@ func TestUpdateMetadataTo_SkipOnlyEmptyStringsList(t *testing.T) {
t.Error("all-empty list should be skipped")
}
}
-
-// TestUpdateMetadataTo_ScalarScalarKeepsExistingPythonParity pins the exact
-// Python update_metadata_to (common/metadata_utils.py:301) scalar semantics for
-// the "re-ingest + same key + both sides scalar" scenario: the loop iterates
-// the second argument (existing_meta), so when the target already holds a
-// scalar the EXISTING (stored) value wins — NOT a [old,new] list. This guards
-// against a regression where scalar+scalar would be list-ified.
-func TestUpdateMetadataTo_ScalarScalarKeepsExistingPythonParity(t *testing.T) {
- // mergeDocMetadata calls UpdateMetadataTo(new, existing), so existing is
- // the second argument and must win for a shared scalar key.
- result := UpdateMetadataTo(
- map[string]any{"author": "NEWLY_EXTRACTED"},
- map[string]any{"author": "STORED"},
- )
- if result["author"] != "STORED" {
- t.Errorf("author = %v, want STORED (existing scalar wins, Python parity)", result["author"])
- }
-}