Skip to content
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

Workaround for Spotify API bug #457

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rspotify-model/src/artist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use std::collections::HashMap;

use crate::{ArtistId, CursorBasedPage, Followers, Image};
use crate::{data_type_patcher::as_u32, ArtistId, CursorBasedPage, Followers, Image};

/// Simplified Artist Object
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
Expand All @@ -25,6 +25,8 @@ pub struct FullArtist {
pub id: ArtistId<'static>,
pub images: Vec<Image>,
pub name: String,
// TODO: remove this statement after Spotify fix the [issue](https://github.com/ramsayleung/rspotify/issues/452)
#[serde(deserialize_with = "as_u32")]
pub popularity: u32,
}

Expand Down
28 changes: 28 additions & 0 deletions rspotify-model/src/data_type_patcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Workaround for Spotify API bug which causes dome uint fields
// being return as floats
// TODO: remove this workaround after Spotify fix the [issue](https://github.com/ramsayleung/rspotify/issues/452)

use serde::{Deserialize, Deserializer};

pub fn as_u32<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
D: Deserializer<'de>,
{
let float_data: f64 = Deserialize::deserialize(deserializer)?;

let u32_data = float_data as u32;

Ok(u32_data)
}

pub fn as_some_u32<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
where
D: Deserializer<'de>,
{
let float_data: Option<f64> = Deserialize::deserialize(deserializer)?;

match float_data {
Some(f) => Ok(Some(f as u32)),
None => Ok(None),
}
}
6 changes: 6 additions & 0 deletions rspotify-model/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
//! Image object

pub use crate::data_type_patcher::as_some_u32;

use serde::{Deserialize, Serialize};

/// Image object
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Image {
// TODO: remove this statement after Spotify fix the [issue](https://github.com/ramsayleung/rspotify/issues/452)
#[serde(deserialize_with = "as_some_u32")]
pub height: Option<u32>,
pub url: String,
// TODO: remove this statement after Spotify fix the [issue](https://github.com/ramsayleung/rspotify/issues/452)
#[serde(deserialize_with = "as_some_u32")]
pub width: Option<u32>,
}
9 changes: 6 additions & 3 deletions rspotify-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod auth;
pub mod category;
pub mod context;
pub(crate) mod custom_serde;
pub mod data_type_patcher;
pub mod device;
pub mod enums;
pub mod error;
Expand All @@ -24,9 +25,9 @@ pub mod track;
pub mod user;

pub use {
album::*, artist::*, audio::*, auth::*, category::*, context::*, device::*, enums::*, error::*,
idtypes::*, image::*, offset::*, page::*, playing::*, playlist::*, recommend::*, search::*,
show::*, track::*, user::*,
album::*, artist::*, audio::*, auth::*, category::*, context::*, data_type_patcher::as_u32,
device::*, enums::*, error::*, idtypes::*, image::*, offset::*, page::*, playing::*,
playlist::*, recommend::*, search::*, show::*, track::*, user::*,
};

use serde::{Deserialize, Serialize};
Expand All @@ -36,6 +37,8 @@ use serde::{Deserialize, Serialize};
pub struct Followers {
// This field will always set to null, as the Web API does not support it at the moment.
// pub href: Option<String>,
// TODO: remove this statement after Spotify fix the [issue](https://github.com/ramsayleung/rspotify/issues/452)
#[serde(deserialize_with = "as_u32")]
pub total: u32,
}

Expand Down
Loading