Lix JSON parser silently accepts duplicate object keys #1162
Labels
No labels
Affects/CppNix
Affects/Nightly
Affects/Only nightly
Affects/Stable
Area/build-packaging
Area/cli
Area/evaluator
Area/fetching
Area/flakes
Area/language
Area/lix ci
Area/nix-eval-jobs
Area/profiles
Area/protocol
Area/releng
Area/remote-builds
Area/repl
Area/repl/debugger
Area/store
awaiting
author
awaiting
contributors
bug
Context
contributors
Context
drive-by
Context
maintainers
Context
RFD
crash 💥
Cross Compilation
devx
diagnostics
docs
Downstream Dependents
E/easy
E/hard
E/help wanted
E/reproducible
E/requires rearchitecture
Feature/S3
Importance
High
Importance
Low
imported
Language/Bash
Language/C++
Language/NixLang
Language/Python
Language/Rust
Needs Langver
OS/Linux
OS/macOS
performance
regression
Release Blocking
Non-urgent
Release Blocking
Urgent
stability
Status
blocked
Status
invalid
Status
postponed
Status
wontfix
testing
testing/flakey
Topic/Large Scale Installations
Urgency
High
Urgency
Low
ux
No milestone
No project
No assignees
6 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
lix-project/lix#1162
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Describe the bug
Lix's JSON parser used by
builtins.fromJSONsilently accepts duplicate object keys, discarding all values but the last.Steps To Reproduce
Expected behavior
Such unusual JSON input should be rejected, or if not, the current behavior should be at least tested for.
nix --versionoutputAdditional context
The existing behavior is defined by
JSONObjectStateinlix/libexpr/json-to-value.ccclass JSONObjectState : public JSONState {using JSONState::JSONState;GcMap<Symbol, Value> attrs;Symbol _key;std::unique_ptr<JSONState> resolve(EvalState & state) override{auto attrs2 = state.ctx.buildBindings(attrs.size());for (auto & i : attrs)attrs2.insert(i.first, i.second);parent->value() = {NewValueAs::attrs, attrs2.alreadySorted()};return std::move(parent);}void add() override{attrs.insert_or_assign(_key, value());v = nullptr;}public:void key(string_t & name, EvalState & state){_key = state.ctx.symbols.create(name);}};Since I'm already in the weeds in the JSON parser I'm happy to drive a fix, but that first requires determining how this will be fixed. This would be a breaking change. Would we want to warn first before making it an error? Would we need to add a flag to re-enable old behavior?
Maybe it would be best to add it to the deprecated features, adding a warning for now that can be turned to a hard error in a future release
People argue on Matrix that the current behavior is desired, on the basis of the JSON spec being fucked and other JSON parsers behaving the same. In that case, the current behavior should be documented and tested.
We strongly believe Lix should not ever hard error on this. Duplicate keys in JSON are a known failure of the spec (among plenty others), and, for better or for worse, many other popular JSON parser implementations also accept duplicate keys: Python's standard
jsonmodule, jq, andserde_jsonall accept duplicate keys (and discard all but the last value).No, to us, the question is not whether or not Lix should accept duplicate keys, but what Lix should do with them.
agreed, lix should not emit any errors here. opt-in warnings (once we have an eval warnings system) sound like a good idea though, but even then we can't emit them everywhere without either support from nlohmann or using our own object reconstruction for all json parsing. given that our behavior is consistent with the rest of the ecosystem we could also do nothing at all and still be largely fine.
This issue was mentioned on Gerrit on the following CLs: