-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2772 from projectblacklight/yaml-column-encoder-main
Use a custom YAML coder to restore backwards-compatible deserialization of serialized query parameters
- Loading branch information
Showing
5 changed files
with
88 additions
and
19 deletions.
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