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

Deep symbolize keys when parsing options #312

Merged
merged 1 commit into from
Feb 13, 2018
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
2 changes: 1 addition & 1 deletion lib/services/api/request_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.parse_user(data)

def self.parse_options(data)
raise BadRequestError, "Request is missing options" if data["options"].blank?
data["options"].symbolize_keys
data["options"].deep_symbolize_keys
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can API save the data in the same format as what UI does? Currently the data saved via UI is like this.

---
:src_ids:
- '7000000000029'
:vm_memory: 1024
:disk_add:
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  disk_size_in_mb: '1'
  persistent: true
  thin_provisioned: true
  new_controller_type: VirtualLsiLogicController
  dependent: true
  bootable: false
:request_type: :vm_reconfigure

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lfu we could possibly convert to yaml...however, at the moment it is not playing nice:

TypeError: no implicit conversion of Symbol into Integer
from /Users/jilliantullo/code/manageiq/app/models/mixins/miq_request_mixin.rb:4:in `[]'

After looking at it, it appears that the create_request method we are calling is expecting a hash.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jntullo options is a serialized column. So DB saves the options data in yaml format. I was hoping to keep the data format in DB the same as what UI does. Hash would work if the format can't be exactly the same. Currently all the callers use the options key as a symbol.

end

def self.parse_auto_approve(data)
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/services/api/request_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
expect { described_class.parse_options({}) }.to raise_error(Api::BadRequestError, /missing options/)
end

it "symbolizes keys" do
actual = described_class.parse_options("options" => {"foo" => "bar"})
expected = {:foo => "bar"}
it "deep symbolizes keys" do
actual = described_class.parse_options("options" => {"foo" => "bar", "foobar" => {"bazz" => "foo"}})
expected = {:foo => "bar", :foobar => {:bazz => "foo"}}
expect(actual).to eq(expected)
end
end
Expand Down