Allow non-hash values in config files
Fix: https://github.com/rails/rails/issues/37800
This commit is contained in:
parent
f2b69d80ab
commit
21c7199c0f
@ -220,27 +220,25 @@ def message_verifier(verifier_name)
|
||||
# config.middleware.use ExceptionNotifier, config_for(:exception_notification)
|
||||
# end
|
||||
def config_for(name, env: Rails.env)
|
||||
if name.is_a?(Pathname)
|
||||
yaml = name
|
||||
else
|
||||
yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml")
|
||||
end
|
||||
yaml = name.is_a?(Pathname) ? name : Pathname.new("#{paths["config"].existent.first}/#{name}.yml")
|
||||
|
||||
if yaml.exist?
|
||||
require "erb"
|
||||
config = YAML.load(ERB.new(yaml.read).result, symbolize_names: true) || {}
|
||||
config = (config[:shared] || {}).deep_merge(config[env.to_sym] || {})
|
||||
all_configs = YAML.load(ERB.new(yaml.read).result, symbolize_names: true) || {}
|
||||
config, shared = all_configs[env.to_sym], all_configs[:shared]
|
||||
|
||||
ActiveSupport::OrderedOptions.new.tap do |options|
|
||||
options.update(config)
|
||||
if config.is_a?(Hash)
|
||||
ActiveSupport::OrderedOptions.new.update(shared&.deep_merge(config) || config)
|
||||
else
|
||||
config || shared
|
||||
end
|
||||
else
|
||||
raise "Could not load configuration. No such file - #{yaml}"
|
||||
end
|
||||
rescue Psych::SyntaxError => e
|
||||
rescue Psych::SyntaxError => error
|
||||
raise "YAML syntax error occurred while parsing #{yaml}. " \
|
||||
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
|
||||
"Error: #{e.message}"
|
||||
"Error: #{error.message}"
|
||||
end
|
||||
|
||||
# Stores some of the Rails initial environment parameters which
|
||||
|
@ -1958,6 +1958,22 @@ class D < C
|
||||
assert_equal({ baz: 1 }, actual[:bar])
|
||||
end
|
||||
|
||||
test "config_for does not assume config is a hash" do
|
||||
app_file "config/custom.yml", <<~RUBY
|
||||
development:
|
||||
- foo
|
||||
- bar
|
||||
RUBY
|
||||
|
||||
add_to_config <<~RUBY
|
||||
config.my_custom_config = config_for('custom')
|
||||
RUBY
|
||||
|
||||
app "development"
|
||||
|
||||
assert_equal %w( foo bar ), Rails.application.config.my_custom_config
|
||||
end
|
||||
|
||||
test "config_for uses the Pathname object if it is provided" do
|
||||
app_file "config/custom.yml", <<-RUBY
|
||||
development:
|
||||
@ -1985,19 +2001,19 @@ class D < C
|
||||
assert_equal "Could not load configuration. No such file - #{app_path}/config/custom.yml", exception.message
|
||||
end
|
||||
|
||||
test "config_for without the environment configured returns an empty hash" do
|
||||
app_file "config/custom.yml", <<-RUBY
|
||||
test:
|
||||
key: 'custom key'
|
||||
test "config_for without the environment configured returns nil" do
|
||||
app_file "config/custom.yml", <<~RUBY
|
||||
test:
|
||||
key: 'custom key'
|
||||
RUBY
|
||||
|
||||
add_to_config <<-RUBY
|
||||
add_to_config <<~RUBY
|
||||
config.my_custom_config = config_for('custom')
|
||||
RUBY
|
||||
|
||||
app "development"
|
||||
|
||||
assert_equal({}, Rails.application.config.my_custom_config)
|
||||
assert_nil Rails.application.config.my_custom_config
|
||||
end
|
||||
|
||||
test "config_for implements shared configuration as secrets case found" do
|
||||
@ -2055,17 +2071,16 @@ class D < C
|
||||
assert_equal({ baz: 1, qux: 2 }, Rails.application.config.my_custom_config[:foo][:bar])
|
||||
end
|
||||
|
||||
test "config_for with empty file returns an empty hash" do
|
||||
app_file "config/custom.yml", <<-RUBY
|
||||
RUBY
|
||||
test "config_for with empty file returns nil" do
|
||||
app_file "config/custom.yml", ""
|
||||
|
||||
add_to_config <<-RUBY
|
||||
add_to_config <<~RUBY
|
||||
config.my_custom_config = config_for('custom')
|
||||
RUBY
|
||||
|
||||
app "development"
|
||||
|
||||
assert_equal({}, Rails.application.config.my_custom_config)
|
||||
assert_nil Rails.application.config.my_custom_config
|
||||
end
|
||||
|
||||
test "config_for containing ERB tags should evaluate" do
|
||||
|
Loading…
Reference in New Issue
Block a user