Skip to content

Commit

Permalink
Merge pull request #2 from hinthealth/ac/chore/fix-array-like-keys
Browse files Browse the repository at this point in the history
Fix extracting keys for array like things.
  • Loading branch information
agustinrhcp authored Oct 27, 2023
2 parents b605bcf + 97d6b9f commit 91d33e9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/pact/provider/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def describe_response expected_response, interaction_context
end

def warn_if_actual_has_extra_keys(actual, expected)
extra_keys = extract_keys(Array(actual).first) - extract_keys(Array(expected).first)
extra_keys = extract_keys(actual) - extract_keys(expected)

if extra_keys.any?
pending "Extra keys: #{extra_keys.join(", ")}"
Expand All @@ -197,12 +197,15 @@ def warn_if_actual_has_extra_keys(actual, expected)
end

def extract_keys(hash, prefix: '')
return extract_keys(hash.first, prefix: "#{prefix}[]") if hash.is_a?(Array) && hash.first.is_a?(Hash)
return [] if hash.is_a?(Array)
return extract_keys(hash.first) if hash.is_a?(Array)

hash.reduce([]) do |acc, (k, v)|
new_acc = acc.concat(["#{prefix}#{k}"])
next new_acc.concat(extract_keys(v, prefix: "#{prefix}#{k}.")) if v.is_a?(Hash)
hash.reduce([]) do |acc, (key, value)|
next acc if key == 'id'

new_acc = acc.concat(["#{prefix}#{key}"])
next new_acc.concat(extract_keys(value, prefix: "#{prefix}#{key}.")) if value.is_a?(Hash)
next new_acc.concat(extract_keys(value.first, prefix: "#{prefix}#{key}[]")) if value.is_a?(Array) && value.first.is_a?(Hash)
next new_acc.concat(extract_keys(value.contents, prefix: "#{prefix}#{key}[]")) if value.is_a?(Pact::ArrayLike) && value.contents.is_a?(Hash)

new_acc
end
Expand Down

0 comments on commit 91d33e9

Please sign in to comment.