diff --git a/src/opds_server/db/access.py b/src/opds_server/db/access.py index 643b664..bc822ef 100644 --- a/src/opds_server/db/access.py +++ b/src/opds_server/db/access.py @@ -199,7 +199,8 @@ async def get_author_name(author_id: int, config: Config) -> str: row = await cursor.fetchone() if not row: raise HTTPException(status_code=404, detail="Author not found") - return row[0] + # Calibre escapes commas within author names as pipes in storage. + return row[0].replace("|", ",") async def add_authors(books: list, config: Config) -> dict[int, dict]: @@ -222,7 +223,9 @@ async def add_authors(books: list, config: Config) -> dict[int, dict]: book_ids, ) as cursor: async for book_id, author_id, name in cursor: - authors_by_book[book_id].append({"id": author_id, "name": name}) + authors_by_book[book_id].append( + {"id": author_id, "name": name.replace("|", ",")} + ) result = {} for book_id, title, last_modified in books: @@ -332,7 +335,11 @@ async def get_authors(page: int, config: Config) -> tuple[list, bool, bool]: has_next = len(authors) > limit has_previous = offset > 0 and bool(authors) - return authors[:limit], has_previous, has_next + return ( + [(author_id, name.replace("|", ",")) for author_id, name in authors[:limit]], + has_previous, + has_next, + ) async def get_author_books( diff --git a/tests/test_catalog_endpoints.py b/tests/test_catalog_endpoints.py index 9dcaac9..dafce7a 100644 --- a/tests/test_catalog_endpoints.py +++ b/tests/test_catalog_endpoints.py @@ -231,6 +231,44 @@ def test_author_feed_omits_navigation_beyond_last_page(catalog_client): assert not links(beyond, "previous") and not links(beyond, "next") +@pytest.mark.parametrize( + "stored_name", + ["Asimov| Isaac", "Анисимов| В. И.| & <Соавтор>", "Ada & Sons"], +) +def test_calibre_author_names_decode_commas_without_changing_storage( + client_factory, stored_name +): + """Decode every escaped comma across feeds without splitting authors.""" + library, client = client_factory(page_size=100) + with sqlite3.connect(library / "metadata.db") as connection: + connection.execute("UPDATE authors SET name = ? WHERE id = 1", (stored_name,)) + + expected_name = stored_name.replace("|", ",") + listing = parse_atom(client.get("/opds/by-author")) + assert entries(listing)[0].findtext("atom:title", namespaces=NS) == expected_name + detail = parse_atom(client.get("/opds/author/1")) + assert detail.findtext("atom:title", namespaces=NS) == f"Books by {expected_name}" + for endpoint in ( + "/opds/author/1", + "/opds/by-title", + "/opds/by-newest", + "/opds/search?q=Practical", + ): + feed = parse_atom(client.get(endpoint)) + book = next( + entry + for entry in entries(feed) + if entry.findtext("atom:title", namespaces=NS) == "A Book" + ) + assert book.findtext("atom:author/atom:name", namespaces=NS) == expected_name + assert book.findtext("atom:author/atom:uri", namespaces=NS) == "/opds/author/1" + + with sqlite3.connect(library / "metadata.db") as connection: + assert connection.execute( + "SELECT name FROM authors WHERE id = 1" + ).fetchone() == (stored_name,) + + def test_author_navigation_detail_and_authorless_books(catalog_client): """Follow author links and keep authorless or multiply-authored books readable."""