Skip to content

Fix Union deserialization for RecommendationFolder.items#239

Merged
marcelveldt merged 2 commits into
mainfrom
fix/recommendation-folder-union-deserialization
May 29, 2026
Merged

Fix Union deserialization for RecommendationFolder.items#239
marcelveldt merged 2 commits into
mainfrom
fix/recommendation-folder-union-deserialization

Conversation

@jdaberkow
Copy link
Copy Markdown
Member

mashumaro deserializes Union types by trying each variant in declaration order and picking the first one that doesn't raise. Since Artist has the fewest required fields, every item in RecommendationFolder.items gets deserialized as Artist regardless of its actual media_type.

This adds a custom deserializer via field_options(deserialize=...) that dispatches to the correct class based on the media_type field value and the presence of provider_mappings (to distinguish full MediaItem types from ItemMapping/BrowseFolder).

Includes a roundtrip test covering Artist, Album, Track, Playlist, and ItemMapping.

mashumaro's default Union deserialization tries types in declaration order
and picks the first match (Artist), regardless of the actual media_type.

Add field_options(deserialize=...) on RecommendationFolder.items that
dispatches to the correct class based on the media_type field value.
Copy link
Copy Markdown
Member

@marcelveldt marcelveldt left a comment

Choose a reason for hiding this comment

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

Nice fix @jdaberkow !

@marcelveldt marcelveldt merged commit 3961e97 into main May 29, 2026
3 checks passed
@marcelveldt marcelveldt deleted the fix/recommendation-folder-union-deserialization branch May 29, 2026 14:33
github-actions Bot pushed a commit to music-assistant/server that referenced this pull request May 29, 2026
Update music-assistant-models to version
[1.1.127](https://github.com/music-assistant/models/releases/tag/1.1.127)


- Add decoded_audio_format field to StreamDetails (by @marcelveldt in
[#240](music-assistant/models#240))

## 🚀 Features and enhancements

- Add artist_str property to Audiobook model (by @jdaberkow in
[#234](music-assistant/models#234))

## 🐛 Bugfixes

- Fix Union deserialization for RecommendationFolder.items (by
@jdaberkow in
[#239](music-assistant/models#239))


## 🙇 Contributors

@jdaberkow and @marcelveldt

Co-authored-by: marcelveldt <6389780+marcelveldt@users.noreply.github.com>
jdaberkow added a commit to music-assistant/server that referenced this pull request May 30, 2026
…n deserialization

The RecommendationFolder.items field now has a custom deserializer
(music-assistant/models#239) that discriminates Union types by media_type,
so full Playlist/Artist/Album objects survive the to_dict()/from_dict()
roundtrip correctly. The ItemMapping conversion workaround is no longer needed.

Resolves review issue #18.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes a bug in the code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants