Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 12, 2025

Closes #10972

Merging works with missing or malformed author entries threw TypeError: Cannot read properties of undefined when attempting to access author.key on undefined objects, preventing the merge UI from rendering.

Technical

Added defensive checks at four access points where author data is read without validation:

  • get_author_names(): Filter malformed entries before mapping to keys

    .filter(authorEntry => authorEntry.author && authorEntry.author.key)
    .map(authorEntry => authorEntry.author.key)
  • enhancedRecords: Check existence before accessing properties and verify author exists in search results

    if (entry.author && entry.author.key) {
        const authorId = entry.author.key.slice('/authors/'.length);
        if (authorId in author_names) {
            entry.name = author_names[authorId];
        }
    }
  • hash_subel(): Fallback to JSON serialization for malformed author entries

  • AuthorRoleTable: Conditional render displays "(malformed author)" instead of crashing

Testing

Visit merge URLs with works containing malformed authors (e.g., https://openlibrary.org/works/merge?records=OL16305918W,OL20650159W,OL19635988W) - UI should render successfully with graceful handling of missing author data.

Stakeholders

@cdrini

Original prompt

This section details on the original issue you should resolve

<issue_title>Cannot merge works if a work has no author</issue_title>
<issue_description>### Problem

Reproducing the bug

  1. Go to https://openlibrary.org/works/merge?records=OL16305918W,OL20650159W,OL19635988W
  • Expected behavior: The works are listed
  • Actual behavior: There is an error and nothing is rendered

Context

  • Browser (Chrome, Safari, Firefox, etc):
  • OS (Windows, Mac, etc):
  • Logged in (Y/N):
  • Environment (prod, dev, local): prod

Breakdown

Requirements Checklist

  • [ ]

Related files

Stakeholders


Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.
    </issue_description>

<agent_instructions>Do you see the problem?

The error in the MergeUI (ol-MergeUI.js) vue code is happening if the author entry is malformatted and missing a key. There's a draft PR exploring this:
https://github.com/internetarchive/openlibrary/pull/10973/changes

https://openlibrary.org/works/merge?records=OL16305918W,OL20650159W,OL19635988W

js console shows e.g. TypeError: Cannot read properties of undefined (reading 'OL2702410A'). recordMap['OL2702410A'] is undefined.

Can you review the code and see why this may be happening, propose a fix, which is either a console popup explaining exactly what is going wrong (e.g. this specific author malformed, this work has no editions, that kind of thing) and then perform some sort of remediation (e.g. exclude the problematic work).

First, start by analyzing ol-MergeUI.js and the cases without reasonable doubt where this failure would occur. We're not looking for a huge PR which protects every branch, we want you to surgically zero in on the specific relevant pieces that are causing this failure, recognize the problem, and put a minimal intervention in place.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@cdrini Actually it seems more subtle, the error is happening if the author entry is malformatted and missing a key.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 12, 2025 18:09
Add defensive checks in MergeTable.vue and utils.js to handle cases where:
1. Author entry is missing the 'author' property
2. Author entry is missing the 'author.key' property
3. Author is not found in search results

This prevents TypeError when trying to merge works with missing/malformed authors.

Co-authored-by: mekarpeles <[email protected]>
Additional defensive checks to prevent TypeError in:
1. hash_subel function when hashing author entries
2. AuthorRoleTable when displaying author links

These handle the same malformed author scenarios consistently.

Co-authored-by: mekarpeles <[email protected]>
Copilot AI changed the title [WIP] Fix issue with merging works that lack authors Fix: Handle malformed author entries in work merge UI Dec 12, 2025
Copilot AI requested a review from mekarpeles December 12, 2025 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot merge works if a work has no author

2 participants