B #-: Fix update patch operation when nested attrs are provided (#4067)

* Fix update patch operation when nested attrs are provided
*  Prevent array merging
This commit is contained in:
Victor Palma authored and GitHub committed 2026-02-17 15:56:56 +01:00
1 parent 9609192e0f
commit dcd760f9ae
3 files changed
+81 -6

No files matched your search

+21 -3
View File
@@ -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
+21 -3
View File
@@ -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
+39
View File
@@ -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