Skip to content

Commit

Permalink
Monkey patch Object#as_json
Browse files Browse the repository at this point in the history
After upgrading to Rails 7.1 there were persistent 'stack level too deep' with
some partials. This was traced back to:

  https://github.com/rails/rails/blob/fa9cf269191c5077de1abdd1e3f934fbeaf2a5d0/activesupport/lib/active_support/core_ext/object/json.rb#L58-L66

Changing this monkey patch slightly prevents the errors. Ideally, this will be
removed when a better solution is found.

See: rails/rails#51626
  • Loading branch information
jrmhaig committed Jan 10, 2025
1 parent f6eb4e1 commit 00a8b26
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/initializers/00_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ class TrueClass
class FalseClass
include Extensions::BooleanExtension::False
end

class Object
include Extensions::ObjectExtension
end
11 changes: 11 additions & 0 deletions lib/extensions/object_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Extensions
module ObjectExtension
def as_json(options = nil)
if respond_to?(:to_hash)
to_hash.as_json(options)
else
self.to_s
end
end
end
end

0 comments on commit 00a8b26

Please sign in to comment.