-
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
Open
papafe
wants to merge
8
commits into
mongodb:main
Choose a base branch
from
papafe:csharp3494
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6422763
CSHARP-3494: Fix discriminator for generic types
papafe 9570ae0
Added tests
papafe 35fb461
Simplification
papafe 2132420
Name correction
papafe bb4795e
Fixed test
papafe 20c01e2
Improved testing
papafe cf10e77
Moved tests.
papafe bbe4516
Added comment.
papafe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
||
//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() | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
?