Skip to content

Commit

Permalink
Implemented workaround for Spotify API bug
Browse files Browse the repository at this point in the history
Due to Spotify API returning floats instead of uints for some fields of
the Artist it cannot be parsed. This commit adds deserializing float
values into u32 as a workaround.

Signed-off-by: Maciej Torhan <[email protected]>
  • Loading branch information
m-torhan committed Feb 1, 2024
1 parent 6b23c52 commit 432348f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
6 changes: 6 additions & 0 deletions rspotify-model/src/artist.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
//! All objects related to artist defined by Spotify API
mod data_type_patcher {
include!("data_type_patcher.rs");
}

pub use data_type_patcher::as_u32;

use serde::{Deserialize, Serialize};

Expand All @@ -25,6 +30,7 @@ pub struct FullArtist {
pub id: ArtistId<'static>,
pub images: Vec<Image>,
pub name: String,
#[serde(deserialize_with = "as_u32")]
pub popularity: u32,
}

Expand Down
26 changes: 26 additions & 0 deletions rspotify-model/src/data_type_patcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Workaround for Spotify API bug which causes dome uint fields
// being return as floats

use serde::{Deserialize, Deserializer};

pub fn as_u32<'de, D>(deserializer: D) -> Result<u32, D::Error>

Check warning on line 6 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Building

function `as_u32` is never used

Check warning on line 6 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Building

function `as_u32` is never used

Check failure on line 6 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Test and Lint for each Client (rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-...

function `as_u32` is never used

Check failure on line 6 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Test and Lint for each Client (rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/re...

function `as_u32` is never used
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>

Check warning on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Building

function `as_some_u32` is never used

Check warning on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Building

function `as_some_u32` is never used

Check warning on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Building

function `as_some_u32` is never used

Check warning on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Building

function `as_some_u32` is never used

Check failure on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Test and Lint for each Client (rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-...

function `as_some_u32` is never used

Check failure on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Test and Lint for each Client (rspotify/cli,rspotify/env-file,rspotify/client-ureq,rspotify/ureq-...

function `as_some_u32` is never used

Check failure on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Test and Lint for each Client (rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/re...

function `as_some_u32` is never used

Check failure on line 17 in rspotify-model/src/data_type_patcher.rs

View workflow job for this annotation

GitHub Actions / Test and Lint for each Client (rspotify/cli,rspotify/env-file,rspotify/client-reqwest,rspotify/re...

function `as_some_u32` is never used
where
D: Deserializer<'de>,
{
let float_data: f64 = Deserialize::deserialize(deserializer)?;

let u32_data = float_data as u32;

Ok(Some(u32_data))
}
7 changes: 7 additions & 0 deletions rspotify-model/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
//! Image object
mod data_type_patcher {
include!("data_type_patcher.rs");
}

pub use data_type_patcher::as_some_u32;

use serde::{Deserialize, Serialize};

/// Image object
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Image {
#[serde(deserialize_with = "as_some_u32")]
pub height: Option<u32>,
pub url: String,
#[serde(deserialize_with = "as_some_u32")]
pub width: Option<u32>,
}
10 changes: 7 additions & 3 deletions rspotify-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! All Spotify API endpoint response objects. Please refer to the endpoints
//! where they are used for a link to their reference in the Spotify API
//! documentation.
mod data_type_patcher {
include!("data_type_patcher.rs");
}
pub mod album;
pub mod artist;
pub mod audio;
Expand All @@ -24,9 +27,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 +39,7 @@ 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>,
#[serde(deserialize_with = "as_u32")]
pub total: u32,
}

Expand Down

0 comments on commit 432348f

Please sign in to comment.