[GH-ISSUE #316] The Deserialize impl for the id types doesn't work correctly with serde_json::from_reader #97

Closed
opened 2026-02-27 20:23:06 +03:00 by kerem · 1 comment
Owner

Originally created by @semicoleon on GitHub (May 21, 2022).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/316

Describe the bug
When deserializing from an std::io::Reader instead of using serde_json::from_slice PlaylistIds (and the other id types, I assume) fail with invalid type: string "...", expected a borrowed string. See a minimal reproduction here. The README on that repo also has a brief explanation of what I think is causing the problem based on a quick look at the Deserialize impl.

To Reproduce
Steps to reproduce the behavior:

  1. Attempt to deserialize any type which contains a PlaylistId with serde_json::from_reader(). It will fail.

Expected behavior
serde_json::from_slice(data) == serde_json::from_reader(data) when the derserialized type impls DeserializeOwned

Originally created by @semicoleon on GitHub (May 21, 2022). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/316 **Describe the bug** When deserializing from an std::io::Reader instead of using serde_json::from_slice PlaylistIds (and the other id types, I assume) fail with `invalid type: string "...", expected a borrowed string`. See a [minimal reproduction here]( https://github.com/semicoleon/rspotify-serde-json-bug). The README on that repo also has a brief explanation of what I think is causing the problem based on a quick look at the Deserialize impl. **To Reproduce** Steps to reproduce the behavior: 1. Attempt to deserialize any type which contains a PlaylistId with serde_json::from_reader(). It will fail. **Expected behavior** serde_json::from_slice(data) == serde_json::from_reader(data) when the derserialized type impls DeserializeOwned
kerem 2026-02-27 20:23:06 +03:00
Author
Owner

@ramsayleung commented on GitHub (May 21, 2022):

Oooh, thanks for your report, I can reproduce this problem. The compiler complains :

> RUST_BACKTRACE=1 cargo test
    Finished test [unoptimized + debuginfo] target(s) in 13.40s
     Running unittests src/main.rs (target/debug/deps/rspotify_serde_json_bug-d4a8eb306bbbda43)

running 2 tests
test test::slice ... ok
test test::reader ... FAILED

failures:

---- test::reader stdout ----
thread 'test::reader' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"averyrealplaylistid\", expected a borrowed string", line: 1, column: 28)', src/main.rs:17:60

I agree with your point that the offending line is let field = <&str>::deserialize(deserializer)?; at idtypes.rs:352, <&str> expects a borrowed string, but a from_reader function may only deserialize owned data.

If you have time, feel free to create a PR to fix this issue :)

<!-- gh-comment-id:1133586602 --> @ramsayleung commented on GitHub (May 21, 2022): Oooh, thanks for your report, I can reproduce this problem. The compiler complains : ```sh > RUST_BACKTRACE=1 cargo test Finished test [unoptimized + debuginfo] target(s) in 13.40s Running unittests src/main.rs (target/debug/deps/rspotify_serde_json_bug-d4a8eb306bbbda43) running 2 tests test test::slice ... ok test test::reader ... FAILED failures: ---- test::reader stdout ---- thread 'test::reader' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"averyrealplaylistid\", expected a borrowed string", line: 1, column: 28)', src/main.rs:17:60 ``` I agree with your point that the offending line is `let field = <&str>::deserialize(deserializer)?;` at `idtypes.rs:352`, `<&str>` expects a borrowed string, but a` from_reader` function may only deserialize owned data. If you have time, feel free to create a PR to fix this issue :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/rspotify#97
No description provided.