[PR #7329] fix(mongodb-adapter): use duck-typing for ObjectId validation #24134

Open
opened 2026-04-15 22:11:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7329
Author: @krismach
Created: 1/13/2026
Status: 🔄 Open

Base: mainHead: fix/mongodb-adapter-objectid-instanceof


📝 Commits (2)

  • a699e48 fix(mongodb-adapter): use duck-typing for ObjectId validation
  • b07db1c test(mongodb-adapter): add unit tests for isObjectIdLike duck-typing

📊 Changes

2 files changed (+104 additions, -5 deletions)

View changed files

📝 e2e/adapter/test/mongo-adapter/adapter.mongo-db.test.ts (+83 -1)
📝 packages/mongo-adapter/src/mongodb-adapter.ts (+21 -4)

📄 Description

The MongoDB adapter uses instanceof ObjectId to validate ObjectId values. This fails when the ObjectId originates from a different instance of the mongodb package (e.g., when mongoose bundles its own mongodb).

This PR replaces instanceof ObjectId checks with duck-typing by checking for the presence of the toHexString method.


Summary by cubic

Switch ObjectId validation in the MongoDB adapter to duck-typing to avoid instanceof failures across different mongodb package instances. This improves compatibility with Mongoose and prevents invalid ID errors.

  • Bug Fixes
    • Added isObjectIdLike helper that checks for toHexString.
    • Replaced instanceof ObjectId checks in input/output transforms and array handling.
    • Accepts ObjectId-like values directly to avoid "INVALID_ID" errors.

Written for commit b07db1cb2f. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7329 **Author:** [@krismach](https://github.com/krismach) **Created:** 1/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/mongodb-adapter-objectid-instanceof` --- ### 📝 Commits (2) - [`a699e48`](https://github.com/better-auth/better-auth/commit/a699e48ab2bdff2e38554df03698855f6567cd8c) fix(mongodb-adapter): use duck-typing for ObjectId validation - [`b07db1c`](https://github.com/better-auth/better-auth/commit/b07db1cb2f468dd17b86479711eec51c931d6acb) test(mongodb-adapter): add unit tests for isObjectIdLike duck-typing ### 📊 Changes **2 files changed** (+104 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `e2e/adapter/test/mongo-adapter/adapter.mongo-db.test.ts` (+83 -1) 📝 `packages/mongo-adapter/src/mongodb-adapter.ts` (+21 -4) </details> ### 📄 Description The MongoDB adapter uses instanceof ObjectId to validate ObjectId values. This fails when the ObjectId originates from a different instance of the mongodb package (e.g., when mongoose bundles its own mongodb). This PR replaces instanceof ObjectId checks with duck-typing by checking for the presence of the toHexString method. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Switch ObjectId validation in the MongoDB adapter to duck-typing to avoid instanceof failures across different mongodb package instances. This improves compatibility with Mongoose and prevents invalid ID errors. - **Bug Fixes** - Added isObjectIdLike helper that checks for toHexString. - Replaced instanceof ObjectId checks in input/output transforms and array handling. - Accepts ObjectId-like values directly to avoid "INVALID_ID" errors. <sup>Written for commit b07db1cb2f468dd17b86479711eec51c931d6acb. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:11:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#24134