Skip to content

Commit 60cc822

Browse files
authored
🔀 Merge pull request #221 from oauth-xx/issue/80-handle-nested-array-of-hashes-in-params
✨ Handle a nested array of hashes in OAuth::Helper.normalize
2 parents 99a8d0d + 7d38441 commit 60cc822

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
* Added CODE_OF_CONDUCT.md (#217, #218 by @pboling)
1212
* Added FUNDING.yml (#217, #218 by @pboling)
1313
* Added Client Certificate Options: :ssl_client_cert and :ssl_client_key (#136, #220 by @pboling)
14+
* Handle a nested array of hashes in OAuth::Helper.normalize (#80, #221 by @pboling)
1415

1516
### Changed
1617

‎lib/oauth/helper.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ def normalize(params)
4747
# make sure the array has an element so we don't lose the key
4848
values << nil if values.empty?
4949
# multiple values were provided for a single key
50-
values.sort.collect do |v|
51-
[escape(k),escape(v)] * "="
50+
if values[0].is_a?(Hash)
51+
normalize_nested_query(values, k)
52+
else
53+
values.sort.collect do |v|
54+
[escape(k),escape(v)] * "="
55+
end
5256
end
5357
elsif values.is_a?(Hash)
5458
normalize_nested_query(values, k)
@@ -58,7 +62,7 @@ def normalize(params)
5862
end * "&"
5963
end
6064

61-
#Returns a string representation of the Hash like in URL query string
65+
# Returns a string representation of the Hash like in URL query string
6266
# build_nested_query({:level_1 => {:level_2 => ['value_1','value_2']}}, 'prefix'))
6367
# #=> ["prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_1", "prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_2"]
6468
def normalize_nested_query(value, prefix = nil)

‎test/units/test_oauth_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ def test_normalize
8282
assert_equal("oauth_consumer_key=vince_clortho&oauth_nonce=nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1240004133&oauth_token=token_value&oauth_version=1.0&weight%5Bvalue%5D=65", OAuth::Helper.normalize(params))
8383
end
8484

85+
def test_normalize_with_nested_array_of_hashes
86+
params = {
87+
"oauth_nonce" => "nonce",
88+
"weight" => { :value => "65" },
89+
"items" => [{"a" => 1}, {"b" => 2}],
90+
"oauth_signature_method" => "HMAC-SHA1",
91+
"oauth_timestamp" => "1240004133",
92+
"oauth_consumer_key" => "vince_clortho",
93+
"oauth_token" => "token_value",
94+
"oauth_version" => "1.0"
95+
}
96+
assert_equal("items%5B%5D%5Ba%5D=1&items%5B%5D%5Bb%5D=2&oauth_consumer_key=vince_clortho&oauth_nonce=nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1240004133&oauth_token=token_value&oauth_version=1.0&weight%5Bvalue%5D=65", OAuth::Helper.normalize(params))
97+
end
98+
8599
def test_normalize_nested_query
86100
assert_equal([], OAuth::Helper.normalize_nested_query({}))
87101
assert_equal(["foo=bar"], OAuth::Helper.normalize_nested_query({:foo => "bar"}))

0 commit comments

Comments
 (0)