-
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
Changes from 8 commits
6422763
9570ae0
35fb461
2132420
bb4795e
20c01e2
cf10e77
bbe4516
6446d0a
32d3a3e
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 |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Bson.TestHelpers; | ||
|
@@ -65,6 +67,101 @@ private class H : G | |
{ | ||
} | ||
|
||
// BaseDocument, BasedDocument2 and derived classes are used for tests with generic types | ||
abstract class BaseDocument; | ||
|
||
class DerivedDocument<T> : BaseDocument | ||
{ | ||
[BsonId] | ||
public int Id { get; set; } | ||
|
||
public T Value { get; set; } | ||
} | ||
|
||
class DerivedDocumentDouble<T1, T2> : BaseDocument | ||
{ | ||
[BsonId] | ||
public int Id { get; set; } | ||
|
||
public T1 Value1 { get; set; } | ||
|
||
public T2 Value2 { get; set; } | ||
} | ||
|
||
//The deserialization tests for generic types needs to use a different set of classes than the serialization ones, | ||
//otherwise the discriminators could have been already registered, depending on the order of the tests. | ||
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. Why is this dependent on the order of the tests? Configuration of classes/discriminators should work no matter what order the tests are run in. 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 think this here at this stage was probably unnecessary and I've uniformed the tests. |
||
//It's necessary to specify the derived specific types with BsonKnownTypes for this to work. | ||
[BsonKnownTypes(typeof(DerivedDocument2<int>))] | ||
[BsonKnownTypes(typeof(DerivedDocument2<List<Dictionary<string, int>>>))] | ||
[BsonKnownTypes(typeof(DerivedDocumentDouble2<int, string>))] | ||
abstract class BaseDocument2 {} | ||
|
||
class DerivedDocument2<T> : BaseDocument2 | ||
{ | ||
[BsonId] | ||
public int Id { get; set; } | ||
|
||
public T Value { get; set; } | ||
} | ||
|
||
class DerivedDocumentDouble2<T1, T2> : BaseDocument2 | ||
{ | ||
[BsonId] | ||
public int Id { get; set; } | ||
|
||
public T1 Value1 { get; set; } | ||
|
||
public T2 Value2 { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void TestDeserializeGenericType() | ||
{ | ||
var serialized = """{ "_t" : "DerivedDocument2<Int32>", "_id" : 1, "Value" : 42 }"""; | ||
var rehydrated = BsonSerializer.Deserialize<BaseDocument2>(serialized); | ||
rehydrated.Should().BeOfType<DerivedDocument2<int>>(); | ||
} | ||
|
||
[Fact] | ||
public void TestDeserializeGenericTypeWithNestedType() | ||
{ | ||
var serialized = """{ "_t" : "DerivedDocument2<List<Dictionary<String, Int32>>>", "_id" : 1, "Value" : [{ "key" : 1 }] }"""; | ||
var rehydrated = BsonSerializer.Deserialize<BaseDocument2>(serialized); | ||
rehydrated.Should().BeOfType<DerivedDocument2<List<Dictionary<string, int>>>>(); | ||
} | ||
|
||
[Fact] | ||
public void TestDeserializeGenericTypeWithTwoTypes() | ||
{ | ||
var serialized = """{ "_t" : "DerivedDocumentDouble2<Int32, String>", "_id" : 1, "Value1" : 42, "Value2" : "hello" }"""; | ||
var rehydrated = BsonSerializer.Deserialize<BaseDocument2>(serialized); | ||
rehydrated.Should().BeOfType<DerivedDocumentDouble2<int,string>>(); | ||
} | ||
|
||
[Fact] | ||
public void TestSerializeGenericType() | ||
{ | ||
var document = new DerivedDocument<int> { Id = 1, Value = 42 }; | ||
var serialized = document.ToJson(typeof(BaseDocument)); | ||
serialized.Should().Be("""{ "_t" : "DerivedDocument<Int32>", "_id" : 1, "Value" : 42 }"""); | ||
} | ||
|
||
[Fact] | ||
public void TestSerializeGenericTypeWithNestedType() | ||
{ | ||
var document = new DerivedDocument<List<Dictionary<string, int>>> { Id = 1, Value = [new() { { "key", 1 } }] }; | ||
var serialized = document.ToJson(typeof(BaseDocument)); | ||
serialized.Should().Be("""{ "_t" : "DerivedDocument<List<Dictionary<String, Int32>>>", "_id" : 1, "Value" : [{ "key" : 1 }] }"""); | ||
} | ||
|
||
[Fact] | ||
public void TestSerializeGenericTypeWithTwoTypes() | ||
{ | ||
var document = new DerivedDocumentDouble<int, string> { Id = 1, Value1 = 42, Value2 = "hello"}; | ||
var serialized = document.ToJson(typeof(BaseDocument)); | ||
serialized.Should().Be("""{ "_t" : "DerivedDocumentDouble<Int32, String>", "_id" : 1, "Value1" : 42, "Value2" : "hello" }"""); | ||
} | ||
|
||
[Fact] | ||
public void TestSerializeObjectasObject() | ||
{ | ||
|
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?
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 had an offline discussion, and we decided it would make sense to keep this fix limited to
BsonClassMap
based serializers.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.
Do we need a new JIRA ticket for supporting discriminators for generic types when NOT using
BsonClassMap
?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 have created this ticket here to try to make the discriminator conventions completely separated from
BsonClassMap
.