Skip to content

Commit 7ee49cb

Browse files
authored
Merge pull request #5 from tom-burzynski/fix/album-contributors-empty-array
Fix album unmarshal error when ALB_CONTRIBUTORS is an empty array
2 parents 9672d83 + 7fe4208 commit 7ee49cb

2 files changed

Lines changed: 98 additions & 6 deletions

File tree

‎types/album.go‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package types
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
38
// AlbumTypeMinimal represents minimal information about an album, including artist and explicit content details.
49
type AlbumTypeMinimal struct {
510
ALB_ID string `json:"ALB_ID"` // Album ID, e.g., '9188269'
@@ -21,14 +26,38 @@ type AlbumTypeMinimal struct {
2126
TYPE_INTERNAL string `json:"__TYPE__"` // Internal type, e.g., 'album'
2227
}
2328

29+
// AlbumContributors represents contributors to the album.
30+
type AlbumContributors struct {
31+
MainArtist []string `json:"main_artist"` // Main artist, e.g., ['Avicii']
32+
}
33+
34+
// UnmarshalJSON for AlbumContributors allows dynamic handling of multiple structures.
35+
// Albums without contributor metadata (e.g., various-artists soundtracks) are returned
36+
// by the API as an empty array instead of an object.
37+
func (ac *AlbumContributors) UnmarshalJSON(data []byte) error {
38+
// Attempt to unmarshal directly into the struct
39+
type Alias AlbumContributors
40+
var tmp Alias
41+
if err := json.Unmarshal(data, &tmp); err == nil {
42+
*ac = AlbumContributors(tmp)
43+
return nil
44+
}
45+
46+
// If the above fails, try parsing as an empty array
47+
if string(data) == "[]" || string(data) == "{}" {
48+
*ac = AlbumContributors{}
49+
return nil
50+
}
51+
52+
return fmt.Errorf("failed to unmarshal AlbumContributors: %s", string(data))
53+
}
54+
2455
// AlbumType represents detailed information about an album including contributors and release dates.
2556
type AlbumType struct {
26-
ALB_CONTRIBUTORS struct { // Contributors to the album
27-
MainArtist []string `json:"main_artist"` // Main artist, e.g., ['Avicii']
28-
} `json:"ALB_CONTRIBUTORS"`
29-
ALB_ID string `json:"ALB_ID"` // Album ID, e.g., '9188269'
30-
ALB_PICTURE string `json:"ALB_PICTURE"` // Album picture hash, e.g., '6e58a99f59a150e9b4aefbeb2d6fc856'
31-
EXPLICIT_ALBUM_CONTENT struct { // Explicit content details
57+
ALB_CONTRIBUTORS AlbumContributors `json:"ALB_CONTRIBUTORS"` // Contributors to the album
58+
ALB_ID string `json:"ALB_ID"` // Album ID, e.g., '9188269'
59+
ALB_PICTURE string `json:"ALB_PICTURE"` // Album picture hash, e.g., '6e58a99f59a150e9b4aefbeb2d6fc856'
60+
EXPLICIT_ALBUM_CONTENT struct { // Explicit content details
3261
EXPLICIT_LYRICS_STATUS int `json:"EXPLICIT_LYRICS_STATUS"` // Explicit lyrics status, e.g., 0
3362
EXPLICIT_COVER_STATUS int `json:"EXPLICIT_COVER_STATUS"` // Explicit cover status, e.g., 0
3463
} `json:"EXPLICIT_ALBUM_CONTENT"`

‎types/album_test.go‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAlbumContributorsUnmarshalJSON(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
input string
14+
expected AlbumContributors
15+
wantErr bool
16+
}{
17+
{
18+
name: "Object with main artist",
19+
input: `{"main_artist": ["Avicii"]}`,
20+
expected: AlbumContributors{MainArtist: []string{"Avicii"}},
21+
},
22+
{
23+
name: "Empty array (album without contributor metadata)",
24+
input: `[]`,
25+
expected: AlbumContributors{},
26+
},
27+
{
28+
name: "Empty object",
29+
input: `{}`,
30+
expected: AlbumContributors{},
31+
},
32+
{
33+
name: "Invalid structure",
34+
input: `["unexpected"]`,
35+
wantErr: true,
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
var ac AlbumContributors
42+
err := json.Unmarshal([]byte(tt.input), &ac)
43+
if tt.wantErr {
44+
assert.Error(t, err)
45+
return
46+
}
47+
assert.NoError(t, err)
48+
assert.Equal(t, tt.expected, ac)
49+
})
50+
}
51+
}
52+
53+
func TestAlbumTypeUnmarshalWithArrayContributors(t *testing.T) {
54+
// Deezer's album.getData returns ALB_CONTRIBUTORS as an empty array
55+
// instead of an object for albums without contributor metadata.
56+
data := `{"ALB_ID": "6090427", "ALB_TITLE": "Kids Original Motion Picture Soundtrack", "ALB_CONTRIBUTORS": []}`
57+
58+
var album AlbumType
59+
err := json.Unmarshal([]byte(data), &album)
60+
assert.NoError(t, err)
61+
assert.Equal(t, "6090427", album.ALB_ID)
62+
assert.Empty(t, album.ALB_CONTRIBUTORS.MainArtist)
63+
}

0 commit comments

Comments
 (0)