Skip to content

Conversation

jcobis
Copy link
Collaborator

@jcobis jcobis commented Oct 14, 2025

Description

Followups addressing non-blocking comments from #7439

  • Throw error instead of use fallback value for binary sample value edge case (that we do not expect to encounter)
  • Cast instead of use instanceof

Checklist

  • New tests and/or benchmarks are included
  • Documentation is changed or added
  • If this change updates the UI, screenshots/videos are added and a design review is requested
  • I have signed the MongoDB Contributor License Agreement (https://www.mongodb.com/legal/contributor-agreement)

Types of changes

  • Backport Needed
  • Patch (non-breaking change which fixes an issue)
  • Minor (non-breaking change which adds functionality)
  • Major (fix or feature that would cause existing functionality to change)

@github-actions github-actions bot added the feat label Oct 14, 2025
@jcobis jcobis added the no release notes Fix or feature not for release notes label Oct 14, 2025
@jcobis jcobis marked this pull request as ready for review October 14, 2025 21:14
@jcobis jcobis requested a review from a team as a code owner October 14, 2025 21:14
@jcobis jcobis changed the title feat(compass-collection): Follow-ups tweaks to convertBSONToPrimitive feat(compass-collection): Follow-ups tweaks to convertBSONToPrimitive - CLOUDP-351455 Oct 14, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements follow-up improvements to the convertBSONToPrimitive function based on code review feedback. The changes enhance error handling and improve type checking by using BSON type properties instead of instanceof checks.

  • Replace instanceof checks with _bsontype property-based type detection
  • Throw error for Binary data edge case instead of returning fallback value
  • Add comprehensive test coverage for the Binary data error scenario

Reviewed Changes

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

File Description
packages/compass-collection/src/transform-schema-to-field-info.ts Refactored BSON type detection using _bsontype property and added error handling for Binary data
packages/compass-collection/src/transform-schema-to-field-info.spec.ts Added test case to verify Binary data error throwing behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines 121 to 126
/**
* Helper to cast BSON objects to their specific types
*/
function castBSONValue<T>(value: unknown): T {
return value as T;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a bit backward to me. If you're moving away from instanceof checks, I'd prefer leaning into type guards based on a union of expected BSON types. Actually surprised "bson" doesn't already have this union defined 🤔

I put together a TS playground to exemplify what I'm talking about.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, they do expose the BSONValue class, so you could make this

Suggested change
/**
* Helper to cast BSON objects to their specific types
*/
function castBSONValue<T>(value: unknown): T {
return value as T;
}
/**
* Helper to cast BSON objects to their specific types
*/
function castBSONValue<T extends BSONValue>(value: BSONValue): T {
return value as T;
}

Copy link
Contributor

@kraenhansen kraenhansen Oct 15, 2025

Choose a reason for hiding this comment

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

The BSONValue from typescript is the abstract super-class? It adds no constrains to the _bsontype field beyond string ... seems much less usable than a union over the concrete implementations of that super-class.

The original and suggested code effectively use none of the type guarantees provided by "bson": There's nothing linking the switch-case statements to the particular BSONValue - might as well implement it in JavaScript at that point 🤷

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's correct, and I agree that it would be useful to have a union of all possible BSON values available somewhere 🙂

I think we effectively have that elsewhere in Compass, though

Copy link
Contributor

@kraenhansen kraenhansen Oct 15, 2025

Choose a reason for hiding this comment

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

we effectively have that elsewhere in Compass

That would be a good candidate for reuse, I'm not aware of such utility?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Mentioned it below:

with TypeCastMap from the hadron-type-checker package.

It's not perfect but it should work here

Copy link
Collaborator Author

@jcobis jcobis Oct 15, 2025

Choose a reason for hiding this comment

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

Went with the suggestion to use BSONValue for now, which allowed me to remove that cast. Let me know what you think

(Edit: just refreshed this page and saw the new comments since)

Comment on lines 121 to 126
/**
* Helper to cast BSON objects to their specific types
*/
function castBSONValue<T>(value: unknown): T {
return value as T;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, they do expose the BSONValue class, so you could make this

Suggested change
/**
* Helper to cast BSON objects to their specific types
*/
function castBSONValue<T>(value: unknown): T {
return value as T;
}
/**
* Helper to cast BSON objects to their specific types
*/
function castBSONValue<T extends BSONValue>(value: BSONValue): T {
return value as T;
}

Copy link
Contributor

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

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

My specific concern is this type assertion and I've suggested better and more type-safe ways to express this.

@addaleax
Copy link
Collaborator

and I've suggested better

Gotta disagree with this one. It's not a better suggestion.

I think we're in alignment that it would be good to have the bson library expose a type that fully describes possible BSON types. That's a fair request, but it's not something we can handle in this PR on its own, it requires a change to the BSON library itself.

If you currently want something that describes all possible BSON types, I'd go with TypeCastMap from the hadron-type-checker package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat no release notes Fix or feature not for release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants