Skip to content

Commit e0c4e00

Browse files
committed
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
1 parent 786baa6 commit e0c4e00

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Changelog
2+
* [Unreleased] Honor constructor `symbolize_names` defaults when decoding JSON responses
23
* [Unreleased] Add native Extract API support via the `extract` method
34
* [Unreleased] Apply the `timeout` option to persistent connections
45
* [Unreleased] Add Image API support

‎lib/serpapi/client.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def handle_response(response, decoder, endpoint, params)
351351
end
352352

353353
def process_json_response(response, endpoint, params)
354-
symbolize = params.fetch(:symbolize_names, true)
354+
symbolize = params.fetch(:symbolize_names) { @params.fetch(:symbolize_names, true) }
355355

356356
begin
357357
data = JSON.parse(response.body, symbolize_names: symbolize)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require 'spec_helper'
2+
require 'stringio'
3+
4+
describe 'JSON parser option precedence' do
5+
let(:transport) { double('HTTP client') }
6+
let(:response) do
7+
double(
8+
status: 200,
9+
headers: { 'Content-Type' => 'application/json' },
10+
body: '{"search_metadata":{"id":"offline"}}',
11+
flush: nil
12+
)
13+
end
14+
15+
before do
16+
allow(HTTP).to receive(:timeout).and_return(transport)
17+
allow(transport).to receive(:get).and_return(response)
18+
allow(transport).to receive(:post).and_return(response)
19+
end
20+
21+
[nil, true, false].each do |constructor_option|
22+
[nil, true, false].each do |call_option|
23+
it "decodes JSON with constructor #{constructor_option.inspect} and call #{call_option.inspect}" do
24+
defaults = { persistent: false }
25+
defaults[:symbolize_names] = constructor_option unless constructor_option.nil?
26+
params = { q: 'coffee' }
27+
params[:symbolize_names] = call_option unless call_option.nil?
28+
client = SerpApi::Client.new(defaults)
29+
symbolize = call_option.nil? ? constructor_option != false : call_option
30+
key = symbolize ? :search_metadata : 'search_metadata'
31+
nested_key = symbolize ? :id : 'id'
32+
33+
expect(client.search(params)).to eq(key => { nested_key => 'offline' })
34+
expect(transport).to have_received(:get) do |_url, options|
35+
expect(options.fetch(:params)).not_to have_key(:symbolize_names)
36+
end
37+
end
38+
end
39+
end
40+
41+
it 'uses constructor defaults for archived searches' do
42+
client = SerpApi::Client.new(persistent: false, symbolize_names: false)
43+
44+
expect(client.search_archive('offline')).to have_key('search_metadata')
45+
end
46+
47+
it 'uses constructor defaults and flushes persistent responses' do
48+
allow(transport).to receive(:persistent).and_return(transport)
49+
client = SerpApi::Client.new(symbolize_names: false)
50+
51+
expect(client.search(q: 'coffee')).to have_key('search_metadata')
52+
expect(response).to have_received(:flush)
53+
end
54+
55+
it 'uses constructor defaults for account responses' do
56+
client = SerpApi::Client.new(persistent: false, symbolize_names: false)
57+
58+
expect(client.account).to have_key('search_metadata')
59+
end
60+
61+
it 'uses constructor defaults for image uploads without sending parser options' do
62+
client = SerpApi::Client.new(persistent: false, symbolize_names: false)
63+
64+
expect(client.upload_image(StringIO.new('image'))).to have_key('search_metadata')
65+
expect(transport).to have_received(:post) do |_url, options|
66+
expect(options.fetch(:form)).not_to have_key(:symbolize_names)
67+
end
68+
end
69+
70+
it 'allows an image upload to override the constructor option' do
71+
client = SerpApi::Client.new(persistent: false, symbolize_names: false)
72+
73+
expect(client.upload_image(StringIO.new('image'), symbolize_names: true)).to have_key(:search_metadata)
74+
end
75+
76+
{ html: 'text/html', md: 'text/markdown' }.each do |format, content_type|
77+
it "keeps #{format} responses as text" do
78+
allow(response).to receive(:headers).and_return('Content-Type' => content_type)
79+
allow(response).to receive(:body).and_return('raw text')
80+
client = SerpApi::Client.new(persistent: false, symbolize_names: false)
81+
82+
expect(client.public_send(format, q: 'coffee')).to eq('raw text')
83+
end
84+
end
85+
end

0 commit comments

Comments
 (0)