Skip to content

Commit

Permalink
Bring the facebook param fix to inclusive facets as well
Browse files Browse the repository at this point in the history
This allows users to pass in URLs in the format
http://[domain]?f_inclusive[format][0]=Book
  • Loading branch information
sandbergja authored and cbeer committed Mar 4, 2022
1 parent 46b3c0a commit 37dea4e
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 @@ -39,11 +39,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 @@ -192,6 +189,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 routable_model_for(blacklight_config)
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 @@ -48,10 +48,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 37dea4e

Please sign in to comment.