|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Collections.ObjectModel; |
| 18 | +using FluentAssertions; |
| 19 | +using MongoDB.Bson.Serialization; |
| 20 | +using MongoDB.Bson.Serialization.Attributes; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Bson.Tests.Jira.CSharp783 |
| 24 | +{ |
| 25 | + public class CSharp4412Tests |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void IEnumerable_Ids_should_deserialize_from_ObjectIds() |
| 29 | + { |
| 30 | + var json = "{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"; |
| 31 | + |
| 32 | + var rehydrated = BsonSerializer.Deserialize<ClassWithIEnumerableIds>(json); |
| 33 | + |
| 34 | + rehydrated.Ids.Should().Equal("0102030405060708090a0b0c"); |
| 35 | + } |
| 36 | + |
| 37 | + public static readonly object[][] IEnumerable_should_serialize_as_ObjectIds_MemberData = |
| 38 | + { |
| 39 | + new object[] { new string[] { "0102030405060708090a0b0c" } }, |
| 40 | + new object[] { new List<string> { "0102030405060708090a0b0c" } }, |
| 41 | + new object[] { new Collection<string> { "0102030405060708090a0b0c" } }, |
| 42 | + new object[] { new ReadOnlyCollection<string>(new List<string> { "0102030405060708090a0b0c" }) }, |
| 43 | + new object[] { new HashSet<string> { "0102030405060708090a0b0c" } }, |
| 44 | + new object[] { new SortedSet<string> { "0102030405060708090a0b0c" } } |
| 45 | + }; |
| 46 | + |
| 47 | + [Theory] |
| 48 | + [MemberData(nameof(IEnumerable_should_serialize_as_ObjectIds_MemberData))] |
| 49 | + public void IEnumerable_should_serialize_as_ObjectIds(IEnumerable<string> value) |
| 50 | + { |
| 51 | + var document = new ClassWithIEnumerableIds { Ids = value }; |
| 52 | + |
| 53 | + var json = document.ToJson(); |
| 54 | + |
| 55 | + json.Should().Be("{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void ICollection_Ids_should_deserialize_from_ObjectIds() |
| 60 | + { |
| 61 | + var json = "{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"; |
| 62 | + |
| 63 | + var rehydrated = BsonSerializer.Deserialize<ClassWithICollectionIds>(json); |
| 64 | + |
| 65 | + rehydrated.Ids.Should().Equal("0102030405060708090a0b0c"); |
| 66 | + } |
| 67 | + |
| 68 | + public static readonly object[][] ICollection_should_serialize_as_ObjectIds_MemberData = |
| 69 | + { |
| 70 | + new object[] { new string[] { "0102030405060708090a0b0c" } }, |
| 71 | + new object[] { new List<string> { "0102030405060708090a0b0c" } }, |
| 72 | + new object[] { new Collection<string> { "0102030405060708090a0b0c" } }, |
| 73 | + new object[] { new ReadOnlyCollection<string>(new List<string> { "0102030405060708090a0b0c" }) }, |
| 74 | + new object[] { new HashSet<string> { "0102030405060708090a0b0c" } }, |
| 75 | + new object[] { new SortedSet<string> { "0102030405060708090a0b0c" } } |
| 76 | + }; |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [MemberData(nameof(ICollection_should_serialize_as_ObjectIds_MemberData))] |
| 80 | + public void ICollection_should_serialize_as_ObjectIds(ICollection<string> value) |
| 81 | + { |
| 82 | + var document = new ClassWithICollectionIds { Ids = value }; |
| 83 | + |
| 84 | + var json = document.ToJson(); |
| 85 | + |
| 86 | + json.Should().Be("{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void IList_Ids_should_deserialize_from_ObjectIds() |
| 91 | + { |
| 92 | + var json = "{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"; |
| 93 | + |
| 94 | + var rehydrated = BsonSerializer.Deserialize<ClassWithIListIds>(json); |
| 95 | + |
| 96 | + rehydrated.Ids.Should().Equal("0102030405060708090a0b0c"); |
| 97 | + } |
| 98 | + |
| 99 | + public static readonly object[][] IList_should_serialize_as_ObjectIds_MemberData = |
| 100 | + { |
| 101 | + new object[] { new string[] { "0102030405060708090a0b0c" } }, |
| 102 | + new object[] { new List<string> { "0102030405060708090a0b0c" } }, |
| 103 | + new object[] { new Collection<string> { "0102030405060708090a0b0c" } }, |
| 104 | + new object[] { new ReadOnlyCollection<string>(new List<string> { "0102030405060708090a0b0c" }) } |
| 105 | + }; |
| 106 | + |
| 107 | + [Theory] |
| 108 | + [MemberData(nameof(IList_should_serialize_as_ObjectIds_MemberData))] |
| 109 | + public void IList_should_serialize_as_ObjectIds(IList<string> value) |
| 110 | + { |
| 111 | + var document = new ClassWithIListIds { Ids = value }; |
| 112 | + |
| 113 | + var json = document.ToJson(); |
| 114 | + |
| 115 | + json.Should().Be("{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void ISet_Ids_should_deserialize_from_ObjectIds() |
| 120 | + { |
| 121 | + var json = "{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"; |
| 122 | + |
| 123 | + var rehydrated = BsonSerializer.Deserialize<ClassWithISetIds>(json); |
| 124 | + |
| 125 | + rehydrated.Ids.Should().Equal("0102030405060708090a0b0c"); |
| 126 | + } |
| 127 | + |
| 128 | + public static readonly object[][] ISet_should_serialize_as_ObjectIds_MemberData = |
| 129 | + { |
| 130 | + new object[] { new HashSet<string> { "0102030405060708090a0b0c" } }, |
| 131 | + new object[] { new SortedSet<string> { "0102030405060708090a0b0c" } } |
| 132 | + }; |
| 133 | + |
| 134 | + [Theory] |
| 135 | + [MemberData(nameof(ISet_should_serialize_as_ObjectIds_MemberData))] |
| 136 | + public void ISet_should_serialize_as_ObjectIds(ISet<string> value) |
| 137 | + { |
| 138 | + var document = new ClassWithISetIds { Ids = value }; |
| 139 | + |
| 140 | + var json = document.ToJson(); |
| 141 | + |
| 142 | + json.Should().Be("{ \"Ids\" : [ObjectId(\"0102030405060708090a0b0c\")] }"); |
| 143 | + } |
| 144 | + |
| 145 | + private class ClassWithIEnumerableIds |
| 146 | + { |
| 147 | + [BsonRepresentation(BsonType.ObjectId)] |
| 148 | + public IEnumerable<string> Ids { get; set; } |
| 149 | + } |
| 150 | + |
| 151 | + private class ClassWithICollectionIds |
| 152 | + { |
| 153 | + [BsonRepresentation(BsonType.ObjectId)] |
| 154 | + public ICollection<string> Ids { get; set; } |
| 155 | + } |
| 156 | + |
| 157 | + private class ClassWithIListIds |
| 158 | + { |
| 159 | + [BsonRepresentation(BsonType.ObjectId)] |
| 160 | + public IList<string> Ids { get; set; } |
| 161 | + } |
| 162 | + |
| 163 | + private class ClassWithISetIds |
| 164 | + { |
| 165 | + [BsonRepresentation(BsonType.ObjectId)] |
| 166 | + public ISet<string> Ids { get; set; } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments