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

Bug Fix: Handle null images for playlist #716

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Changes from 2 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
9 changes: 7 additions & 2 deletions src/api/api_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ trait WithImages {
pub struct Playlist {
pub id: String,
pub name: String,
pub images: Vec<Image>,
pub images: Option<Vec<Image>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does #[serde(default)] fix the issue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprisingly, no. I haven't investigated why but adding that to the images will still throw a null-errror.

pub tracks: Page<PlaylistTrack>,
pub owner: PlaylistOwner,
}
Expand All @@ -197,9 +197,14 @@ pub struct PlaylistOwner {
pub display_name: String,
}

const def_image: &'static [Image] = &[Image {url: String::new(), height: Some(640), width: Some(640)}];

impl WithImages for Playlist {
fn images(&self) -> &[Image] {
&self.images
match &self.images {
Some(x) => &x[..],
None => &def_image[..],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we return an empty array here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Rust returns returns a value referencing data owned by the current function. I believe it has to do with garbage collection, but I do not know much rust so I just got around the issue by making a static constant outside the implementation.

}
}
}

Expand Down
Loading