oj: Recent versions (3.11.4 and 3.11.5) are causing issues with the Pact library
👋 I’ll say up front that this was difficult to narrow down, and I’m still not certain of the root cause. It’s unclear to me whether it’s ultimately on the Oj or Pact side.
Problem
After updating to Oj 3.11.4 and 3.11.5, we are seeing intermittent test failures for Pact examples. The failures appear to be a result of how objects are serialized. On oj 3.11.3, responses present like:
{ "some_field" => #<Pact::SomethingLike...> }
But after upgrading certain responses later in test runs present like:
{ "some_field" => { "json_class" => "Pact::SomethingLike", ... }
Note: the issue is currently presenting itself for certain RSpec “seeds” and often happens after some successfully and correctly serialized response bodies. For example, the first Pact test will run correctly and successfully and a subsequent one is serialized incorrectly as shown above which results in failure.
Thank you for your work on this library, and please let me know if I can provide more information. Unfortunately at this time, I’m not certain how to reproduce the issue outside of our code and test suite, but I’m hopefully the reporting it may trigger some thought on recent changes.
» ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
» rails -v
Rails 6.1.3.1
See also https://github.com/pact-foundation/pact-ruby/issues/240
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (11 by maintainers)
Revisiting this issue again. đź‘‹
I am able to resolve the issue by setting
Oj.default_options = { create_additions: true }after Oj enters:compatmode. Was there a behavior change in how “create additions” functioned after v3.11.3?I’ve updated the Gist with an example: https://gist.github.com/zorab47/f00e4ad8d5b02b583c2ba97db6011f67#file-oj-pact-reproduction-script-rb-L106-L126
There is not an easy way. There might be some way of checking symbols and classes but nothing obvious. The way to disable is to not call the to_json or as_json methods on objects. That can be done by calling Oj.dump with the mode set to something like
:customand making sure:use_to_jsonand:use_as_jsonare false.I’ve reproduced the issue in the Ruby file oj-blueprinter-pact-reproduction-script.rb. The issue is likely due to an interaction the Blueprinter library is doing with Oj. It will require additional research.
The top of the file is set up to toggle between Oj versions 3.11.3 and 3.11.5 to show the issue when Oj 3.11.5 is used.
https://gist.github.com/zorab47/f00e4ad8d5b02b583c2ba97db6011f67
cc @bethesque
Oj does monkey patch when in mimic mode. It has to to act like the JSON gem. That is the only case though. Normally I avoid money patching like a plague.