From 518ce3b168f4af496b1a24af0a0768cf556644fc Mon Sep 17 00:00:00 2001 From: Divyanshu Kumar <130997107+Divyanshu-2907@users.noreply.github.com> Date: Thu, 5 Nov 2026 16:18:07 +0530 Subject: [PATCH 1/3] Don't modify the params hash passed to Client.new The constructor deleted :timeout and :persistent from the caller's hash, so a reused config hash gave the second client the default 120s timeout and a persistent socket. A frozen hash raised FrozenError. Work on a dup of params instead. dup is used rather than clone because clone keeps the frozen state. --- CHANGELOG.md | 1 + lib/serpapi/client.rb | 16 ++++---- spec/serpapi/client/constructor_spec.rb | 51 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 spec/serpapi/client/constructor_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index fa01a94..dceb7fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Changelog + * [Unreleased] Stop `Client.new` from modifying the params hash passed in, and accept frozen hashes * [Unreleased] Add native Extract API support via the `extract` method * [Unreleased] Apply the `timeout` option to persistent connections * [Unreleased] Add Image API support diff --git a/lib/serpapi/client.rb b/lib/serpapi/client.rb index b8ae22a..0f3941e 100644 --- a/lib/serpapi/client.rb +++ b/lib/serpapi/client.rb @@ -74,22 +74,20 @@ def initialize(params = {}) raise SerpApiError, 'params cannot be nil' if params.nil? raise SerpApiError, "params must be hash, not: #{params.class}" unless params.instance_of?(Hash) + # work on a copy so the caller's hash is never modified + # (dup, unlike clone, also returns an unfrozen copy of a frozen hash) + config = params.dup + # store client HTTP request timeout - @timeout = params[:timeout] || 120 + @timeout = config.delete(:timeout) || 120 @timeout.freeze # enable HTTP persistent mode - @persistent = true - @persistent = params[:persistent] if params.key?(:persistent) + @persistent = config.key?(:persistent) ? config.delete(:persistent) : true @persistent.freeze - # delete this client only configuration keys - %i[timeout persistent].each do |option| - params.delete(option) if params.key?(option) - end - # set default query parameters - @params = params.clone || {} + @params = config # track ruby library as a client for statistic purpose @params[:source] = 'serpapi-ruby:' << SerpApi::VERSION diff --git a/spec/serpapi/client/constructor_spec.rb b/spec/serpapi/client/constructor_spec.rb new file mode 100644 index 0000000..6d81883 --- /dev/null +++ b/spec/serpapi/client/constructor_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +# These specs only build clients, so they make no HTTP request to serpapi.com. +describe 'SerpApi::Client constructor' do + it 'does not modify the params hash passed by the caller' do + config = { engine: 'google', api_key: 'test_key', timeout: 10, persistent: false } + original = config.dup + + SerpApi::Client.new(config) + + expect(config).to eq(original) + end + + it 'gives the same settings to every client built from a reused config hash' do + config = { engine: 'google', api_key: 'test_key', timeout: 10, persistent: false } + + first = SerpApi::Client.new(config) + second = SerpApi::Client.new(config) + + expect(second.timeout).to eq(first.timeout) + expect(second.timeout).to eq(10) + expect(second.persistent).to be false + expect(second.socket).to be_nil + end + + it 'accepts a frozen params hash' do + config = { engine: 'google', api_key: 'test_key', timeout: 10, persistent: false }.freeze + + client = SerpApi::Client.new(config) + + expect(client.timeout).to eq(10) + expect(client.persistent).to be false + expect(client.engine).to eq('google') + end + + it 'keeps timeout and persistent out of the query parameters' do + client = SerpApi::Client.new(engine: 'google', api_key: 'test_key', timeout: 10, persistent: false) + + expect(client.params).not_to have_key(:timeout) + expect(client.params).not_to have_key(:persistent) + end + + it 'defaults to a 120 second timeout and persistent mode' do + client = SerpApi::Client.new(engine: 'google', api_key: 'test_key') + + expect(client.timeout).to eq(120) + expect(client.persistent).to be true + expect(client.socket).not_to be_nil + client.close + end +end From c24449b1c89980c2edb044c4566e788c0bc1047b Mon Sep 17 00:00:00 2001 From: Victor Benarbia Date: Mon, 30 Nov 2026 17:43:30 -0600 Subject: [PATCH 2/3] similar implement avoiding the full duplication of object. --- lib/serpapi/client.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/serpapi/client.rb b/lib/serpapi/client.rb index 5871900..95eb375 100644 --- a/lib/serpapi/client.rb +++ b/lib/serpapi/client.rb @@ -74,20 +74,18 @@ def initialize(params = {}) raise SerpApiError, 'params cannot be nil' if params.nil? raise SerpApiError, "params must be hash, not: #{params.class}" unless params.instance_of?(Hash) - # work on a copy so the caller's hash is never modified - # (dup, unlike clone, also returns an unfrozen copy of a frozen hash) - config = params.dup - # store client HTTP request timeout - @timeout = config.delete(:timeout) || 120 + @timeout = params[:timeout] || 120 @timeout.freeze # enable HTTP persistent mode - @persistent = config.key?(:persistent) ? config.delete(:persistent) : true + @persistent = true + @persistent = params[:persistent] if params.key?(:persistent) @persistent.freeze - # set default query parameters - @params = config + # set default query parameters without the client only configuration keys + # (reject returns a new hash, so the caller's hash is never modified) + @params = params.reject { |key, _| [:timeout, :persistent].include?(key) } # track ruby library as a client for statistic purpose @params[:source] = 'serpapi-ruby:' << SerpApi::VERSION From af533a8f76b9c30455c973759726bc7fb407150f Mon Sep 17 00:00:00 2001 From: Victor Benarbia Date: Mon, 30 Nov 2026 17:53:24 -0600 Subject: [PATCH 3/3] Read client options given with String keys The constructor docs say keys can be Symbols or Strings, but timeout, persistent, api_key, engine, and symbolize_names were only read with Symbol keys. With String keys, timeout and persistent were ignored and sent to the API as query parameters, #api_key and #engine returned nil, and a per-call 'symbolize_names' was ignored and sent to the API. Read these options through a helper that accepts both key types. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + lib/serpapi/client.rb | 20 ++++++++------ spec/serpapi/client/constructor_spec.rb | 13 +++++++++ spec/serpapi/client/symbolize_names_spec.rb | 30 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 spec/serpapi/client/symbolize_names_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 207eddb..522252b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [Unreleased] Report the HTTP status instead of a JSON parse error for non-200 responses whose body is not JSON, such as a Cloudflare error page * [Unreleased] Raise `SerpApiError` instead of an `EncodingError` for responses whose body doesn't match the charset in their Content-Type * [Unreleased] Stop `Client.new` from modifying the params hash passed in, and accept frozen hashes + * [Unreleased] Read `api_key`, `engine`, `timeout`, `persistent`, and `symbolize_names` given with String keys, and stop sending a String `symbolize_names` key to the API * [Unreleased] Add native Extract API support via the `extract` method * [Unreleased] Apply the `timeout` option to persistent connections * [Unreleased] Add Image API support diff --git a/lib/serpapi/client.rb b/lib/serpapi/client.rb index 90e81e3..b9ce914 100644 --- a/lib/serpapi/client.rb +++ b/lib/serpapi/client.rb @@ -75,17 +75,16 @@ def initialize(params = {}) raise SerpApiError, "params must be hash, not: #{params.class}" unless params.instance_of?(Hash) # store client HTTP request timeout - @timeout = params[:timeout] || 120 + @timeout = option(params, :timeout) || 120 @timeout.freeze # enable HTTP persistent mode - @persistent = true - @persistent = params[:persistent] if params.key?(:persistent) + @persistent = option(params, :persistent, true) @persistent.freeze # set default query parameters without the client only configuration keys # (reject returns a new hash, so the caller's hash is never modified) - @params = params.reject { |key, _| [:timeout, :persistent].include?(key) } + @params = params.reject { |key, _| [:timeout, :persistent].any? { |name| name.to_s == key.to_s } } # ensure default parameter would not be modified later @params.freeze @@ -260,12 +259,12 @@ def account(api_key = nil) # @return [String] default search engine def engine - @params[:engine] + option(@params, :engine) end # @return [String] api_key user secret API key as provided to the constructor def api_key - @params[:api_key] + option(@params, :api_key) end # close open connection if active @@ -287,6 +286,11 @@ def inspect private + # Read an option passed with either a Symbol or a String key, the Symbol key first. + def option(hash, name, default = nil) + hash.fetch(name) { hash.fetch(name.to_s, default) } + end + def force_output(params, format) return params unless params.is_a?(Hash) @@ -304,7 +308,7 @@ def query(params) q.delete(:output) if params.key?('output') && !params.key?(:output) # do not pollute default params with custom params - q.delete(:symbolize_names) if q.key?(:symbolize_names) + q.delete_if { |key, _| key.to_s == 'symbolize_names' } # delete empty key/value q.compact @@ -392,7 +396,7 @@ def error_details(response) end def parse_json(response, endpoint, params) - JSON.parse(response.body, symbolize_names: params.fetch(:symbolize_names, true)) + JSON.parse(response.body, symbolize_names: option(params, :symbolize_names, true)) rescue JSON::ParserError, EncodingError raise_parser_error(response, endpoint, params) end diff --git a/spec/serpapi/client/constructor_spec.rb b/spec/serpapi/client/constructor_spec.rb index 6d81883..29d5cdb 100644 --- a/spec/serpapi/client/constructor_spec.rb +++ b/spec/serpapi/client/constructor_spec.rb @@ -40,6 +40,19 @@ expect(client.params).not_to have_key(:persistent) end + it 'reads client options and defaults passed with String keys' do + config = { 'engine' => 'google', 'api_key' => 'test_key', 'timeout' => 10, 'persistent' => false } + + client = SerpApi::Client.new(config) + + expect(client.engine).to eq('google') + expect(client.api_key).to eq('test_key') + expect(client.timeout).to eq(10) + expect(client.persistent).to be false + expect(client.socket).to be_nil + expect(client.params).to eq('engine' => 'google', 'api_key' => 'test_key') + end + it 'defaults to a 120 second timeout and persistent mode' do client = SerpApi::Client.new(engine: 'google', api_key: 'test_key') diff --git a/spec/serpapi/client/symbolize_names_spec.rb b/spec/serpapi/client/symbolize_names_spec.rb new file mode 100644 index 0000000..da76a0b --- /dev/null +++ b/spec/serpapi/client/symbolize_names_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +# These specs stub the HTTP client, so they make no HTTP request to serpapi.com. +describe 'symbolize_names search option' do + let(:http) { double } + let(:client) { SerpApi::Client.new(api_key: 'test-key', engine: 'google', persistent: false) } + let(:response) do + double(status: 200, body: '{"search_metadata":{"id":"1"}}', headers: { 'Content-Type' => 'application/json' }) + end + + before do + allow(HTTP).to receive(:timeout).and_return(double(headers: http)) + allow(http).to receive(:get).and_return(response) + end + + [:symbolize_names, 'symbolize_names'].each do |key| + it "returns String keys and keeps #{key.inspect} out of the request" do + results = client.search(q: 'coffee', key => false) + + expect(results).to eq('search_metadata' => { 'id' => '1' }) + expect(http).to have_received(:get) do |_url, options| + expect(options[:params].keys.map(&:to_s)).not_to include('symbolize_names') + end + end + end + + it 'returns Symbol keys by default' do + expect(client.search(q: 'coffee')).to eq(search_metadata: { id: '1' }) + end +end