fix: enable multi-valued releasetype in smart playlists (#4621)

* fix: prevent infinite loop in Type filter autocomplete

Fixed an infinite loop issue in the album Type filter caused by an inline
arrow function in the optionText prop. The inline function created a new
reference on every render, causing React-Admin's AutocompleteInput to
continuously re-fetch data from the /api/tag endpoint.

The solution extracts the formatting function outside the component scope
as formatReleaseType, ensuring a stable function reference across renders.
This prevents unnecessary re-renders and API calls while maintaining the
humanized display format for release type values.

* fix: enable multi-valued releasetype in smart playlists

Smart playlists can now match all values in multi-valued releasetype tags.
Previously, the albumtype field was mapped to the single-valued mbz_album_type
database field, which only stored the first value from tags like album; soundtrack.
This prevented smart playlists from matching albums with secondary release types
like soundtrack, live, or compilation when tagged by MusicBrainz Picard.

The fix removes the direct database field mapping and allows both albumtype and
releasetype to use the multi-valued tag system. The albumtype field is now an
alias that points to the releasetype tag field, ensuring both query the same
JSON path in the tags column. This maintains backward compatibility with the
documented albumtype field while enabling proper multi-value tag matching.

Added tests to verify both releasetype and albumtype correctly generate
multi-valued tag queries.

Fixes #4616

* fix: resolve albumtype alias for all operators and sorting

Codex correctly identified that the initial fix only worked for Contains/StartsWith/EndsWith operators. The alias resolution was happening too late in the code path.

Fixed by resolving the alias in two places:
1. tagCond.ToSql() - now uses the actual field name (releasetype) in the JSON path
2. Criteria.OrderBy() - now uses the actual field name when building sort expressions

Added tests for Is/IsNot operators and sorting to ensure complete coverage.
This commit is contained in:
Deluan Quintão
2025-10-26 19:36:44 -04:00
committed by GitHub
parent aa7f55646d
commit d021289279
5 changed files with 67 additions and 9 deletions
+6 -1
View File
@@ -61,7 +61,12 @@ func (c Criteria) OrderBy() string {
if f.order != "" { if f.order != "" {
mapped = f.order mapped = f.order
} else if f.isTag { } else if f.isTag {
mapped = "COALESCE(json_extract(media_file.tags, '$." + sortField + "[0].value'), '')" // Use the actual field name (handles aliases like albumtype -> releasetype)
tagName := sortField
if f.field != "" {
tagName = f.field
}
mapped = "COALESCE(json_extract(media_file.tags, '$." + tagName + "[0].value'), '')"
} else if f.isRole { } else if f.isRole {
mapped = "COALESCE(json_extract(media_file.participants, '$." + sortField + "[0].name'), '')" mapped = "COALESCE(json_extract(media_file.participants, '$." + sortField + "[0].name'), '')"
} else { } else {
+10
View File
@@ -118,6 +118,16 @@ var _ = Describe("Criteria", func() {
) )
}) })
It("sorts by albumtype alias (resolves to releasetype)", func() {
AddTagNames([]string{"releasetype"})
goObj.Sort = "albumtype"
gomega.Expect(goObj.OrderBy()).To(
gomega.Equal(
"COALESCE(json_extract(media_file.tags, '$.releasetype[0].value'), '') asc",
),
)
})
It("sorts by random", func() { It("sorts by random", func() {
newObj := goObj newObj := goObj
newObj.Sort = "random" newObj.Sort = "random"
+13 -5
View File
@@ -32,7 +32,6 @@ var fieldMap = map[string]*mappedField{
"sortalbum": {field: "media_file.sort_album_name"}, "sortalbum": {field: "media_file.sort_album_name"},
"sortartist": {field: "media_file.sort_artist_name"}, "sortartist": {field: "media_file.sort_artist_name"},
"sortalbumartist": {field: "media_file.sort_album_artist_name"}, "sortalbumartist": {field: "media_file.sort_album_artist_name"},
"albumtype": {field: "media_file.mbz_album_type", alias: "releasetype"},
"albumcomment": {field: "media_file.mbz_album_comment"}, "albumcomment": {field: "media_file.mbz_album_comment"},
"catalognumber": {field: "media_file.catalog_num"}, "catalognumber": {field: "media_file.catalog_num"},
"filepath": {field: "media_file.path"}, "filepath": {field: "media_file.path"},
@@ -55,6 +54,9 @@ var fieldMap = map[string]*mappedField{
"mbz_release_group_id": {field: "media_file.mbz_release_group_id"}, "mbz_release_group_id": {field: "media_file.mbz_release_group_id"},
"library_id": {field: "media_file.library_id", numeric: true}, "library_id": {field: "media_file.library_id", numeric: true},
// Backward compatibility: albumtype is an alias for releasetype tag
"albumtype": {field: "releasetype", isTag: true},
// special fields // special fields
"random": {field: "", order: "random()"}, // pseudo-field for random sorting "random": {field: "", order: "random()"}, // pseudo-field for random sorting
"value": {field: "value"}, // pseudo-field for tag and roles values "value": {field: "value"}, // pseudo-field for tag and roles values
@@ -154,13 +156,19 @@ type tagCond struct {
func (e tagCond) ToSql() (string, []any, error) { func (e tagCond) ToSql() (string, []any, error) {
cond, args, err := e.cond.ToSql() cond, args, err := e.cond.ToSql()
// Check if this tag is marked as numeric in the fieldMap // Resolve the actual tag name (handles aliases like albumtype -> releasetype)
if fm, ok := fieldMap[e.tag]; ok && fm.numeric { tagName := e.tag
cond = strings.ReplaceAll(cond, "value", "CAST(value AS REAL)") if fm, ok := fieldMap[e.tag]; ok {
if fm.field != "" {
tagName = fm.field
}
if fm.numeric {
cond = strings.ReplaceAll(cond, "value", "CAST(value AS REAL)")
}
} }
cond = fmt.Sprintf("exists (select 1 from json_tree(tags, '$.%s') where key='value' and %s)", cond = fmt.Sprintf("exists (select 1 from json_tree(tags, '$.%s') where key='value' and %s)",
e.tag, cond) tagName, cond)
if e.not { if e.not {
cond = "not " + cond cond = "not " + cond
} }
+34
View File
@@ -105,6 +105,40 @@ var _ = Describe("Operators", func() {
gomega.Expect(sql).To(gomega.BeEmpty()) gomega.Expect(sql).To(gomega.BeEmpty())
gomega.Expect(args).To(gomega.BeEmpty()) gomega.Expect(args).To(gomega.BeEmpty())
}) })
It("supports releasetype as multi-valued tag", func() {
AddTagNames([]string{"releasetype"})
op := Contains{"releasetype": "soundtrack"}
sql, args, err := op.ToSql()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(sql).To(gomega.Equal("exists (select 1 from json_tree(tags, '$.releasetype') where key='value' and value LIKE ?)"))
gomega.Expect(args).To(gomega.HaveExactElements("%soundtrack%"))
})
It("supports albumtype as alias for releasetype", func() {
AddTagNames([]string{"releasetype"})
op := Contains{"albumtype": "live"}
sql, args, err := op.ToSql()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(sql).To(gomega.Equal("exists (select 1 from json_tree(tags, '$.releasetype') where key='value' and value LIKE ?)"))
gomega.Expect(args).To(gomega.HaveExactElements("%live%"))
})
It("supports albumtype alias with Is operator", func() {
AddTagNames([]string{"releasetype"})
op := Is{"albumtype": "album"}
sql, args, err := op.ToSql()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
// Should query $.releasetype, not $.albumtype
gomega.Expect(sql).To(gomega.Equal("exists (select 1 from json_tree(tags, '$.releasetype') where key='value' and value = ?)"))
gomega.Expect(args).To(gomega.HaveExactElements("album"))
})
It("supports albumtype alias with IsNot operator", func() {
AddTagNames([]string{"releasetype"})
op := IsNot{"albumtype": "compilation"}
sql, args, err := op.ToSql()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
// Should query $.releasetype, not $.albumtype
gomega.Expect(sql).To(gomega.Equal("not exists (select 1 from json_tree(tags, '$.releasetype') where key='value' and value = ?)"))
gomega.Expect(args).To(gomega.HaveExactElements("compilation"))
})
}) })
Describe("Custom Roles", func() { Describe("Custom Roles", func() {
+4 -3
View File
@@ -42,6 +42,9 @@ const useStyles = makeStyles({
}, },
}) })
const formatReleaseType = (record) =>
record?.tagValue ? humanize(record?.tagValue) : '-- None --'
const AlbumFilter = (props) => { const AlbumFilter = (props) => {
const classes = useStyles() const classes = useStyles()
const translate = useTranslate() const translate = useTranslate()
@@ -142,9 +145,7 @@ const AlbumFilter = (props) => {
> >
<AutocompleteInput <AutocompleteInput
emptyText="-- None --" emptyText="-- None --"
optionText={(record) => optionText={formatReleaseType}
record?.tagValue ? humanize(record?.tagValue) : '-- None --'
}
/> />
</ReferenceInput> </ReferenceInput>
<NullableBooleanInput source="compilation" /> <NullableBooleanInput source="compilation" />