@@ -211,7 +211,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
211211 if pe .Equals (rhsPEs [rI ]) {
212212 // merge LHS & RHS items
213213 mergedRHS .Insert (pe , struct {}{})
214- lChild , _ := observedLHS .Get (pe )
214+ lChild , _ := observedLHS .Get (pe ) // may be nil if the PE is duplicaated.
215215 rChild , _ := observedRHS .Get (pe )
216216 mergeOut , errs := w .mergeListItem (t , pe , lChild , rChild )
217217 errs = append (errs , errs ... )
@@ -237,7 +237,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
237237 if lI < lLen {
238238 pe := lhsPEs [lI ]
239239 if _ , ok := observedRHS .Get (pe ); ! ok {
240- // take LHS item
240+ // take LHS item using At to make sure we get the right item (observed may not contain the right item).
241241 lChild := lhs .AtUsing (w .allocator , lI )
242242 mergeOut , errs := w .mergeListItem (t , pe , lChild , nil )
243243 errs = append (errs , errs ... )
@@ -255,7 +255,7 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
255255 // Take the RHS item, merge with matching LHS item if possible
256256 pe := rhsPEs [rI ]
257257 mergedRHS .Insert (pe , struct {}{})
258- lChild , _ := observedLHS .Get (pe ) // may be nil
258+ lChild , _ := observedLHS .Get (pe ) // may be nil if absent or duplicaated.
259259 rChild , _ := observedRHS .Get (pe )
260260 mergeOut , errs := w .mergeListItem (t , pe , lChild , rChild )
261261 errs = append (errs , errs ... )
@@ -305,6 +305,9 @@ func (w *mergingWalker) indexListPathElements(t *schema.List, list value.List, a
305305 continue
306306 } else if ! found {
307307 observed .Insert (pe , child )
308+ } else {
309+ // Duplicated items are not merged with the new value, make them nil.
310+ observed .Insert (pe , value .NewValueInterface (nil ))
308311 }
309312 pes = append (pes , pe )
310313 }
0 commit comments