global
def cleanup_array!(base, spec, selector, fields_for_identity = [])
def cleanup_array!(base, spec, selector, fields_for_identity = []) marshal = lambda do |obj| Marshal.dump(slice(obj, fields_for_identity)) end RSpec::OpenAPI::HashHelper.matched_paths(base, selector).each do |paths| target_array = base.dig(*paths) spec_array = spec.dig(*paths) next unless target_array.is_a?(Array) && spec_array.is_a?(Array) spec_identities = Set.new(spec_array.map(&marshal)) target_array.select! { |e| spec_identities.include?(marshal.call(e)) } target_array.sort_by! { |param| fields_for_identity.map { |f| param[f] }.join('-') } # Keep the last duplicate to produce the result stably deduplicated = target_array.reverse.uniq { |param| slice(param, fields_for_identity) }.reverse target_array.replace(deduplicated) end base end