From 00a8b26e824188d0dce255c730412339352cd92c Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 10 Jan 2025 21:19:03 +0000 Subject: [PATCH] Monkey patch Object#as_json 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: https://github.com/rails/rails/issues/51626 --- config/initializers/00_extensions.rb | 4 ++++ lib/extensions/object_extension.rb | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 lib/extensions/object_extension.rb diff --git a/config/initializers/00_extensions.rb b/config/initializers/00_extensions.rb index a534bc80f7..4f3bba67ea 100644 --- a/config/initializers/00_extensions.rb +++ b/config/initializers/00_extensions.rb @@ -41,3 +41,7 @@ class TrueClass class FalseClass include Extensions::BooleanExtension::False end + +class Object + include Extensions::ObjectExtension +end diff --git a/lib/extensions/object_extension.rb b/lib/extensions/object_extension.rb new file mode 100644 index 0000000000..72b0d11f72 --- /dev/null +++ b/lib/extensions/object_extension.rb @@ -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