From 0d2cbe26df8bcb7f6cc84e3de7a0ef2263e69a7d Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Tue, 2 Aug 2011 16:20:54 -0400 Subject: [PATCH] Fixed issue with header/options ambiguity. --- lib/google/api_client.rb | 12 ++++-------- spec/google/api_client/discovery_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 725784cab..511ebf5a5 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -533,14 +533,10 @@ module Google # ) def execute(*params) # This block of code allows us to accept multiple parameter passing - # styles, and maintaining backwards compatibility. - if params.last.respond_to?(:to_hash) && params.size != 2 - # Hash options are tricky. If we get two arguments, it's ambiguous - # whether to treat them as API parameters or Hash options, but since - # it's rare to need to pass in options, we must assume that the - # developer wanted to pass API parameters. Prefer using named - # parameters to avoid this issue. Unnamed parameters should be - # considered syntactic sugar. + # styles, and maintaining some backwards compatibility. + # + # Note: I'm extremely tempted to deprecate this style of execute call. + if params.last.respond_to?(:to_hash) && params.size == 1 options = params.pop else options = {} diff --git a/spec/google/api_client/discovery_spec.rb b/spec/google/api_client/discovery_spec.rb index cd4606807..01e8205ed 100644 --- a/spec/google/api_client/discovery_spec.rb +++ b/spec/google/api_client/discovery_spec.rb @@ -14,6 +14,7 @@ require 'spec_helper' +require 'json' require 'signet/oauth_1/client' require 'httpadapter/adapters/net_http' @@ -238,6 +239,19 @@ describe Google::APIClient do ) end).should raise_error(Google::APIClient::ClientError) end + + it 'should correctly handle unnamed parameters' do + @client.authorization = :oauth_2 + @client.authorization.access_token = '12345' + result = @client.execute( + @prediction.training.insert, + {}, + JSON.generate({"id" => "bucket/object"}), + {'Content-Type' => 'application/json'} + ) + method, uri, headers, body = result.request + Hash[headers]['Content-Type'].should == 'application/json' + end end describe 'with the buzz API' do