Skip to content

Build headers_json with JSON.generate so json 3 can't fail every request - #28

Merged
PetrHeinz merged 2 commits into
mainfrom
claude/json-generate
Oct 2, 2026
Merged

PetrHeinz merged 2 commits into
mainfrom
claude/json-generate

Conversation

@PetrHeinz

Copy link
Copy Markdown
Member

With json 3 installed next to an ActiveSupport that still passes quirks_mode: to JSON.generate (older than 7.2.4, and every 8.0 release), any direct #to_json call goes through ActiveSupport's encoder and raises, because json 3 rejects that keyword. HTTPRequest and HTTPResponse build headers_json with #to_json, so HTTPEvents raises on every request. From the red-team of 0.2.8: with json ≥ 3 on Rails ≤ 8.0 every request returned a 500 and nothing was logged, while a stock app without the gem still served its pages.

  • HTTPRequest and HTTPResponse build headers_json with ::JSON.generate. These were the only to_json calls in lib.
  • The new tests stub Hash#to_json the way ActiveSupport ≤ 8.0 behaves under json 3: a direct call raises, while a call from inside JSON.generate (which passes a JSON::State) goes through to json.

Behaviour and compatibility:

  • Without ActiveSupport the output is byte-for-byte the same.
  • With ActiveSupport loaded, headers_json no longer escapes <, > and & as <, > and & (ActiveSupport's escape_html_entities_in_json). The JSON means the same after parsing.

Targets the logtail-rack 0.2.9 patch release. No dependencies on the other open PRs. The core gem's own to_json calls (STDOUT logging, error backtraces) are changed separately in logtail-ruby.

The first commit only adds the tests and is expected to fail on CI; the fix follows in the next commit.

🤖 Generated with Claude Code

With json 3 and ActiveSupport 8.0 or older, a direct #to_json call goes
through ActiveSupport's encoder, which passes quirks_mode: to
JSON.generate and json 3 rejects it. HTTPRequest and HTTPResponse build
headers_json with #to_json, so every request raises there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ActiveSupport older than 7.2.4 and every 8.0 release routes a direct
#to_json call through its own encoder, which passes quirks_mode: to
JSON.generate. json 3 rejects that keyword, so with both installed every
request raised in HTTPEvents. JSON.generate encodes the headers Hash
natively. With ActiveSupport loaded, <, > and & are no longer escaped as
<, > and &; the JSON means the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PetrHeinz
PetrHeinz marked this pull request as ready for review October 2, 2026 09:18
@PetrHeinz
PetrHeinz merged commit f0f1dce into main Oct 2, 2026
24 checks passed
@PetrHeinz
PetrHeinz deleted the claude/json-generate branch October 2, 2026 11:34
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.

1 participant