Project

General

Profile

« Previous | Next » 

Revision d477552c

Added by Daniel Lobato Garcia almost 6 years ago

Fixes #23382 - Hashes in arrays are shown properly on ENC

When defining smart class parameter values with type YAML and submitted
in GUI an Ruby error message gets prepended for each array entry.

Defining an YAML-Array like:

---
- name: foo
mount_point: /bar
- name: john
mount_point: /doe

is shown on the ENC as:

- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
name: foo
mount_point: "/bar"
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
name: john
mount_point: "/doe"

In order to avoid parsing the whole YAML or monkeypatching
HashWithIndifferentAccess the easiest workaround is to convert to
JSON then parse back to YAML

View differences:

app/controllers/hosts_controller.rb
begin
respond_to do |format|
# don't break lines in yaml to support Ruby < 1.9.3
host_info_yaml = @host.info.to_yaml(:line_width => -1)
# Remove the HashesWithIndifferentAccess using 'deep_stringify_keys',
# then we turn it into YAML
host_info_yaml = @host.info.deep_stringify_keys.to_yaml(:line_width => -1)
format.html { render :html => "<pre>#{ERB::Util.html_escape(host_info_yaml)}</pre>".html_safe }
format.yml { render :plain => host_info_yaml }
end
test/controllers/hosts_controller_test.rb
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, params: { :name => @host.name, :format => "yml" }, session: set_session_user
assert_response :success
as_admin { @enc = @host.info.to_yaml }
as_admin { @enc = @host.info.deep_stringify_keys.to_yaml(:line_width => -1) }
assert_equal @enc, response.body
end
test "externalNodes should render YAML hashes correctly" do
HostInfoProviders::PuppetInfo.any_instance.expects(:classes_info_hash).returns(
'dhcp' => {
'bootfiles' => [
{'name' => 'foo', 'mount_point' => '/bar'}.with_indifferent_access,
{'name' => 'john', 'mount_point' => '/doe'}.with_indifferent_access
]
}
).at_least_once
get :externalNodes, params: { :name => @host.name, :format => "yml" }, session: set_session_user
assert_response :success
as_admin { @enc = @host.info.deep_stringify_keys.to_yaml }
assert_equal @enc, response.body
end

Also available in: Unified diff