-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
10f9e11
770f24d
a6612bb
4cf885f
2f46ff4
3e53e45
68b5985
5e8b734
97bfe4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,14 +39,19 @@ 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 ArgumentNullException(nameof(elementName)); | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Should not have been changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this should have gone for the previous condition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you consider slightly more precise wording?
I think Also, I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
if (elementName == "_id") | ||
{ | ||
throw new ArgumentException("Element names cannot be '_id'.", nameof(elementName)); | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
_elementName = elementName; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* 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; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Bson.Serialization.Conventions; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Jira | ||
{ | ||
public class Csharp4040Tests | ||
papafe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private class BaseDocument | ||
{ | ||
[BsonId] public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); | ||
|
||
[BsonElement("_t")] | ||
public string Field1 { get; set; } | ||
} | ||
|
||
private class DerivedDocument : BaseDocument {} | ||
|
||
[Fact] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests can eventually be moved to somewhere more appropriate. |
||
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 property Field1 of type 'MongoDB.Driver.Tests.Jira.Csharp4040Tests+DerivedDocument' " + | ||
"cannot use element name '_t' because it is already being used by " + | ||
"the discriminator convention 'ScalarDiscriminatorConvention'."); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
istrue
only whenGetDiscriminatorConvention
is called inBsonClassMapSerializer.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.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.