From f164dbdb1e5fc316e78b5308f908473be37b4103 Mon Sep 17 00:00:00 2001 From: Ihor Andrianov Date: Tue, 28 Jan 2025 21:26:05 +0200 Subject: [PATCH] fix case where first patched value applied wins --- core/json/json_operations.rs | 8 ++++++-- testing/json.test | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/core/json/json_operations.rs b/core/json/json_operations.rs index 95298febb..735674863 100644 --- a/core/json/json_operations.rs +++ b/core/json/json_operations.rs @@ -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::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) diff --git a/testing/json.test b/testing/json.test index 78e6171c8..d156572e0 100755 --- a/testing/json.test +++ b/testing/json.test @@ -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}}}