Skip to content

Commit

Permalink
Merge pull request #2655 from projectblacklight/backport-2654
Browse files Browse the repository at this point in the history
Backport #2654: Bring the facebook param fix to inclusive facets as well
  • Loading branch information
jcoyne authored Mar 4, 2022
2 parents 242880e + 011d1e3 commit 1dedb3c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/blacklight/search_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ def self.normalize_params(untrusted_params = {})
end

# Normalize facet parameters mangled by facebook
if params[:f].is_a?(Hash) && params[:f].values.any? { |x| x.is_a?(Hash) }
params[:f] = params[:f].transform_values do |value|
value.is_a?(Hash) ? value.values : value
end
end
params[:f] = normalize_facet_params(params[:f]) if facet_params_need_normalization(params[:f])
params[:f_inclusive] = normalize_facet_params(params[:f_inclusive]) if facet_params_need_normalization(params[:f_inclusive])

params
end
Expand Down Expand Up @@ -251,6 +248,16 @@ def facet_prefix
params[facet_request_keys[:prefix]]
end

def self.facet_params_need_normalization(facet_params)
facet_params.is_a?(Hash) && facet_params.values.any? { |x| x.is_a?(Hash) }
end

def self.normalize_facet_params(facet_params)
facet_params.transform_values do |value|
value.is_a?(Hash) ? value.values : value
end
end

private

def search_field_key
Expand Down
6 changes: 5 additions & 1 deletion spec/lib/blacklight/search_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@
end

context 'with facebooks badly mangled query parameters' do
let(:params) { { f: { field: { '0': 'first', '1': 'second' } } } }
let(:params) do
{ f: { field: { '0': 'first', '1': 'second' } },
f_inclusive: { field: { '0': 'first', '1': 'second' } } }
end

it 'normalizes the facets to the expected format' do
expect(search_state.to_h).to include f: { field: %w[first second] }
expect(search_state.to_h).to include f_inclusive: { field: %w[first second] }
end
end

Expand Down

0 comments on commit 1dedb3c

Please sign in to comment.