-
-
Notifications
You must be signed in to change notification settings - Fork 253
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
#as_json ignored on Rails 7.2.0 #934
Comments
Sigh, Rails is a moving target. I'll look into it. Thanks for the test case. That helps a lot. |
This change looks like the most likely culprit (at least to me), right now: rails/rails@8e251a0 Might be a red herring, though. There are similar issues on other projects, though: rails/rails#52519 |
Looks like a likely cause. Just highlights the dangers of monkey patching. This will take some head scratching to find a work around. |
I'm guessing the issue is caused by redefining Line 908 in f5e928a
Would it be an option to define a new module with the method on it and include it in require 'json'
Object.ancestors
# => [Object, JSON::Ext::Generator::GeneratorMethods::Object, PP::ObjectMixin, Kernel, BasicObject] There would still be the problem of requiring everything in the right order, but I guess if gem 'oj', require: 'oj/mimic_json' # Make sure this comes before the rails gem
gem 'rails' Admittedly, these are all not quite as elegant as the current way. The other way I could think of is prepending the module to |
Rail 7.1 uses Rail 7.2 uses In Rails 7.1, active_support's to_json would be overridden at the last. However, in Rails 7.2, seems it is not. |
Not sure if this is correct, but it seems to work with the following changes: diff --git a/ext/oj/mimic_json.c b/ext/oj/mimic_json.c
index 35ad089..1bf6757 100644
--- a/ext/oj/mimic_json.c
+++ b/ext/oj/mimic_json.c
@@ -756,7 +756,8 @@ static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
oj_out_init(&out);
out.omit_nil = copts.dump_opts.omit_nil;
- copts.mode = CompatMode;
+ // The mode in oj_default_options will be changed to RailMode by rails_mimic_json
+ // copts.mode = CompatMode;
copts.to_json = No;
if (1 <= argc && Qnil != argv[0]) {
oj_parse_mimic_dump_options(argv[0], &copts);
diff --git a/ext/oj/rails.c b/ext/oj/rails.c
index 91fed0d..d2a04a8 100644
--- a/ext/oj/rails.c
+++ b/ext/oj/rails.c
@@ -803,7 +803,7 @@ rails_mimic_json(VALUE self) {
}
oj_mimic_json_methods(json);
// Setting the default mode breaks the prmoise in the docs not to.
- // oj_default_options.mode = RailsMode;
+ oj_default_options.mode = RailsMode;
return Qnil;
} |
Does that change break other tests? Does it break Rails 7.1 support? |
This way it would always be necessary to call both
With Rails 7.1, it looks like only |
Maybe, it would be better not to override |
I think it is no problem on Rails with above plane.
However, for above code, we may want to override the |
It may be somewhat evil, but it may be better if to_json is defined when ActiveSupport is not existed. => 1bda7ab |
The check for ActiveSupport might be best. |
Can confirm that this fixes the issue on Rails 7.2 👍 |
Any chance of a new release with this commit in it? The way this breaks applications on rails 7.2 is pretty hard to debug (it turns out a dependency was including oj and setting |
I'll release tonight. |
Released. |
Reading through the other issues, I'm sorry to break the news, but it looks like your favorite issue with
#to_json
and#as_json
is back with the recent release of Rails 7.2.0. The issue doesn't show up with Rails 7.1.x, but looking through the Rails source code it isn't immediately apparent what changed.The issue shows up upon calling
Oj.mimic_JSON
. Adding or removingOj.optimize_rails
, doesn't change the behavior.Expected output:
Actual output:
Thanks a lot for this gem! I've been using it without any problems for years now 💛
The text was updated successfully, but these errors were encountered: