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

undefined method `to_str' for 🆔Symbol (NoMethodError) in 2.8.2 #498

Closed
johrstrom opened this issue Apr 3, 2023 · 8 comments
Closed

Comments

@johrstrom
Copy link

johrstrom commented Apr 3, 2023

Hi, I'm getting this error when I've updated to 2.8.2 from 2.8.1. Here's a very simple test case in irb that I've confirmed works in 2.8.1.

irb(main):004:0> uri = Addressable::Template.new("https://example/some/api/{?query*}")
=> #<Addressable::Template:0x104 PATTERN:https://example/some/api/{?query*}>
irb(main):005:0> uri.expand({'query' => {id: 'abc123' } })
/home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:900:in `normalize_value': undefined method `to_str' for :id:Symbol (NoMethodError)
Did you mean?  to_se-2.8.2.gem
               to_symx-5.0.1.gem
Successffrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:908:in `block in normalize_value'
Successffrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:907:in `each'
Parsing from /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:907:in `inject'
Installifrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:907:in `normalize_value'
Parsing from /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:774:in `block in transform_capture'
Installifrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:756:in `each'
Done insfrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:756:in `inject'
2 gems ifrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:756:in `transform_capture'
[dashboafrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:595:in `block in expand'
irb(mainfrom /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:594:in `gsub!'
=> true from /home/jeff/.gems/ruby-3.0.3/gems/addressable-2.8.2/lib/addressable/template.rb:594:in `expand'
irb(mainfrom (irb):5:in `<main>'mplate.new("#{server[:host]}{/segments*}/{?query*}")
        from /home/jeff/apps/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
        from /home/jeff/apps/ruby/3.0.3/bin/irb:23:in `load'
        from /home/jeff/apps/ruby/3.0.3/bin/irb:23:in `<main>'

Here's a simple snippet that you may be able to just copy paste easier.

require 'addressable'

uri = Addressable::Template.new("https://example/some/api/{?query*}")
uri.expand({'query' => { id: 'abc123' }})
@johrstrom
Copy link
Author

johrstrom commented Apr 3, 2023

Obviously it's due to passing a hash with symbol keys instead of string keys. Just using string keys works, in case other folks run into this and need a work around.

require 'addressable'

uri = Addressable::Template.new("https://example/some/api/{?query*}")
uri.expand({'query' => { 'id' => 'abc123' }})

@dentarg
Copy link
Collaborator

dentarg commented Apr 3, 2023

@jarthod mind taking a look?

I guess the template tests does not use symbols?

@johrstrom
Copy link
Author

johrstrom commented Apr 3, 2023

I was also able to reproduce with integers as values, so I think it affects keys and values, just FYI.

require 'addressable'

uri = Addressable::Template.new("https://example/some/api/{?query*}")
uri.expand({'query' => { 'id' => 1 }})

@dentarg
Copy link
Collaborator

dentarg commented Apr 3, 2023

Looks like to_s was always called before #492 (https://github.com/sporkmonger/addressable/pull/492/files#diff-fb36d3dc67e6565ffde17e666a98697f48e76dac38fabf1bb9e97cdf3b583d76)

# Unicode normalization form KC.
def self.unicode_normalize_kc(input)
input = input.to_s unless input.is_a?(String)

def self.unicode_normalize_kc(value)
IDN::Stringprep.nfkc_normalize(value.to_s)
end

@johrstrom
Copy link
Author

I can submit a patch if here you like, though given it's worked for the past 11 years, I'm not sure what unintended side affects it may cause. Someone with more intimate knowledge of the callers may know more?

value = value.respond_to?(:to_ary) ? value.to_ary : value.to_str

@jarthod
Copy link
Contributor

jarthod commented Apr 4, 2023

I am having a look, I saw specs checking both symbols, string, and integers so I thought this was covered but apparently they were only testing this at the first level, and it turns out there's a special treatment at this level, so I'll add a couple more specs testing this at the second level and provide a patch. From what I can see putting back a to_s at the right place will do the trick yes.

Edit: Fix merged in PR #499, sorry for the regression 🙇

@crevete
Copy link

crevete commented Apr 4, 2023

@dentarg just want to know when the 2.8.3 tag will be cut? thanks

@dentarg
Copy link
Collaborator

dentarg commented Apr 4, 2023

2.8.3 is out now

@dentarg dentarg unpinned this issue Apr 9, 2023
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

4 participants