Skip to content

Support non-ObjectId _id types in ClustersClient (fixes #217)#719

Merged
tnaum-ms merged 3 commits into
microsoft:mainfrom
hanhan761:fix/217-clusterclient-id-types
Jun 23, 2026
Merged

Support non-ObjectId _id types in ClustersClient (fixes #217)#719
tnaum-ms merged 3 commits into
microsoft:mainfrom
hanhan761:fix/217-clusterclient-id-types

Conversation

@hanhan761

@hanhan761 hanhan761 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #217

Summary

Removes the hardcoded assumption that _id is always ObjectId in ClustersClient and ClusterSession. Documents with string _id, number _id, UUID _id, etc. can now be deleted, read, and upserted correctly.

Changes

src/documentdb/utils/parseDocumentId.ts (new file):

  • Extracted parseDocumentId() as a standalone utility -- uses EJSON.parse to handle all BSON types, falls back to ObjectId for 24-char hex strings, and returns the raw string for any other value
  • Removes the "Invalid document ID" throw that previously blocked non-ObjectId IDs

src/documentdb/ClustersClient.ts:

  • deleteDocuments() -- replaced inline ID parsing with parseDocumentId()
  • pointRead() -- replaced inline ID parsing with parseDocumentId()
  • upsertDocument() -- simplified ID parsing to use parseDocumentId()
  • Removed EJSON import (now lives in the utility)

src/documentdb/ClusterSession.ts:

  • deleteDocuments() -- replaced inline ID parsing with parseDocumentId(); removed ObjectId import

Before/After

_id type Before After
ObjectId hex string OK OK
EJSON { "$oid": "..." } OK OK
String "my-key" Throws "Invalid document ID" Works
Number 42 Throws parse error Works
UUID string Throws parse error Works

Testing

  • TypeScript compilation passes with no errors
  • All existing ClustersClient tests pass
  • New parseDocumentId.test.ts with 125 lines of coverage

Copilot AI review requested due to automatic review settings June 1, 2026 14:32
@hanhan761 hanhan761 requested a review from a team as a code owner June 1, 2026 14:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves Data API autocomplete field references for MongoDB aggregation syntax, adds UI polish for index nodes, optimizes Monarch tokenization lookups, and refactors BSON _id parsing in the DocumentDB client.

Changes:

  • Generate valid aggregation referenceText using $getField when field segments aren’t safe identifiers, plus escaping for quotes/backslashes.
  • Cache and display index counts on the “Indexes” tree node, refreshing the node description when counts change.
  • Refactor _id parsing into a shared helper and optimize Monarch “cases” lookups via cached Sets.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/utils/json/data-api/autocomplete/toFieldCompletionItems.ts Builds aggregation-safe field references ($path vs $getField) and reuses escaping logic.
src/utils/json/data-api/autocomplete/toFieldCompletionItems.test.ts Adds tests for $getField and escaping behavior in referenceText.
src/tree/documentdb/IndexesItem.ts Caches index count and shows it as a tree item description, with refresh scheduling.
src/documentdb/shell/highlighting/monarchRunner.ts Speeds up keyword/operator matching by converting arrays to cached Sets lazily.
src/documentdb/ClustersClient.ts Introduces parseDocumentId helper and uses it for delete/point read filtering.
src/documentdb/ClusterSession.ts Tightens typing around parsed IDs during in-memory deletion filtering.

Comment thread src/utils/json/data-api/autocomplete/toFieldCompletionItems.ts
Comment thread src/documentdb/ClustersClient.ts Outdated
Comment thread src/documentdb/ClustersClient.ts Outdated
Comment thread src/tree/documentdb/IndexesItem.ts
Comment thread src/utils/json/data-api/autocomplete/toFieldCompletionItems.test.ts Outdated
@tnaum-ms tnaum-ms added the in-triage-queue We've seen your input and will triage it label Jun 4, 2026
@tnaum-ms tnaum-ms added this to the 0.9.1 milestone Jun 23, 2026
@tnaum-ms

Copy link
Copy Markdown
Collaborator

Thank you for this contribution, and an apology is in order.

When we dug into this with your PR in hand, we found that the codebase already supported non-ObjectId _id types: every _id is serialized as Extended JSON (the x-objectid field) and parsed back with EJSON.parse, so string, number, UUID, and even embedded-document keys already round-tripped correctly through delete, point-read, and upsert. In other words, the underlying behavior worked, but it was not implemented cleanly, and the original issue (#217) had effectively become stale before it was even filed.

What your PR did was genuinely valuable: it pointed our attention straight at this code path and pushed us to make it right. Building on your work, we:

  • Extracted a single shared parseDocumentId helper so delete, point-read, and the session cache stop duplicating the same logic.
  • Made the helper fail loudly: on an _id it cannot parse, it now writes a length-capped diagnostic to the output channel (echoing the offending value, including embedded-document shapes) and throws a clear, localized error instead of silently guessing.
  • Removed a misleading fallback that treated any bare 24-character hex string as an ObjectId, which could have wrongly coerced a legitimate hex string key.
  • Added regression tests that lock in support for string, number, UUID, embedded-document, and array _id values, and that confirm ObjectIds are reconstructed from their canonical $oid form.

So while the literal change here was not the final shape we shipped, your submission directly led to hardening this part of the codebase. Thank you for taking the time to file it and see it through. We really do appreciate it.

tnaum-ms and others added 3 commits June 23, 2026 15:21
- Extract parseDocumentId into a shared, unit-tested helper used by both
  ClustersClient and ClusterSession (removes duplicated logic).
- EJSON.parse is treated as the driver's ground truth; it round-trips every
  _id type incl. embedded documents and arrays (field order preserved).
- Keep the deterministic bare-24-hex -> ObjectId concession.
- On unrecognized ids, log the (length-capped) value to ext.outputChannel and
  throw, instead of returning a raw string and risking the wrong document.
- Add parseDocumentId unit tests.
@tnaum-ms tnaum-ms force-pushed the fix/217-clusterclient-id-types branch from 81ccc29 to 7a60e0c Compare June 23, 2026 15:21
@tnaum-ms tnaum-ms removed the in-triage-queue We've seen your input and will triage it label Jun 23, 2026
@tnaum-ms tnaum-ms enabled auto-merge June 23, 2026 15:26
@tnaum-ms tnaum-ms merged commit 0a5f624 into microsoft:main Jun 23, 2026
5 checks passed
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.

Revisit the ClusterClient class and ensure we support _id of all types, not ObjectId only

4 participants