Skip to content

Conversation

@papafe
Copy link
Contributor

@papafe papafe commented Nov 13, 2025

This PR allows types that use a scalar discriminator convention to have a null discriminator on some types (for example abstract ones), while disallows it on hierarchical discriminator convention.

@papafe papafe requested a review from rstam November 13, 2025 18:27
@papafe papafe self-assigned this Nov 13, 2025
@papafe papafe requested a review from a team as a code owner November 13, 2025 18:27
Copilot AI review requested due to automatic review settings November 13, 2025 18:27
Copy link
Contributor

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 addresses CSHARP-5757 by modifying the discriminator handling logic to allow null discriminators for abstract types when using scalar discriminator conventions, while maintaining the restriction for hierarchical discriminator conventions. The change moves the null discriminator validation from the generic filter builder to the specific hierarchical discriminator implementation.

  • Moved null discriminator validation from FilterDefinitionBuilder to DiscriminatorAstFilter.TypeIs method
  • Added validation that throws NotSupportedException when hierarchical discriminators are null
  • Added comprehensive test coverage for both hierarchical and scalar discriminator scenarios

Reviewed Changes

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

File Description
tests/MongoDB.Driver.Tests/Jira/CSharp5757Tests.cs Adds test cases validating that hierarchical discriminators throw for abstract types while scalar discriminators work correctly with abstract types
src/MongoDB.Driver/Linq/Linq3Implementation/Translators/DiscriminatorAstFilter.cs Adds null discriminator validation specifically for hierarchical discriminator convention
src/MongoDB.Driver/FilterDefinitionBuilder.cs Removes generic null discriminator check to allow scalar conventions to have null discriminators

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@papafe papafe added the bug Fixes issues or unintended behavior. label Nov 13, 2025
var discriminator = discriminatorConvention.GetDiscriminator(nominalType, actualType);
if (discriminator == null)
{
throw new NotSupportedException($"Hierarchical discriminator convention requires that documents of type {BsonUtils.GetFriendlyTypeName(actualType)} have a discriminator value.");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can improve this error message.

A shorter version might be:

throw new NotSupportedException("Discriminator value is null which is not allowed with hierarchical discriminator conventions.");

and a longer version might be:

throw new NotSupportedException($"Discriminator value for type {{BsonUtils.GetFriendlyTypeName(typeof(TDerived))}} is null which is not allowed with hierarchical discriminator conventions.");

var exception = Record.Exception(() => filter.Render(renderedArgs));
exception.Should().BeOfType<NotSupportedException>();
exception.Message.Should().Be("Hierarchical discriminator convention requires that documents of type HealthCareWorker have a discriminator value.");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

These new tests should probably be integration tests like the tests for scalar discriminator conventions.

See CSharp5231Tests.cs for an example of a file that uses two collections for integration tests.

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

Labels

bug Fixes issues or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants