Skip to content

Fix behavior of trying to parse non-string objects #499

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

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

YuheiNakasaka
Copy link
Contributor

@YuheiNakasaka YuheiNakasaka commented Jun 3, 2022

Some Object may return true for respond_to?(:to_str) but actually return nil instead of String when to_str is called.

A good example is the Rails encrypted credentials in Rails 7.0, an object called InheritableOptions that extends Hash.
InheritableOptions implements method_missing and returns true for respond_to?(:to_str) but nil when actually calling to_str.
https://github.com/rails/rails/blob/7-0-stable/activesupport/lib/active_support/ordered_options.rb#L43

Therefore, the following implementation will cause an error.

h = ActiveSupport::InheritableOptions.new({ a: 1, b: 2 })
h.is_a? Hash #=> true
JSON(h) #=> TypeError: no implicit conversion of nil into String

This PR implements strict checking of arguments to avoid this kind of error.

@byroot byroot force-pushed the fix-behavior-to-parse-object branch from f675bd9 to e2e9936 Compare October 17, 2024 17:57
@byroot byroot merged commit e9dfe1d into ruby:master Oct 17, 2024
73 checks passed
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

Successfully merging this pull request may close these issues.

2 participants