Fix #326 - Normalize query parameters to allow falsey values
This commit is contained in:
		
							parent
							
								
									45a8dcba36
								
							
						
					
					
						commit
						1715fc9945
					
				| 
						 | 
				
			
			@ -264,7 +264,9 @@ module Google
 | 
			
		|||
              # Temporary workaround for Hurley bug where the connection preference
 | 
			
		||||
              # is ignored and it uses nested anyway
 | 
			
		||||
              req.url.query_class = Hurley::Query::Flat
 | 
			
		||||
              query.each { | k, v| req.url.query[k] = v }
 | 
			
		||||
              query.each do | k, v|
 | 
			
		||||
                req.url.query[k] = normalize_query_value(v)
 | 
			
		||||
              end
 | 
			
		||||
              # End workaround
 | 
			
		||||
              apply_request_options(req)
 | 
			
		||||
            end
 | 
			
		||||
| 
						 | 
				
			
			@ -291,6 +293,19 @@ module Google
 | 
			
		|||
          req.header.update(header)
 | 
			
		||||
          req.options.timeout = options.timeout_sec
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        private
 | 
			
		||||
 | 
			
		||||
        def normalize_query_value(v)
 | 
			
		||||
          case v
 | 
			
		||||
          when Array
 | 
			
		||||
            v.map { |v2| normalize_query_value(v2) }
 | 
			
		||||
          when nil
 | 
			
		||||
            nil
 | 
			
		||||
          else
 | 
			
		||||
            v.to_s
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -272,4 +272,13 @@ RSpec.describe Google::Apis::Core::HttpCommand do
 | 
			
		|||
    command.query['a'] = [1,2,3]
 | 
			
		||||
    command.execute(client)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it 'should send falsey query parameters' do
 | 
			
		||||
    stub_request(:get, 'https://www.googleapis.com/zoo/animals?a=0&b=false')
 | 
			
		||||
      .to_return(status: [200, ''])
 | 
			
		||||
    command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
 | 
			
		||||
    command.query['a'] = 0
 | 
			
		||||
    command.query['b'] = false
 | 
			
		||||
    command.execute(client)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue