Skip to content

Raise GeneratorError for colliding keys with rfc8785 and no as_json - #1155

Closed
Branson404 wants to merge 1 commit into
ruby:masterfrom
Branson404:rfc8785-colliding-keys
Closed

Branson404 wants to merge 1 commit into
ruby:masterfrom
Branson404:rfc8785-colliding-keys

Conversation

@Branson404

Copy link
Copy Markdown

Fixes #1152

With rfc8785: true, a hash whose keys collide once stringified, such as { "1" => 1, 1 => 2 }, raised NoMethodError: undefined method 'call' for nil (or for false with a JSON::Coder created without a block) instead of JSON::GeneratorError. on_duplicate_key re-coerced every key through as_json, but there is no as_json converter in those configurations.

Without a converter the keys are stringified, as the sort proc already does, so on_duplicate_key now does the same.

Added a test for String/Integer and Symbol/Integer collisions and for a JSON::Coder without a block; it fails without the change. Ran the full rake test (703 tests, 0 failures) on Ruby 3.4 only; JRuby and other Ruby versions were not run.

In rfc8785 mode the duplicate key check re-coerced every key through as_json, which is nil for a non-strict generator and false for a strict Coder without a block, so a hash such as { "1" => 1, 1 => 2 } raised NoMethodError. Without an as_json converter the keys are stringified, as the sort proc already does, so do the same when looking for the duplicate. Fixes ruby#1152.
@byroot

byroot commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks for the patch, however I decided to go with a different fix: #1156

@byroot byroot closed this Nov 29, 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.

RFC 8785 canonicalization raises NoMethodError (not JSON::GeneratorError) for colliding mixed-type object keys

2 participants