-
Notifications
You must be signed in to change notification settings - Fork 257
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
Use a custom YAML coder to restore backwards-compatible deserialization of serialized query parameters #2770
Merged
+91
−21
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b8ed4a
Use a custom YAML coder to restore backwards-compatible deserializati…
cbeer 367f1b6
test against updated Rails dependencies
barmintor d2c4519
update search_spec to cover query_params scenarios
barmintor 5abe70d
Update app/models/search.rb
cbeer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
# This is a custom YAML coder for (de)serializing blacklight search parameters that | ||
# supports deserializing HashWithIndifferentAccess parameters (as was historically done by Blacklight). | ||
class SearchParamsYamlCoder | ||
# Serializes an attribute value to a string that will be stored in the database. | ||
def self.dump(obj) | ||
# Convert HWIA to an ordinary hash so we have some hope of using the regular YAML encoder in the future | ||
obj = obj.to_hash if obj.is_a?(ActiveSupport::HashWithIndifferentAccess) | ||
|
||
YAML.dump(obj) | ||
end | ||
|
||
# Deserializes a string from the database to an attribute value. | ||
def self.load(yaml) | ||
return yaml unless yaml.is_a?(String) && yaml.start_with?("---") | ||
|
||
params = yaml_load(yaml) | ||
|
||
params.with_indifferent_access | ||
end | ||
|
||
# rubocop:disable Security/YAMLLoad | ||
if YAML.respond_to?(:unsafe_load) | ||
def self.yaml_load(payload) | ||
if ActiveRecord.try(:use_yaml_unsafe_load) || ActiveRecord::Base.try(:use_yaml_unsafe_load) | ||
YAML.unsafe_load(payload) | ||
else | ||
permitted_classes = (ActiveRecord.try(:yaml_column_permitted_classes) || ActiveRecord::Base.try(:yaml_column_permitted_classes) || []) + | ||
Blacklight::Engine.config.blacklight.search_params_permitted_classes | ||
YAML.safe_load(payload, permitted_classes: permitted_classes, aliases: true) | ||
end | ||
end | ||
else | ||
def self.yaml_load(payload) | ||
if ActiveRecord.try(:use_yaml_unsafe_load) || ActiveRecord::Base.try(:use_yaml_unsafe_load) | ||
YAML.load(payload) | ||
else | ||
permitted_classes = (ActiveRecord.try(:yaml_column_permitted_classes) || ActiveRecord::Base.try(:yaml_column_permitted_classes) || []) + | ||
Blacklight::Engine.config.blacklight.search_params_permitted_classes | ||
YAML.safe_load(payload, permitted_classes: permitted_classes, aliases: true) | ||
end | ||
end | ||
end | ||
# rubocop:enable Security/YAMLLoad | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Search do | ||
let(:search) { described_class.new(user: user) } | ||
let(:user) { User.create! email: '[email protected]', password: 'xyz12345' } | ||
let(:hash_params) { { q: "query", f: { facet: "filter" } } } | ||
let(:query_params) { hash_params } | ||
|
||
describe "query_params" do | ||
before do | ||
@search = described_class.new(user: user) | ||
@query_params = { q: "query", f: "facet" } | ||
shared_examples "persisting query_params" do | ||
it "can save and retrieve the hash" do | ||
search.query_params = query_params | ||
search.save! | ||
expect(described_class.find(search.id).query_params).to eq query_params.with_indifferent_access | ||
end | ||
end | ||
|
||
it "can save and retrieve the hash" do | ||
@search.query_params = @query_params | ||
@search.save! | ||
expect(described_class.find(@search.id).query_params).to eq @query_params | ||
context "are an indifferent hash" do | ||
include_context "persisting query_params" do | ||
let(:query_params) { hash_params.with_indifferent_access } | ||
end | ||
end | ||
|
||
context "are a string-keyed hash" do | ||
include_context "persisting query_params" do | ||
let(:query_params) { hash_params.with_indifferent_access.to_hash } | ||
end | ||
end | ||
|
||
context "include symbol keys" do | ||
include_context "persisting query_params" do | ||
let(:query_params) { hash_params } | ||
end | ||
end | ||
end | ||
|
||
describe "saved?" do | ||
it "is true when user_id is not NULL and greater than 0" do | ||
@search = described_class.new(user: user) | ||
@search.save! | ||
|
||
expect(@search).to be_saved | ||
search.save! | ||
expect(search).to be_saved | ||
end | ||
|
||
it "is false when user_id is NULL or less than 1" do | ||
@search = described_class.create | ||
expect(@search).not_to be_saved | ||
context "when user_id is NULL or less than 1" do | ||
let(:search) { described_class.create } | ||
|
||
it "is false" do | ||
expect(search).not_to be_saved | ||
end | ||
end | ||
end | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a comment explaining why this is necessary, and that it becomes no longer necessary if in a Rails version including rails/rails#45591
(If that rails gets merged, as it looks like it will, then at some future point where BL only supports Rails versions where the latest patch includes that rails change, this custom coder will no longer be necessary)
Or the comment could be down with the custom coder class itself, maybe.