fix case where first patched value applied wins

This commit is contained in:
Ihor Andrianov
2025-01-28 21:26:05 +02:00
parent 8ba43bfc17
commit f164dbdb1e
2 changed files with 9 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
use std::collections::VecDeque;
use std::collections::{HashSet, VecDeque};
use crate::types::OwnedValue;
@@ -48,7 +48,7 @@ pub fn json_patch(target: &OwnedValue, patch: &OwnedValue) -> crate::Result<Owne
/// * Non-object values replace the target completely
fn merge_patch(target: &mut Val, patch: Val) {
let mut queue = VecDeque::with_capacity(8);
let mut applied_keys = HashSet::<String>::new();
queue.push_back(PatchOperation {
path: Vec::new(),
patch,
@@ -72,7 +72,11 @@ fn merge_patch(target: &mut Val, patch: Val) {
*current_val = Val::Null;
}
(Val::Object(target_map), Val::Object(patch_map)) => {
applied_keys.clear();
for (key, patch_val) in patch_map {
if applied_keys.insert(key.clone()) == false {
continue;
}
if let Some(pos) = target_map
.iter()
.position(|(target_key, _)| target_key == &key)

View File

@@ -592,6 +592,9 @@ do_execsql_test json-patch-preserve-duplicates-1 {
do_execsql_test json-patch-preserve-duplicates-2 {
select json_patch('{"x":100,"x":200}', '{"x":900}');
} {{{"x":900,"x":200}}}
do_execsql_test json-patch-first-update-wins {
select json_patch('{"x":100,"c":200}', '{"x":900, "x":null}');
} {{{"x":900,"c":200}}}
do_execsql_test json-patch-override-1 {
select json_patch('{"a":1,"b":2}', '{"b":3}');
} {{{"a":1,"b":3}}}