Files
romm/backend/utils/facets.py
Jeremy aae48728e5 fix(metadata): read names from {id, name} facet entries in recommendations and ROM responses (#5249)
* fix(recommendations): handle named metadata objects in facet tokens

* fix(recommendations): pr-ready step 2 (code-review): skip non-list columns, base role fallback on usable names

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(recommendations): pr-ready step 3 (simplify): type facet params as object, trim redundant test cases

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(recommendations): pr-ready step 4 (review-polish): keep the extract_tokens test prefix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(roms): read names from metadata objects in filter values and ROM responses

Share the facet name cleaner as utils.facets.facet_names, and run it in the
filter-values reader, RomMetadataSchema, and over every list[str] key of the
provider metadata blobs, so an {id, name} entry no longer fails /api/roms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(roms): clean name lists in raw metadata edits, reject non-object JSON

parse_raw_metadata now runs the provider blob's list-of-names keys through
facet_names before storing it, so {id, name} objects stop landing in the
database, and answers 422 when the JSON is not an object. Manual metadata
keeps its strict adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 12:03:36 -04:00

18 lines
669 B
Python

from collections.abc import Mapping
def facet_names(values: object) -> list[str]:
"""Clean names from a facet column, skipping entries that carry none."""
# The raw-metadata form stores JSON unvalidated, so a column can hold
# {id, name} objects, bare ids, or a scalar where a list belongs.
if isinstance(values, str):
values = [values]
elif not isinstance(values, (list, tuple)):
return []
names: list[str] = []
for value in values:
name = value.get("name") if isinstance(value, Mapping) else value
if isinstance(name, str) and (cleaned := name.strip()):
names.append(cleaned)
return names