plugins/web: fix endpoints /…/values/…

Following #4709 and #5447, the web plugin used single-quotes (ie. string
litteral) in the SQL query for table columns. Thus, for instance, the query
`GET /item/values/albumartist` would return the litteral "albumartist"
instead of a list of unique album artists.
This commit is contained in:
Théophile Bastian 2025-11-11 21:04:30 +01:00
parent 07445fdd07
commit 666c412b0e
3 changed files with 12 additions and 1 deletions

View file

@ -232,7 +232,7 @@ def _get_unique_table_field_values(model, field, sort_field):
raise KeyError
with g.lib.transaction() as tx:
rows = tx.query(
f"SELECT DISTINCT '{field}' FROM '{model._table}' ORDER BY '{sort_field}'"
f"SELECT DISTINCT {field} FROM {model._table} ORDER BY {sort_field}"
)
return [row[0] for row in rows]

View file

@ -43,6 +43,10 @@ Bug fixes:
accepted a list of strings). :bug:`5962`
- Fix a bug introduced in release 2.4.0 where import from any valid
import-log-file always threw a "none of the paths are importable" error.
- :doc:`/plugins/web`: repair broken `/item/values/…` and `/albums/values/…`
endpoints. Previously, due to single-quotes (ie. string literal) in the SQL
query, the query eg. `GET /item/values/albumartist` would return the literal
"albumartist" instead of a list of unique album artists.
For plugin developers:

View file

@ -118,6 +118,13 @@ class WebPluginTest(ItemInDBTestCase):
assert response.status_code == 200
assert len(res_json["items"]) == 3
def test_get_unique_item_artist(self):
response = self.client.get("/item/values/artist")
res_json = json.loads(response.data.decode("utf-8"))
assert response.status_code == 200
assert res_json["values"] == ["", "AAA Singers"]
def test_get_single_item_by_id(self):
response = self.client.get("/item/1")
res_json = json.loads(response.data.decode("utf-8"))