From dcd760f9aedb696e697c5da14344c59fd9cdeab2 Mon Sep 17 00:00:00 2001 From: Victor Palma Date: Tue, 17 Feb 2026 15:56:56 +0100 Subject: [PATCH] B #-: Fix update patch operation when nested attrs are provided (#4067) * Fix update patch operation when nested attrs are provided * Prevent array merging --- src/form/app/models/provider.rb | 24 +++++++++++++++++--- src/form/app/models/provision.rb | 24 +++++++++++++++++--- src/form/app/models/schema.rb | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/form/app/models/provider.rb b/src/form/app/models/provider.rb index f952b958e9..da431051f2 100644 --- a/src/form/app/models/provider.rb +++ b/src/form/app/models/provider.rb @@ -295,10 +295,28 @@ module OneForm json = JSON.parse(json) if json.is_a?(String) nbody = @body.clone - UPDATE_ATTRS.each do |attribute| - next unless json[attribute] + return OpenNebula::Error.new( + 'Invalid update payload: expected a JSON object' + ) unless json.is_a?(Hash) - nbody[attribute] = json[attribute] + UPDATE_ATTRS.each do |attribute| + next unless json.key?(attribute) + + incoming = json[attribute] + current = nbody[attribute] + + next if incoming == REDACTED_MARK + + if incoming.is_a?(Hash) + incoming = incoming.reject {|_, v| v == REDACTED_MARK } + end + + nbody[attribute] = + if current.is_a?(Hash) && incoming.is_a?(Hash) + current.deep_merge(incoming, false) + else + incoming + end end rc = rename(nbody['name']) if nbody['name'] != name diff --git a/src/form/app/models/provision.rb b/src/form/app/models/provision.rb index 9ba62762b5..492e64869d 100644 --- a/src/form/app/models/provision.rb +++ b/src/form/app/models/provision.rb @@ -763,10 +763,28 @@ module OneForm json = JSON.parse(json) if json.is_a?(String) nbody = @body.clone - UPDATE_ATTRS.each do |attribute| - next unless json[attribute] + return OpenNebula::Error.new( + 'Invalid update payload: expected a JSON object' + ) unless json.is_a?(Hash) - nbody[attribute] = json[attribute] + UPDATE_ATTRS.each do |attribute| + next unless json.key?(attribute) + + incoming = json[attribute] + current = nbody[attribute] + + next if incoming == REDACTED_MARK + + if incoming.is_a?(Hash) + incoming = incoming.reject {|_, v| v == REDACTED_MARK } + end + + nbody[attribute] = + if current.is_a?(Hash) && incoming.is_a?(Hash) + current.deep_merge(incoming, false) + else + incoming + end end rc = rename(nbody['name']) if nbody['name'] != name diff --git a/src/form/app/models/schema.rb b/src/form/app/models/schema.rb index 9930cd6888..b5e19ff543 100644 --- a/src/form/app/models/schema.rb +++ b/src/form/app/models/schema.rb @@ -213,3 +213,42 @@ module OneForm end end + +# Expanding hash class with new methods +class Hash + + # Returns a new hash containing the contents of other_hash and the + # contents of self. If the value for entries with duplicate keys + # is a Hash, it will be merged recursively, otherwise it will be that + # of other_hash. + # + # @param [Hash] other_hash + # + # @return [Hash] Containing the merged values + # + # @example Merging two hashes + # h1 = {:a => 3, {:b => 3, :c => 7}} + # h2 = {:a => 22, c => 4, {:b => 5}} + # + # h1.deep_merge(h2) #=> {:a => 22, c => 4, {:b => 5, :c => 7}} + def deep_merge(other_hash, merge_array = true) + target = clone + + other_hash.each do |key, value| + current = target[key] + + target[key] = + if value.is_a?(Hash) && current.is_a?(Hash) + current.deep_merge(value, merge_array) + elsif value.is_a?(Array) && current.is_a?(Array) && merge_array + merged = current + value + merged.all? {|el| el.is_a?(Hash) } ? merged.uniq : merged + else + value + end + end + + target + end + +end