Skip to content

Conversation

@papafe
Copy link
Contributor

@papafe papafe commented May 6, 2025

No description provided.

@papafe papafe requested a review from rstam May 6, 2025 09:59
@papafe papafe requested a review from a team as a code owner May 6, 2025 09:59

private class DerivedDocument : BaseDocument {}

[Fact]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These tests can eventually be moved to somewhere more appropriate.

/// </summary>
/// <returns>The discriminator convention for the class.</returns>
internal IDiscriminatorConvention GetDiscriminatorConvention()
internal IDiscriminatorConvention GetDiscriminatorConvention(bool checkConflicts = false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to find a place where to check that there are no conflicts with the other fields element names. I decided to do it here so it's done once and not continuously when serializing objects.
For now checkConflicts is true only when GetDiscriminatorConvention is called in BsonClassMapSerializer.SerializeDiscriminator (that is used only when serializing classes).
I'm not sure this is the optimal place for this, and if it would be worth throwing also when deserializing, for instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would we ever NOT check for conflicts? Why do we even need the checkConflicts parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We discussed on slack, but I've removed the parameter and moved the conflict checking inside so that it is run once and not all the time the method is called.

@papafe papafe added the bug Fixes issues or unintended behavior. label May 6, 2025
/// </summary>
/// <returns>The discriminator convention for the class.</returns>
internal IDiscriminatorConvention GetDiscriminatorConvention()
internal IDiscriminatorConvention GetDiscriminatorConvention(bool checkConflicts = false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would we ever NOT check for conflicts? Why do we even need the checkConflicts parameter?

if (elementName.IndexOf('\0') != -1)
{
throw new ArgumentException("Element names cannot contain nulls.", "elementName");
throw new ArgumentException("Element names cannot be null or empty.", nameof(elementName));
Copy link
Contributor

Choose a reason for hiding this comment

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

throw new ArgumentException("Element names cannot contain nulls.", "elementName");

Should not have been changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this should have gone for the previous condition.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would you consider slightly more precise wording?

throw new ArgumentException("Discriminator element name cannot be null or empty.", nameof(elementName));

I think element name (singular) is better than plural.

Also, I prefer Discriminator element name to Element name because element names CAN be empty. It is only discriminator element names that cannot be empty (but that's an arbitrary limitation that we are imposing, in theory they could be empty because "" is an element name just as much as "_t" is).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also think it's more appropriate, I will change the wording also on the other exception.

@papafe papafe requested a review from rstam May 9, 2025 09:40
if (elementName.IndexOf('\0') != -1)
{
throw new ArgumentException("Element names cannot contain nulls.", "elementName");
throw new ArgumentException("Element names cannot be null or empty.", nameof(elementName));
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you consider slightly more precise wording?

throw new ArgumentException("Discriminator element name cannot be null or empty.", nameof(elementName));

I think element name (singular) is better than plural.

Also, I prefer Discriminator element name to Element name because element names CAN be empty. It is only discriminator element names that cannot be empty (but that's an arbitrary limitation that we are imposing, in theory they could be empty because "" is an element name just as much as "_t" is).

if (elementName.IndexOf('\0') != -1)
{
throw new ArgumentException("Element names cannot contain nulls.", "elementName");
throw new ArgumentException("Element names cannot contain nulls.", nameof(elementName));
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider Element name (singular)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it makes sense, the wording was to be uniform with the other exception. I've changed that too, I think the singular is more appropriate here.

@papafe papafe requested a review from rstam May 23, 2025 08:56
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

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

LGTM

@papafe papafe merged commit 2d3e62f into mongodb:main Jun 3, 2025
26 of 30 checks passed
@papafe papafe deleted the csharp4040 branch June 3, 2025 15:16
@sanych-sun sanych-sun changed the title CSHARP4040: Fix bug when using field with same element name as discriminator CSHARP-4040: Fix bug when using field with same element name as discriminator Jun 4, 2025
@dorki
Copy link

dorki commented Nov 20, 2025

hi @papafe and @sanych-sun ,

this change introduced a breaking behavior in our codebase and is blocking us from moving to .NET 10 (the fix for .NET 10 is
in 3.5.1).

we set the discriminator to elementName = "TypeName", which until now also existed in the classMap. this allowed us to use linq3 to translate expressions on that field, as well as Mongo’s built-in OfType.
with the new exception, we had to remove the TypeName field from the classMap, and now expression translation fails because the driver no longer recognizes it as a valid member (MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: document.TypeName).

would really appreciate a quick response — we’ve already been blocked waiting for 3.5.1.
thanks!

@papafe
Copy link
Contributor Author

papafe commented Nov 20, 2025

Hi @dorki, sorry to hear about that. Could you possibly create a ticket following the description here?
It would also be useful to have a reproducible code snippet or an example of the expressions that are failing now, so we can have a better idea of the issue and possibly recommend some alternatives. You can add that to the ticket itself.

@dorki
Copy link

dorki commented Nov 20, 2025

will do, but may i ask what was the idea behind this change? since the element selected for the discriminator, is serialized, i should be able to query it, and therefore it must exists in the class map ?

@papafe
Copy link
Contributor Author

papafe commented Nov 25, 2025

Our current discriminator implementation assumes that the discriminator is an hidden field that only the driver can set. Because of this assumption, having a field with the same element name of the discriminator is something we never intended to support, and could lead to some unexpected consequences if done involuntarily.

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.

3 participants