cloudwego / cloudwego/sonic-rs

Question: should `get_many` define behavior for duplicate object member names?

Open
#234 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
920
Forks
68
Avg merge
2h 5m
Merged PRs (30d)
1

Description

Hi! I’d like to clarify whether `get_many` and `get_many_unchecked` are expected to have defined behavior when a JSON object contains duplicate member names.

[RFC 8259 §4](https://www.rfc-editor.org/rfc/rfc8259.html#section-4) says that object member names **SHOULD** be unique and notes that receiver behavior is unpredictable when they are not. Duplicate names are therefore discouraged for interoperable JSON, but they are not rejected by the JSON grammar. [ECMA-404 §6](https://www.ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf) likewise leaves their semantics to the JSON processor.

sonic-rs also currently documents [`Object` as allowing duplicate keys](https://github.com/cloudwego/sonic-rs/blob/03545a9530346fe279b674dd496e037d94204bc5/src/value/object.rs#L27-L42).

## Reproduction

A runnable reproduction is available here:

https://github.com/starcatmeow/sonic-rs/blob/rayli/repro-get-many-duplicate-keys/examples/get_many_duplicate_keys.rs

```bash
cargo run --example get_many_duplicate_keys
```

Using this input:

```json
{"a": 1, "a": 2, "b": 3}
```

The current output is:

```text
get([a]): 1
get_many([a], [b]): [Some("2"), None]
```

Thus, `get` returns the first occurrence of `a`, while `get_many` returns the second occurrence and does not return the requested `b` that follows it.

Is duplicate-member handling intentionally outside the contract of `get_many`, or would the maintainers be interested in defining behavior for this case?

If defined behavior is desirable, would first-occurrence-wins be reasonable? That would match the current behavior of `get` and `Object::get`, producing:

```text
[Some("1"), Some("3")]
```

If this behavior is preferred, I’d be happy to send a PR with focused tests and documentation for both `get_many` and `get_many_unchecked`.

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction at examples/get_many_duplicate_keys.rs and run `cargo run --example get_many_duplicate_keys` to confirm the reported difference between `get` and `get_many`. Read the linked Object documentation in src/value/object.rs and inspect the implementations of `get_many` and `get_many_unchecked`; the issue asks whether duplicate-name behavior should be defined and suggests first-occurrence-wins, with focused tests and documentation as possible follow-up. Done requires a maintainer decision on the contract.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.