Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/MongoDB.Bson/Serialization/BsonClassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,12 +1325,14 @@ internal IDiscriminatorConvention GetDiscriminatorConvention()
{
// it's possible but harmless for multiple threads to do the discriminator convention lookup at the same time
discriminatorConvention = LookupDiscriminatorConvention();
_discriminatorConvention = discriminatorConvention;

if (discriminatorConvention != null)
{
EnsureNoMemberMapConflicts(discriminatorConvention.ElementName);
}

// only cache if validation succeeds

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.

Claude: When LookupDiscriminatorConvention() returns null, the new code assigns null back to _discriminatorConvention, so no caching of the null result occurs and every call re-runs the lookup walk — this is pre-existing behavior preserved by the fix, not a regression, but the comment "only cache if validation succeeds" is slightly misleading since caching null is also suppressed; consider guarding the null case separately (e.g. a dedicated sentinel or a bool _discriminatorConventionResolved flag) if the repeated lookup ever becomes a performance concern in hot paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reasonable comment, but I wanted to do the minimum amount of changes to fix this.

_discriminatorConvention = discriminatorConvention;
}

return discriminatorConvention;
Expand Down
30 changes: 30 additions & 0 deletions tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.TestHelpers;
using Xunit;

Expand Down Expand Up @@ -735,4 +736,33 @@ private class D : C
{
}
}

public class BsonClassMapGetDiscriminatorConventionTests
{
[Fact]
public void GetDiscriminatorConvention_should_throw_consistently_when_member_conflicts_with_discriminator_element_name()
{
BsonSerializer.RegisterDiscriminatorConvention(typeof(Foo), new FooDiscriminatorConvention());

var classMap = new BsonClassMap<Foo>();
classMap.AutoMap();
classMap.Freeze();

Assert.Throws<BsonSerializationException>(() => classMap.GetDiscriminatorConvention());
Assert.Throws<BsonSerializationException>(() => classMap.GetDiscriminatorConvention());
}

private class Foo
{
[BsonElement("type")]
public string Type { get; set; }
}

private class FooDiscriminatorConvention : IDiscriminatorConvention
{
public string ElementName => "type";
public Type GetActualType(IBsonReader bsonReader, Type nominalType) => nominalType;
public BsonValue GetDiscriminator(Type nominalType, Type actualType) => nominalType.Name;
}
}
}
Loading