Repository navigation
Fix JsonValue double-encoding on Rails 8.1 - #42
Merged
Merged
Conversation
Rails 8.1 memoizes an option-less encoder when the JSON encoder is assigned via `Oj.optimize_rails`, and `Oj::Rails::Encoder` snapshots `Oj.default_options` at construction. Enabling `use_raw_json` afterwards left that encoder with `use_raw_json: false`, double-encoding `JsonValue` when serialized through `Hash#to_json` / `ActiveSupport::JSON.encode`. Set `use_raw_json` first, and add Rails 8.1 to the CI matrix so the existing `JsonValue` coverage exercises the affected path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #41.
Problem
On Rails 8.1,
JsonValueis double-encoded when serialized throughHash#to_json/ActiveSupport::JSON.encodewith no options:Rails 8.1 memoizes an option-less encoder when
ActiveSupport::JSON::Encoding.json_encoder=is assigned, andOj::Rails::EncodersnapshotsOj.default_optionsat construction.setup.rbcalledOj.optimize_rails(which assigns the encoder) before enablinguse_raw_json, so the memoized encoder keptuse_raw_json: false.Fix
Enable
use_raw_jsonbefore callingOj.optimize_rails.Tests
Added Rails 8.1 to the CI matrix. The existing "should not double encode JsonValue" spec already covers the option-less
to_jsonpath and fails on 8.1 without this change. Suite is green on Rails 7.2, 8.0, and 8.1.