Skip to content

Options hiding inside JSON now fail loudly - #47

Merged
nz merged 1 commit into
mainfrom
fix/as-json-raises
Aug 12, 2026
Merged

Options hiding inside JSON now fail loudly#47
nz merged 1 commit into
mainfrom
fix/as-json-raises

Conversation

@nz

@nz nz commented Aug 11, 2026

Copy link
Copy Markdown
Member

Fixes #43.

An Option at the top level already refuses to turn into JSON: it raises an error. But an Option hiding inside a hash or an array slipped through, and came out as strange JSON like {"value":5}. A missing value came out as {}. No error, no warning. Now it raises the same loud error no matter how deeply it is buried.

Detail

  • as_json on Option::Any and Result::Any raises SerializeError with the same messages as to_s/to_json. ActiveSupport's Hash#as_json/Array#as_json recurse via as_json, never to_json, which is how the existing guard was bypassed.
  • Result had the identical hole (Ok(5).as_json => {"value"=>5}), not mentioned in the issue; fixed alongside.
  • Defining as_json without ActiveSupport loaded is harmless, so it is unconditional.

Covered in test/rails_test.rb (hash and array nesting, Option and Result) rather than doctests, since doctests run without ActiveSupport.

@nz
nz enabled auto-merge August 11, 2026 00:20
lutzcc1
lutzcc1 previously approved these changes Aug 11, 2026
The serialization guard only held at the top level. ActiveSupport's
Hash#as_json and Array#as_json recurse through their members with
as_json, never to_json, so a nested Option reached Object#as_json and
serialized as its instance variables: {a: Some(5)}.to_json produced
{"a":{"value":5}} with no error, and a None became {}.

Define as_json on Option::Any and Result::Any to raise SerializeError
alongside to_s and to_json. Almost every real serialization path goes
through a Hash or an Array, so this is where the guard earns its keep.
@nz
nz merged commit da959e1 into main Aug 12, 2026
1 check passed
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.

Option: as_json not overridden — nested Options serialize to {"value":…} instead of raising

3 participants