Skip to content

Normalize OCLC numbers in import records to prevent duplicate editions - #14500

Merged
cdrini merged 2 commits into
internetarchive:masterfrom
pranshup04:fix/14496-normalize-oclc-imports
Nov 17, 2026
Merged

cdrini merged 2 commits into
internetarchive:masterfrom
pranshup04:fix/14496-normalize-oclc-imports

Conversation

@pranshup04

Copy link
Copy Markdown
Contributor

Closes #14496

What does this PR achieve?

Normalizes incoming OCLC numbers during book import so prefixed or zero-padded values can match existing editions stored with bare control numbers. This prevents the import preview from proposing a duplicate edition when the OCLC number is the only matching identifier.

Technical

  • Reuses normalize_oclc() in normalize_record_bibids() before edition matching.
  • Removes supported prefixes, whitespace, hyphens, and leading zeros. Filters invalid values and deduplicates the normalized list while preserving order.
  • Follows cdrini’s guidance to assume normalized OCLC numbers in the catalog. The patch uses the existing lookup and requires no new index field or backfill.

Testing

  • Added regression tests for ocm, ocn, on, and (OCoLC) prefixes, leading zeros, and bare values. The tests verify that the import preview resolves to the existing edition.
  • Added normalization tests for duplicate and invalid values and for a single string input.
  • Ran pytest openlibrary/catalog/add_book/tests openlibrary/tests/contribute/test_identifiers.py in Docker. All 229 tests passed.

Stakeholders

@cdrini

@openlibrary-bot
openlibrary-bot requested a review from a team November 16, 2026 20:36
@openlibrary-bot

Copy link
Copy Markdown
Collaborator

Thanks for the PR, @pranshup04!

@cdrini is assigned to this PR and currently has:

  • 23 open PR(s) of equal or higher priority to review first.
PR triage checklist (maintainers / Pierre)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by Pierre, Open Library's PR first-touch assistant, on behalf of @mekarpeles. This is NOT a code review or sign-off.

@openlibrary-bot openlibrary-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR extends normalize_record_bibids to also clean oclc_numbers on import records, using the existing normalize_oclc helper (openlibrary/utils/oclc.py) to strip ocm/ocn/on/(OCoLC) prefixes, leading zeros, and dedupe, so that an incoming record's OCLC number matches an existing edition's OCLC number regardless of which prefix style the source used. It also handles the case where oclc_numbers arrives as a bare string instead of a list. New unit tests cover the normalization logic directly and an integration test confirms a prefixed incoming OCLC number matches a catalog edition recorded with a different (or no) prefix during import preview.

Dimensions

  • Correctness — 2 findings
  • Security — no findings in this range. This is not a security audit.
  • Accessibility — no findings. No template, JS, component, or style changes in this range — only openlibrary/catalog/add_book/init.py and its test file.
  • Test coverage — no findings. test_normalize_record_bibids_oclc and test_normalize_record_bibids_string_oclc exercise the new normalization branch directly (prefixes, leading zeros, invalid/empty/None entries, string-vs-list input), and test_import_preview_matches_equivalent_oclc_numbers (parametrized over ocm/ocn/on/(OCoLC)/bare forms) exercises the full import-preview matching path end to end.
  • Styleguide — no findings. No material violations — follows the existing isbn/lccn cleaning pattern in the same function, uses walrus assignment consistent with repo convention, line stays within the 162-char limit.

Findings without an in-diff anchor

  • LOW · Correctness — Matched edition can accumulate duplicate OCLC numbers in different formats
    Once find_match locates an existing edition via the newly-normalized oclc_numbers, update_edition_with_rec_data (openlibrary/catalog/add_book/init.py, the edition_list_fields loop around lines 958-976) merges rec's oclc_numbers into the edition's existing oclc_numbers using case-fold string comparison only (v.casefold() not in case_folded_values), not normalize_oclc equivalence. If the matched edition's stored value is e.g. "ocm30877728" and rec's normalized value is "30877728", casefold comparison treats them as different strings, so "30877728" gets appended as a second, functionally-duplicate OCLC number on the edition. A test exercising update_edition_with_rec_data (or _load with save=True) against a pre-existing edition whose oclc_numbers use a prefix would catch this; the current tests only check the preview/match outcome, not the resulting edition's oclc_numbers after an actual save.

Tip

When you've addressed the feedback and pushed your changes, comment @openlibrary-bot review on this PR to run a fresh review.

Note

Automated review by @openlibrary-bot on behalf of @mekarpeles. This is not an approval or a sign-off, and it does not satisfy a required review.

@hornc

hornc commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

I disagree with the bot's comments. OCNs should be stored normalized. If we have existing un-normalised OCNs in the data, they should be corrected, and with this PR, correct OCNs will accumulate.

@openlibrary-bot openlibrary-bot added the Needs: Rebase Has merge conflicts with the base branch — rebase/update to resolve (auto-managed by olaf) label Nov 17, 2026
@pranshup04
pranshup04 force-pushed the fix/14496-normalize-oclc-imports branch from 69eaef3 to f5ff019 Compare November 17, 2026 08:10
@github-actions github-actions Bot added the Needs: Response Issues which require feedback from lead label Nov 17, 2026
@openlibrary-bot openlibrary-bot removed the Needs: Rebase Has merge conflicts with the base branch — rebase/update to resolve (auto-managed by olaf) label Nov 17, 2026

@cdrini cdrini left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @hornc ; there are only 150 cases with the prefix: https://openlibrary.org/query.json?type=/type/edition&oclc~=o*&oclc&limit=500

Otherwise lgtm, thank you @pranshup04 !

@cdrini cdrini removed the Needs: Response Issues which require feedback from lead label Nov 17, 2026
@cdrini

cdrini commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

@openlibrary-bot review

@openlibrary-bot openlibrary-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalizes OCLC numbers (ocm/ocn/on/(OCoLC) prefixes, leading zeros) on incoming import records so they match editions already stored with bare OCLC numbers, fixing the duplicate-edition preview bug from #14496.

🎫 Ticket compliance analysis 🔶

#14496 - Partially compliant

Compliant requirements:

  • Incoming OCLC numbers (ocm/ocn/on/(OCoLC) prefixes, leading zeros, duplicates) are normalized to bare numbers before matching
  • Preview matches an existing edition whose stored OCLC number is bare against an incoming prefixed equivalent

Non-compliant requirements:

  • Preview still fails to match when the existing edition's own stored OCLC number is itself prefixed, since the candidate lookup is an exact string match against the stored value
Dimensions
  • Correctness — 1 finding
  • Security — no findings in this range. This is not a security audit.
  • Accessibility — no findings. No template, JS, component or style changes in this range.
  • Test coverage — no findings. New tests cover normalize_record_bibids directly and an end-to-end preview match; they fail without the fix.
  • Styleguide — no findings. New code follows the existing normalize_record_bibids pattern for isbn/lccn fields.

Tip

A new review starts automatically about 10 minutes after your last push. To start it sooner, a maintainer can re-request a review, or you can comment @openlibrary-bot review on this PR.

Note

Automated review by @openlibrary-bot. This is not an approval or a sign-off, and it does not satisfy a required review.

@cdrini

cdrini commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

@openlibrary-bot We've decided that is not an issue ; can you please approve the review?

@cdrini

cdrini commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Apologies for the stall here folks, we're testing a new automated code review flow which is currently preventing me from merging! Working on resolving.

@cdrini
cdrini merged commit 2810b0f into internetarchive:master Nov 17, 2026
8 of 9 checks passed
@cdrini

cdrini commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

We got there :D Thank you @RayBB for helping us find the new location of the "Dismiss review" button 😁

@pranshup04
pranshup04 deleted the fix/14496-normalize-oclc-imports branch November 17, 2026 21:04
marley pushed a commit to marley/openlibrary that referenced this pull request Nov 19, 2026
…rmalize-oclc-imports

Normalize OCLC numbers in import records to prevent duplicate editions
marley pushed a commit to marley/openlibrary that referenced this pull request Nov 19, 2026
…rmalize-oclc-imports

Normalize OCLC numbers in import records to prevent duplicate editions
marley pushed a commit to marley/openlibrary that referenced this pull request Nov 19, 2026
…rmalize-oclc-imports

Normalize OCLC numbers in import records to prevent duplicate editions
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.

Import preview proposes a duplicate edition when an OCLC number has an ocm prefix

4 participants