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

Weird params behavior #1828

Closed
usern3 opened this issue Oct 20, 2022 · 7 comments
Closed

Weird params behavior #1828

usern3 opened this issue Oct 20, 2022 · 7 comments

Comments

@usern3
Copy link

usern3 commented Oct 20, 2022

I was trying to modify the params I was receiving and I was struggling to convert the keys to symbols. I eventually had to settle on params.to_h.transform_keys(&:to_sym) to get it to work. Now, I could've sworn I've never had to do this in the past, so potentially I screwed something up, I just have no idea what. If it isn't an error on my end though, here's some of my output:

#567354) params.is_a? Hash
true
(ruby@puma: cluster worker 1: 567080 [backend]#567354) params.transform_keys(&:to_sym)
{"name"=>"Device 1",
 "device_id"=>"D1234",
 "scheduled_at"=>"2022-10-19 18:30:08 +0000",
 "location"=>"office1",
 "first_name"=>"dsf",
 "last_name"=>"zdsfgdf",
 "password"=>"dsgdf",
 "email"=>"dsfgsd@sdfs.com"}

(ruby@puma: cluster worker 1: 567080 [backend]#567354) params.transform_keys!(&:to_sym)
{"name"=>"Device 1",
 "device_id"=>"D1234",
 "scheduled_at"=>"2022-10-19 18:30:08 +0000",
 "location"=>"office1",
 "first_name"=>"dsf",
 "last_name"=>"zdsfgdf",
 "email"=>"dsfgsd@sdfs.com"}

(ruby@puma: cluster worker 1: 567080 [backend]#567354) params.symbolize_keys(&:to_sym)
{"name"=>"Device 1",
 "device_id"=>"D1234",
 "scheduled_at"=>"2022-10-19 18:30:08 +0000",
 "first_name"=>"dsf",
 "last_name"=>"zdsfgdf",
 "password"=>"dsgdf",
 "email"=>"dsfgsd@sdfs.com"}

(ruby@puma: cluster worker 0: 567080 [backend]#567813) params.to_h.transform_keys(&:to_sym)
{:name=>"Device 1",
 :device_id=>"D1234",
 :scheduled_at=>"2022-10-19 18:30:08 +0000",
 :location=>"office1",
 :first_name=>"dsf",
 :last_name=>"zdsfgdf",
 :password=>"dsgdf",
 :email=>"dsfgsd@sdfs.com"}
@dentarg
Copy link
Member

dentarg commented Oct 20, 2022

params is not a real hash, see https://github.com/sinatra/sinatra/blob/v3.0.2/lib/sinatra/indifferent_hash.rb

some more info in #1592

@usern3
Copy link
Author

usern3 commented Oct 21, 2022

params is not a real hash, see https://github.com/sinatra/sinatra/blob/v3.0.2/lib/sinatra/indifferent_hash.rb

some more info in #1592

Okay I see now. I'm still confused as to why params.transform_keys(&:to_sym) doesn't work though since IndifferentHash seems to have transform_keys methods?

    def transform_keys(&block)
      dup.transform_keys!(&block)
    end

    def transform_keys!
      super
      super(&method(:convert_key))
    end

@dentarg
Copy link
Member

dentarg commented Oct 21, 2022

convert_key always converts keys to strings

@usern3
Copy link
Author

usern3 commented Oct 21, 2022

convert_key always converts keys to strings

ah gotcha okay thanks for clearing that up.

@usern3 usern3 closed this as completed Oct 27, 2022
@NobodysNightmare
Copy link

Now, I could've sworn I've never had to do this in the past, so potentially I screwed something up

Looking at the timing you brought this up, did your code include a select or reject call before using transform_keys in the past?

I was wondering the same (some code broke after an upgrade) and it turns out that these methods did return regular hashes before the upgrade (and thus transform_keys worked) but return an indifferent hash after the upgrade. See #1711

@usern3
Copy link
Author

usern3 commented Nov 22, 2022

Looking at the timing you brought this up, did your code include a select or reject call before using transform_keys in the past?

I don't recall using select or reject previously. So, if you used one of those before it returned a regular ruby hash?

@NobodysNightmare
Copy link

NobodysNightmare commented Nov 22, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants