Skip to content

Fix JsonValue double-encoding on Rails 8.1 - #42

Merged
ElMassimo merged 1 commit into
mainfrom
fix/rails-8-1-json-value-double-encoding
Jul 18, 2026
Merged

ElMassimo merged 1 commit into
mainfrom
fix/rails-8-1-json-value-double-encoding

Conversation

@ElMassimo

Copy link
Copy Markdown
Owner

Fixes #41.

Problem

On Rails 8.1, JsonValue is double-encoded when serialized through Hash#to_json / ActiveSupport::JSON.encode with no options:

{ value: OjSerializers::JsonValue.new('[{"a":1}]') }.to_json
# Rails 8.0: {"value":[{"a":1}]}
# Rails 8.1: {"value":"[{\"a\":1}]"}

Rails 8.1 memoizes an option-less encoder when ActiveSupport::JSON::Encoding.json_encoder= is assigned, and Oj::Rails::Encoder snapshots Oj.default_options at construction. setup.rb called Oj.optimize_rails (which assigns the encoder) before enabling use_raw_json, so the memoized encoder kept use_raw_json: false.

Fix

Enable use_raw_json before calling Oj.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_json path and fails on 8.1 without this change. Suite is green on Rails 7.2, 8.0, and 8.1.

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.
@ElMassimo
ElMassimo merged commit 85fb990 into main Jul 18, 2026
3 of 5 checks passed
@ElMassimo
ElMassimo deleted the fix/rails-8-1-json-value-double-encoding branch July 18, 2026 00:14
ElMassimo added a commit that referenced this pull request Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonValue double-encoded on Rails 8.1

1 participant