-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-3494: Fix discriminator for generic types #1685
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 all commits
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* 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.Driver.Tests.Linq.Linq3Implementation; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Jira | ||
{ | ||
public class CSharp3494Tests : Linq3IntegrationTest | ||
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. We can decide to move these tests somewhere else if it makes more sense. Also this test does not need to derive from 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. If these tests don't use the driver they should live in the But you may want to make integration tests also, where we write a document to the database and prove that we can read it back. |
||
{ | ||
abstract class BaseDocument; | ||
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. Normally we put all the classes after the test methods. In this file they are mixed in an unusual order, switching back and forth between test methods and class declarations. |
||
|
||
class DerivedDocument<T> : BaseDocument | ||
{ | ||
[BsonId] | ||
public int Id { get; set; } | ||
|
||
public T Value { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Correct_discriminator_should_be_used_for_generic_type() | ||
{ | ||
var document = new DerivedDocument<int> { Id = 1, Value = 42 }; | ||
var serialized = document.ToJson(typeof(BaseDocument)); | ||
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. Consider using the parameter name to make it clear that we're calling
|
||
serialized.Should().Be("""{ "_t" : "DerivedDocument<Int32>", "_id" : 1, "Value" : 42 }"""); | ||
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 thought the But maybe we only do that during serialization (and only in |
||
} | ||
|
||
[BsonKnownTypes(typeof(DerivedDocument2<int>))] | ||
abstract class BaseDocument2 {} | ||
|
||
class DerivedDocument2<T> : BaseDocument2 | ||
{ | ||
[BsonId] | ||
public int Id { get; set; } | ||
|
||
public T Value { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Correct_type_should_be_instantiated_with_discriminator_for_generic_type() | ||
{ | ||
//This test needs to use a different set of classes than the previous one, otherwise the discriminators could have been already | ||
//registered, depending on the order of the tests. We need BsonKnownTypes for this to work. | ||
var serialized = """{ "_t" : "DerivedDocument2<Int32>", "_id" : 1, "Value" : 42 }"""; | ||
var rehydrated = BsonSerializer.Deserialize<BaseDocument2>(serialized); | ||
rehydrated.Should().BeOfType<DerivedDocument2<int>>(); | ||
} | ||
} | ||
} | ||
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. We should test generic types with more than one type parameter like And see if you can come up with a test with nested generic types, maybe something like |
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.
This might fix it for
BsonClassMap
based serializers.But shouldn't this be fixed more generally somehow?