-
Notifications
You must be signed in to change notification settings - Fork 354
Deprecate duplicate keys in object #818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
6e6bd2a to
5ad5d37
Compare
There are few legitimate use cases for duplicate keys, and can in some case be exploited. Rather to always silently accept them, we should emit a warning, and in the future require to explictly allow them.
|
Is there a way to improve the debug message somewhat? It's impossible to find what's causing these in my test runs 😅 |
|
But also you can turn the warning into an error, that should give you a backtrace. |
I assume this is coming from one of my dependencies so not that easy to do that without knowing which |
|
Yeah, I'm currently working on getting the warning emitted at the caller. But you can do something like: JSON.singleton_class.prepend(Module.new {
def parse(source, opts = nil)
opts ||= {}
opts[:allow_duplicate_key] = false
super
end
})NB: Not saying this is fine, just giving you a quick solution until this is improved. |
Followup: ruby#818 Now the warning should point at the `JSON.parse` caller, and not inside the json gem itself.
|
With #832, the warning should point at the caller. |
Followup: ruby#818 Now the warning should point at the `JSON.parse` caller, and not inside the json gem itself.
|
Another trick to handle/track warnings is to turn them into errors, e.g. https://github.com/rails/rails/blob/087e27d78395e630eb8fdd34a5203dc33fcde099/tools/strict_warnings.rb |
|
Awesome, #832 did the trick! Thanks so much for this and all that you do! 🙏 |
Followup: ruby/json#818 Now the warning should point at the `JSON.parse` caller, and not inside the json gem itself. ruby/json@cd51557387
|
If this can be useful to someone, I noticed that most of the time this happened in our app was caused by 1 hash with symbol keys being merged with one hash with string keys, then serialised to JSON, then parsed. |
There are few legitimate use cases for duplicate keys, and can in some case be exploited.
Rather to always silently accept them, we should emit a warning, and in the future require to explictly allow them.
TODO: jruby version