money-rails: exchange_to + to_json = Stackoverflow

In a rails 4 application (ruby 2.2.2 OS X) with money-rails master and money 6.6.1 (merged that PR in a fork) when you exchange into another currency and then call to_json on the money object you get SystemStackError: stack level too deep in ActiveSupport.

Money.new(1_00, 'EUR').to_json # => fine
Money.new(1_00, 'EUR').exchange_to('USD') # => fine
Money.new(1_00, 'EUR').exchange_to('USD').to_json # => Stackoverflow
# even worse now suddenly
Money.new(1_00, 'EUR').to_json # => Stackoverflow

So far I did not try out if this happens too on a currently published gem version with money 6.5. We use this setup because we use a custom exchange rate store with the interface from money 6.6. Could be another source for the issue.

The actual stacktrace is sadly not really that interesting

Exception: SystemStackError: stack level too deep
--
   0: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/numeric/conversions.rb:125:in `block (2 levels) in <class:Numeric>'
   1: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/range/conversions.rb:13:in `to_s'
   2: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/range/conversions.rb:13:in `to_formatted_s'
   3: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:134:in `as_json'

      ...

7838: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
7839: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `each'
7840: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `map'
7841: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `as_json'
7842: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:50:in `as_json'
7843: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
7844: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `each'
7845: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `map'
7846: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `as_json'
7847: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:50:in `as_json'
7848: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
7849: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `each'
7850: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `map'
7851: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:159:in `as_json'
7852: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:50:in `as_json'
7853: <rubypath>/gems/activesupport-4.2.4/lib/active_support/json/encoding.rb:35:in `encode'
7854: <rubypath>/gems/activesupport-4.2.4/lib/active_support/json/encoding.rb:22:in `encode'
7855: <rubypath>/gems/activesupport-4.2.4/lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@hiattp one other finding — it seems that some encoders will allow this behaviour. For example, Oj has this option — https://github.com/ohler55/oj/blob/master/pages/Options.md#use_to_hash-boolean.