Skip to content

Commit 0c56ef5

Browse files
committed
Fix duplicate extraction of atomic elements from associative lists
ExtractItems with WithAppendKeyFields was duplicating atomic elements when extracting from associative lists. The item was processed twice: once for the direct path match and again for sub-path checking. Added test with atomicElementList to verify the fix.
1 parent 789a688 commit 0c56ef5

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

typed/remove.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,23 @@ func (w *removingWalker) doList(t *schema.List) (errs ValidationErrors) {
8080
path, _ := fieldpath.MakePath(pe)
8181
// save items on the path when we shouldExtract
8282
// but ignore them when we are removing (i.e. !w.shouldExtract)
83-
if w.toRemove.Has(path) {
84-
if w.shouldExtract {
85-
newItems = append(newItems, removeItemsWithSchema(item, w.toRemove, w.schema, t.ElementType, w.shouldExtract).Unstructured())
86-
} else {
87-
continue
83+
if w.shouldExtract {
84+
if w.toRemove.Has(path) || !w.toRemove.WithPrefix(pe).Empty() {
85+
if !w.toRemove.WithPrefix(pe).Empty() {
86+
// Continue if there are subset paths
87+
item = removeItemsWithSchema(item, w.toRemove.WithPrefix(pe), w.schema, t.ElementType, w.shouldExtract)
88+
}
89+
newItems = append(newItems, item.Unstructured())
8890
}
89-
}
90-
if subset := w.toRemove.WithPrefix(pe); !subset.Empty() {
91-
item = removeItemsWithSchema(item, subset, w.schema, t.ElementType, w.shouldExtract)
9291
} else {
93-
// don't save items not on the path when we shouldExtract.
94-
if w.shouldExtract {
92+
if w.toRemove.Has(path) {
9593
continue
9694
}
95+
if !w.toRemove.WithPrefix(pe).Empty() {
96+
item = removeItemsWithSchema(item, w.toRemove.WithPrefix(pe), w.schema, t.ElementType, w.shouldExtract)
97+
}
98+
newItems = append(newItems, item.Unstructured())
9799
}
98-
newItems = append(newItems, item.Unstructured())
99100
}
100101
if len(newItems) > 0 {
101102
w.out = newItems

typed/remove_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ var associativeAndAtomicSchema = `types:
107107
- name: atomicMap
108108
type:
109109
namedType: myAtomicMap
110+
- name: atomicElementList
111+
type:
112+
namedType: myAtomicElementList
110113
- name: myList
111114
list:
112115
elementType:
@@ -117,9 +120,20 @@ var associativeAndAtomicSchema = `types:
117120
- id
118121
- name: myAtomicMap
119122
map:
123+
fields:
124+
- name: id
125+
type:
126+
scalar: string
120127
elementType:
121128
scalar: string
122129
elementRelationship: atomic
130+
- name: myAtomicElementList
131+
list:
132+
elementType:
133+
namedType: myAtomicMap
134+
elementRelationship: associative
135+
keys:
136+
- id
123137
- name: mySequence
124138
list:
125139
elementType:
@@ -940,6 +954,14 @@ var extractWithKeysCases = []extractWithKeysTestCase{{
940954
"list": []interface{}{nil},
941955
},
942956
},
957+
{
958+
// extract atomic element from associative list
959+
object: `{"atomicElementList":[{"id":"test-123","data":"value","extra":"field"}]}`,
960+
set: _NS(
961+
_P("atomicElementList", _KBF("id", "test-123")),
962+
),
963+
wantOutput: typed.YAMLObject(`{"atomicElementList":[{"id":"test-123","data":"value","extra":"field"}]}`),
964+
},
943965
},
944966
}}
945967

0 commit comments

Comments
 (0)