Skip to content

CSHARP4040: Fix bug when using field with same element name as discriminator #1684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion src/MongoDB.Bson/Serialization/BsonClassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,10 +1323,25 @@ internal IDiscriminatorConvention GetDiscriminatorConvention()
var discriminatorConvention = _discriminatorConvention;
if (discriminatorConvention == null)
{
// it's possible but harmless for multiple threads to do the discriminator convention lookukp at the same time
// 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)
{
var conflictingMemberMap = _allMemberMaps.FirstOrDefault(memberMap => memberMap.ElementName == discriminatorConvention.ElementName);

if (conflictingMemberMap != null)
{
var fieldOrProperty = conflictingMemberMap.MemberInfo is FieldInfo ? "field" : "property";

throw new BsonSerializationException(
$"The discriminator element name cannot be {discriminatorConvention.ElementName} " +
$"because it is already being used by the {fieldOrProperty} {conflictingMemberMap.MemberName} of type {_classType.FullName}");
}
}
}

return discriminatorConvention;

IDiscriminatorConvention LookupDiscriminatorConvention()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ public abstract class StandardDiscriminatorConvention : IDiscriminatorConvention
/// <param name="elementName">The element name.</param>
protected StandardDiscriminatorConvention(string elementName)
{
if (elementName == null)
if (string.IsNullOrEmpty(elementName))
{
throw new ArgumentNullException("elementName");
throw new ArgumentException("Element names cannot be null or empty.", nameof(elementName));
}
if (elementName.IndexOf('\0') != -1)
{
throw new ArgumentException("Element names cannot contain nulls.", "elementName");
throw new ArgumentException("Element names cannot contain nulls.", nameof(elementName));
}

_elementName = elementName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void TestConstructorThrowsWhenElementNameContainsNulls()
[Fact]
public void TestConstructorThrowsWhenElementNameIsNull()
{
Assert.Throws<ArgumentNullException>(() => new ScalarDiscriminatorConvention(null));
Assert.Throws<ArgumentException>(() => new ScalarDiscriminatorConvention(null));
}

[Fact]
Expand Down
47 changes: 47 additions & 0 deletions tests/MongoDB.Driver.Tests/Jira/CSharp4040Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Xunit;

namespace MongoDB.Driver.Tests.Jira
{
public class CSharp4040Tests
{
private class BaseDocument
{
[BsonId] public ObjectId Id { get; set; } = ObjectId.GenerateNewId();

[BsonElement("_t")]
public string Field1 { get; set; }
}

private class DerivedDocument : BaseDocument {}

[Fact]
public void BsonClassMapSerializer_serialization_when_using_field_with_same_element_name_as_discriminator_should_throw()
{
var obj = new DerivedDocument { Field1 = "field1" };

var recordedException = Record.Exception(() => obj.ToJson(typeof(BaseDocument)));
recordedException.Should().NotBeNull();
recordedException.Should().BeOfType<BsonSerializationException>();
recordedException.Message.Should().Be("The discriminator element name cannot be _t because it is already being used" +
" by the property Field1 of type MongoDB.Driver.Tests.Jira.CSharp4040Tests+DerivedDocument");
}
}
}