Skip to content

Conversation

marcj
Copy link
Owner

@marcj marcj commented Oct 13, 2025

This adds support for simple scored unions. Previously the serializer picked the first matching union member. Now it takes a score per member and uses the one with highest score.

type T = {a: string} | {a: string, b?: number}

// Previously this was always picking first, leading to drop of b. Now it work.
const data = serialize<T>({a: 'foo', b: 123}); 

marcj added 2 commits October 14, 2025 01:24
```
database.query(Entity).withOptions({estimateCount: true}).count();
```
This adds a simple shallow algorithm to detect a union member smarter by scoring its matched properties before taking the serializer. this adds also support for the same in BSON.
@marcj marcj requested a review from Copilot October 14, 2025 00:22
Copy link

@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 adds support for scored unions to improve serialization behavior when union members have overlapping properties. Previously, the serializer always picked the first matching union member, which could lead to data loss. The new implementation assigns scores to union members and selects the one with the highest score, ensuring more accurate serialization.

  • Implements scored union selection for better union member matching
  • Updates type guard functions to return scores instead of simple boolean values
  • Adds comprehensive test coverage for various union scenarios with optional and additional properties

Reviewed Changes

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

Show a summary per file
File Description
packages/type/src/serializer.ts Core implementation of scored union logic with type guard scoring system
packages/type/src/typeguard.ts Updates type guard to handle score-based returns for root validation
packages/bson/src/bson-deserializer-templates.ts BSON-specific implementation of scored union deserialization
packages/bson/src/bson-serializer.ts Updates BSON serializer to use new union handling
packages/type/tests/serializer.spec.ts Test cases for union serialization with overlapping members
packages/type/tests/type-spec.spec.ts Test cases for union JSON serialization scenarios
packages/bson/tests/bson-parser.spec.ts BSON-specific union serialization test cases
packages/bson/tests/type-spec.spec.ts Reordered test assertions for null/undefined handling
packages/mongo/src/query.resolver.ts Added empty query handling for deterministic count results
packages/mongo/src/client/options.ts Added estimateCount option to CommandOptions interface

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

@marcj marcj merged commit 4bea9fb into master Oct 14, 2025
15 of 16 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.

1 participant