|
13 | 13 | * limitations under the License.
|
14 | 14 | */
|
15 | 15 |
|
| 16 | +using System.Collections.Generic; |
16 | 17 | using System.Linq;
|
| 18 | +using FluentAssertions; |
17 | 19 | using MongoDB.Bson.Serialization;
|
18 | 20 | using MongoDB.Bson.Serialization.Attributes;
|
19 | 21 | using MongoDB.Bson.TestHelpers;
|
@@ -65,6 +67,79 @@ private class H : G
|
65 | 67 | {
|
66 | 68 | }
|
67 | 69 |
|
| 70 | + // BaseDocument and derived classes are used for tests with generic types |
| 71 | + // It's necessary to specify the derived specific types with BsonKnownTypes for the deserialization to work. |
| 72 | + [BsonKnownTypes(typeof(DerivedDocument<int>))] |
| 73 | + [BsonKnownTypes(typeof(DerivedDocument<List<Dictionary<string, int>>>))] |
| 74 | + [BsonKnownTypes(typeof(DerivedDocumentDouble<int, string>))] |
| 75 | + abstract class BaseDocument; |
| 76 | + |
| 77 | + class DerivedDocument<T> : BaseDocument |
| 78 | + { |
| 79 | + [BsonId] |
| 80 | + public int Id { get; set; } |
| 81 | + |
| 82 | + public T Value { get; set; } |
| 83 | + } |
| 84 | + |
| 85 | + class DerivedDocumentDouble<T1, T2> : BaseDocument |
| 86 | + { |
| 87 | + [BsonId] |
| 88 | + public int Id { get; set; } |
| 89 | + |
| 90 | + public T1 Value1 { get; set; } |
| 91 | + |
| 92 | + public T2 Value2 { get; set; } |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void TestDeserializeGenericType() |
| 97 | + { |
| 98 | + var serialized = """{ "_t" : "DerivedDocument<Int32>", "_id" : 1, "Value" : 42 }"""; |
| 99 | + var rehydrated = BsonSerializer.Deserialize<BaseDocument>(serialized); |
| 100 | + rehydrated.Should().BeOfType<DerivedDocument<int>>(); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void TestDeserializeGenericTypeWithNestedType() |
| 105 | + { |
| 106 | + var serialized = """{ "_t" : "DerivedDocument<List<Dictionary<String, Int32>>>", "_id" : 1, "Value" : [{ "key" : 1 }] }"""; |
| 107 | + var rehydrated = BsonSerializer.Deserialize<BaseDocument>(serialized); |
| 108 | + rehydrated.Should().BeOfType<DerivedDocument<List<Dictionary<string, int>>>>(); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void TestDeserializeGenericTypeWithTwoTypes() |
| 113 | + { |
| 114 | + var serialized = """{ "_t" : "DerivedDocumentDouble<Int32, String>", "_id" : 1, "Value1" : 42, "Value2" : "hello" }"""; |
| 115 | + var rehydrated = BsonSerializer.Deserialize<BaseDocument>(serialized); |
| 116 | + rehydrated.Should().BeOfType<DerivedDocumentDouble<int,string>>(); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void TestSerializeGenericType() |
| 121 | + { |
| 122 | + var document = new DerivedDocument<int> { Id = 1, Value = 42 }; |
| 123 | + var serialized = document.ToJson(typeof(BaseDocument)); |
| 124 | + serialized.Should().Be("""{ "_t" : "DerivedDocument<Int32>", "_id" : 1, "Value" : 42 }"""); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void TestSerializeGenericTypeWithNestedType() |
| 129 | + { |
| 130 | + var document = new DerivedDocument<List<Dictionary<string, int>>> { Id = 1, Value = [new() { { "key", 1 } }] }; |
| 131 | + var serialized = document.ToJson(typeof(BaseDocument)); |
| 132 | + serialized.Should().Be("""{ "_t" : "DerivedDocument<List<Dictionary<String, Int32>>>", "_id" : 1, "Value" : [{ "key" : 1 }] }"""); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void TestSerializeGenericTypeWithTwoTypes() |
| 137 | + { |
| 138 | + var document = new DerivedDocumentDouble<int, string> { Id = 1, Value1 = 42, Value2 = "hello"}; |
| 139 | + var serialized = document.ToJson(typeof(BaseDocument)); |
| 140 | + serialized.Should().Be("""{ "_t" : "DerivedDocumentDouble<Int32, String>", "_id" : 1, "Value1" : 42, "Value2" : "hello" }"""); |
| 141 | + } |
| 142 | + |
68 | 143 | [Fact]
|
69 | 144 | public void TestSerializeObjectasObject()
|
70 | 145 | {
|
|
0 commit comments