Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring the facebook param fix to inclusive facets as well #2654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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