[GH-ISSUE #392] Can't turn a Token to a serde_json::value::Value and back #126

Closed
opened 2026-02-27 20:23:15 +03:00 by kerem · 2 comments
Owner

Originally created by @PaulOlteanu on GitHub (Mar 8, 2023).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/392

Describe the bug
Trying to create a Token from a serde_json::value::Value results in an error invalid type string expected a borrowed string.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Token/get a token from a client
  2. Create a Value from this token (let value = json!(token))
  3. Turn the value back to a token (let token = serde_json::from_value(value))

Expected behavior
A Token should be able to be created from a Value

Additional context
The issue seems to come from the deserialization portion of space_separated_scopes. Changing github.com/ramsayleung/rspotify@6d304de632/rspotify-model/src/custom_serde.rs (L225)

to be

let scopes: String = Deserialize::deserialize(d)?;

fixes the issue, but I'm not familiar enough with how serde deserialization works to judge if this is a good solution

Originally created by @PaulOlteanu on GitHub (Mar 8, 2023). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/392 **Describe the bug** Trying to create a `Token` from a `serde_json::value::Value` results in an error `invalid type string expected a borrowed string`. **To Reproduce** Steps to reproduce the behavior: 1. Create a `Token`/get a token from a client 2. Create a `Value` from this token (`let value = json!(token)`) 3. Turn the value back to a token (`let token = serde_json::from_value(value)`) **Expected behavior** A `Token` should be able to be created from a `Value` **Additional context** The issue seems to come from the deserialization portion of `space_separated_scopes`. Changing https://github.com/ramsayleung/rspotify/blob/6d304de632a84f79c35cb9ff4a0e66be238cf205/rspotify-model/src/custom_serde.rs#L225 to be ```let scopes: String = Deserialize::deserialize(d)?;``` fixes the issue, but I'm not familiar enough with how serde deserialization works to judge if this is a good solution
kerem 2026-02-27 20:23:15 +03:00
Author
Owner

@ramsayleung commented on GitHub (Apr 10, 2023):

I can reproduce this bug with this following test case:

    #[test]
    fn test_token_deserialize() {
        let mut scopes = HashSet::<String>::new();
        scopes.insert("user-read-email".to_owned());
        let tok = Token {
            access_token: "access_token".to_string(),
            scopes: scopes,
            ..Default::default()
        };
        let value = json!(tok);
        let token = serde_json::from_value::<Token>(value);
        assert!(token.is_ok());
        assert_eq!(token.unwrap().scopes, tok.scopes);
    }

I have created a PR to fix this problem.

<!-- gh-comment-id:1501279394 --> @ramsayleung commented on GitHub (Apr 10, 2023): I can reproduce this bug with this following test case: ```rust #[test] fn test_token_deserialize() { let mut scopes = HashSet::<String>::new(); scopes.insert("user-read-email".to_owned()); let tok = Token { access_token: "access_token".to_string(), scopes: scopes, ..Default::default() }; let value = json!(tok); let token = serde_json::from_value::<Token>(value); assert!(token.is_ok()); assert_eq!(token.unwrap().scopes, tok.scopes); } ``` I have created a PR to fix this problem.
Author
Owner

@ramsayleung commented on GitHub (May 22, 2023):

This problem has been fixed.

<!-- gh-comment-id:1557839472 --> @ramsayleung commented on GitHub (May 22, 2023): This problem has been fixed.
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#126
No description provided.