From e0c4e00d10c1d359a84d711f3ce65d2189269372 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Tue, 17 Nov 2026 21:45:36 +0800 Subject: [PATCH] Honor constructor symbolize_names when decoding JSON The JSON parser reads only per-call options, ignoring the documented constructor setting. Archived searches cannot receive a per-call parser option at all. Fall back to the constructor setting before the default of true. Keep explicit per-call overrides and outgoing requests unchanged. Fixes #44 --- CHANGELOG.md | 1 + lib/serpapi/client.rb | 2 +- spec/serpapi/client/symbolize_names_spec.rb | 85 +++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 spec/serpapi/client/symbolize_names_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index fa01a94..35583d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Changelog + * [Unreleased] Honor constructor `symbolize_names` defaults when decoding JSON responses * [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 14eeb94..e50d852 100644 --- a/lib/serpapi/client.rb +++ b/lib/serpapi/client.rb @@ -351,7 +351,7 @@ def handle_response(response, decoder, endpoint, params) end def process_json_response(response, endpoint, params) - symbolize = params.fetch(:symbolize_names, true) + symbolize = params.fetch(:symbolize_names) { @params.fetch(:symbolize_names, true) } begin data = JSON.parse(response.body, symbolize_names: symbolize) diff --git a/spec/serpapi/client/symbolize_names_spec.rb b/spec/serpapi/client/symbolize_names_spec.rb new file mode 100644 index 0000000..e49683e --- /dev/null +++ b/spec/serpapi/client/symbolize_names_spec.rb @@ -0,0 +1,85 @@ +require 'spec_helper' +require 'stringio' + +describe 'JSON parser option precedence' do + let(:transport) { double('HTTP client') } + let(:response) do + double( + status: 200, + headers: { 'Content-Type' => 'application/json' }, + body: '{"search_metadata":{"id":"offline"}}', + flush: nil + ) + end + + before do + allow(HTTP).to receive(:timeout).and_return(transport) + allow(transport).to receive(:get).and_return(response) + allow(transport).to receive(:post).and_return(response) + end + + [nil, true, false].each do |constructor_option| + [nil, true, false].each do |call_option| + it "decodes JSON with constructor #{constructor_option.inspect} and call #{call_option.inspect}" do + defaults = { persistent: false } + defaults[:symbolize_names] = constructor_option unless constructor_option.nil? + params = { q: 'coffee' } + params[:symbolize_names] = call_option unless call_option.nil? + client = SerpApi::Client.new(defaults) + symbolize = call_option.nil? ? constructor_option != false : call_option + key = symbolize ? :search_metadata : 'search_metadata' + nested_key = symbolize ? :id : 'id' + + expect(client.search(params)).to eq(key => { nested_key => 'offline' }) + expect(transport).to have_received(:get) do |_url, options| + expect(options.fetch(:params)).not_to have_key(:symbolize_names) + end + end + end + end + + it 'uses constructor defaults for archived searches' do + client = SerpApi::Client.new(persistent: false, symbolize_names: false) + + expect(client.search_archive('offline')).to have_key('search_metadata') + end + + it 'uses constructor defaults and flushes persistent responses' do + allow(transport).to receive(:persistent).and_return(transport) + client = SerpApi::Client.new(symbolize_names: false) + + expect(client.search(q: 'coffee')).to have_key('search_metadata') + expect(response).to have_received(:flush) + end + + it 'uses constructor defaults for account responses' do + client = SerpApi::Client.new(persistent: false, symbolize_names: false) + + expect(client.account).to have_key('search_metadata') + end + + it 'uses constructor defaults for image uploads without sending parser options' do + client = SerpApi::Client.new(persistent: false, symbolize_names: false) + + expect(client.upload_image(StringIO.new('image'))).to have_key('search_metadata') + expect(transport).to have_received(:post) do |_url, options| + expect(options.fetch(:form)).not_to have_key(:symbolize_names) + end + end + + it 'allows an image upload to override the constructor option' do + client = SerpApi::Client.new(persistent: false, symbolize_names: false) + + expect(client.upload_image(StringIO.new('image'), symbolize_names: true)).to have_key(:search_metadata) + end + + { html: 'text/html', md: 'text/markdown' }.each do |format, content_type| + it "keeps #{format} responses as text" do + allow(response).to receive(:headers).and_return('Content-Type' => content_type) + allow(response).to receive(:body).and_return('raw text') + client = SerpApi::Client.new(persistent: false, symbolize_names: false) + + expect(client.public_send(format, q: 'coffee')).to eq('raw text') + end + end +end