Skip to content

Commit bf84f8d

Browse files
author
rstam
committed
Fixed CSHARP-446. BsonDocument methods GetDocumentId and SetDocumentId have been changed to be temporarily backward compatible with 1.4.0 and earlier. However, these methods were intended to be private, and all IBsonSerializable methods that were accidentally made public have been marked as deprecated and will be made private by explicit implementation in a future release.
1 parent a9f296c commit bf84f8d

20 files changed

+348
-245
lines changed

Bson/ObjectModel/BsonDocument.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public static BsonDocument Parse(string json)
381381
using (var bsonReader = BsonReader.Create(json))
382382
{
383383
var document = new BsonDocument();
384-
return (BsonDocument)document.Deserialize(bsonReader, typeof(BsonDocument), null);
384+
return (BsonDocument)((IBsonSerializable)document).Deserialize(bsonReader, typeof(BsonDocument), null);
385385
}
386386
}
387387

@@ -406,7 +406,7 @@ public static BsonDocument ReadFrom(BsonBuffer buffer)
406406
public static new BsonDocument ReadFrom(BsonReader bsonReader)
407407
{
408408
BsonDocument document = new BsonDocument();
409-
return (BsonDocument)document.Deserialize(bsonReader, typeof(BsonDocument), null);
409+
return (BsonDocument)((IBsonSerializable)document).Deserialize(bsonReader, typeof(BsonDocument), null);
410410
}
411411

412412
/// <summary>
@@ -754,6 +754,7 @@ public override BsonValue DeepClone()
754754
/// <param name="nominalType">The nominal type of the object (ignored, but should be BsonDocument).</param>
755755
/// <param name="options">The serialization options (ignored).</param>
756756
/// <returns>The document (which has now been initialized by deserialization), or null.</returns>
757+
[Obsolete("Deserialize was intended to be private and will become private in a future release.")]
757758
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
758759
{
759760
if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
@@ -793,16 +794,25 @@ public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializ
793794
/// <param name="idNominalType">The nominal type of the Id.</param>
794795
/// <param name="idGenerator">The IdGenerator for the Id (or null).</param>
795796
/// <returns>True (a BsonDocument either has an Id member or one can be added).</returns>
797+
[Obsolete("GetDocumentId was intended to be private and will become private in a future release. Use document[\"_id\"] or document.GetValue(\"_id\") instead.")]
796798
public bool GetDocumentId(out object id, out Type idNominalType, out IIdGenerator idGenerator)
797799
{
798800
BsonElement idElement;
799801
if (TryGetElement("_id", out idElement))
800802
{
801-
id = idElement.Value;
803+
// TODO: in a future release we will always return a BsonValue (GetDocumentId is not supposed to transform Id values in any way)
804+
// we're returning the raw value in 1.4.2 to remain temporarily backward compatible with 1.4.0 and earlier
805+
id = idElement.Value.RawValue;
806+
if (id == null)
807+
{
808+
id = idElement.Value;
809+
}
802810
idGenerator = BsonSerializer.LookupIdGenerator(id.GetType());
803811

804812
if (idGenerator == null)
805813
{
814+
// note: in 1.4.2 this code isn't yet used because if the RawValue was a Guid then the GuidIdGenerator was found
815+
// but once we start returning BsonValue like we should this code will start being used
806816
var idBinaryData = id as BsonBinaryData;
807817
if (idBinaryData != null && (idBinaryData.SubType == BsonBinarySubType.UuidLegacy || idBinaryData.SubType == BsonBinarySubType.UuidStandard))
808818
{
@@ -813,7 +823,7 @@ public bool GetDocumentId(out object id, out Type idNominalType, out IIdGenerato
813823
else
814824
{
815825
id = null;
816-
idGenerator = BsonObjectIdGenerator.Instance;
826+
idGenerator = ObjectIdGenerator.Instance; // TODO: in a future release we will return an instance of BsonObjectIdGenerator
817827
}
818828

819829
idNominalType = typeof(BsonValue);
@@ -1041,6 +1051,7 @@ public void RemoveElement(BsonElement element)
10411051
/// <param name="bsonWriter">The writer.</param>
10421052
/// <param name="nominalType">The nominalType.</param>
10431053
/// <param name="options">The serialization options (can be null).</param>
1054+
[Obsolete("Serialize was intended to be private and will become private in a future release.")]
10441055
public void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
10451056
{
10461057
if (bsonWriter == null)
@@ -1124,13 +1135,24 @@ public BsonDocument Set(string name, BsonValue value)
11241135
/// Sets the document Id.
11251136
/// </summary>
11261137
/// <param name="id">The value of the Id.</param>
1138+
[Obsolete("SetDocumentId was intended to be private and will become private in a future release. Use document[\"_id\"] = value or document.Set(\"_id\", value) instead.")]
11271139
public void SetDocumentId(object id)
11281140
{
11291141
if (id == null)
11301142
{
11311143
throw new ArgumentNullException("id");
11321144
}
1133-
var idBsonValue = (BsonValue)id;
1145+
1146+
// TODO: in a future release we will just cast directly to BsonValue because the caller is required to pass a BsonValue
1147+
// var idBsonValue = (BsonValue)id;
1148+
1149+
var idBsonValue = id as BsonValue;
1150+
if (idBsonValue == null)
1151+
{
1152+
// SetDocumentId contract requires that caller provide an id of the correct type so this conversion is not correct
1153+
// for temporary backward compatibility with versions prior to 1.4.2 we continue to map objects to BsonValue for now
1154+
idBsonValue = BsonValue.Create(id); // TODO: in a future release this mapping will be removed
1155+
}
11341156

11351157
BsonElement idElement;
11361158
if (TryGetElement("_id", out idElement))
@@ -1280,7 +1302,7 @@ public bool TryGetValue(string name, out BsonValue value)
12801302
/// <param name="bsonWriter">The writer.</param>
12811303
public new void WriteTo(BsonWriter bsonWriter)
12821304
{
1283-
Serialize(bsonWriter, typeof(BsonDocument), null);
1305+
((IBsonSerializable)this).Serialize(bsonWriter, typeof(BsonDocument), null);
12841306
}
12851307

12861308
/// <summary>

Bson/ObjectModel/BsonDocumentWrapper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public override int CompareTo(BsonValue other)
202202
/// <param name="nominalType">Not applicable.</param>
203203
/// <param name="options">Not applicable.</param>
204204
/// <returns>Not applicable.</returns>
205+
[Obsolete("Deserialize was intended to be private and will become private in a future release.")]
205206
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
206207
{
207208
throw new NotSupportedException();
@@ -214,6 +215,7 @@ public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializ
214215
/// <param name="idNominalType">Not applicable.</param>
215216
/// <param name="idGenerator">Not applicable.</param>
216217
/// <returns>Not applicable.</returns>
218+
[Obsolete("GetDocumentId was intended to be private and will become private in a future release.")]
217219
public bool GetDocumentId(out object id, out Type idNominalType, out IIdGenerator idGenerator)
218220
{
219221
throw new NotSupportedException();
@@ -244,6 +246,7 @@ public override int GetHashCode()
244246
/// <param name="bsonWriter">The writer.</param>
245247
/// <param name="nominalType">The nominal type (overridded by the wrapped nominal type).</param>
246248
/// <param name="options">The serialization options.</param>
249+
[Obsolete("Serialize was intended to be private and will become private in a future release.")]
247250
public void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
248251
{
249252
if (_isUpdateDocument)
@@ -272,6 +275,7 @@ public void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializatio
272275
/// SetDocumentId is an invalid operation for BsonDocumentWrapper.
273276
/// </summary>
274277
/// <param name="Id">Not applicable.</param>
278+
[Obsolete("SetDocumentId was intended to be private and will become private in a future release.")]
275279
public void SetDocumentId(object Id)
276280
{
277281
throw new NotSupportedException();

Bson/ObjectModel/BsonValue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Xml;
2222

2323
using MongoDB.Bson.IO;
24+
using MongoDB.Bson.Serialization;
2425

2526
namespace MongoDB.Bson
2627
{
@@ -1297,7 +1298,7 @@ public void WriteTo(BsonWriter bsonWriter)
12971298
var documentWrapper = this as BsonDocumentWrapper;
12981299
if (documentWrapper != null)
12991300
{
1300-
documentWrapper.Serialize(bsonWriter, typeof(BsonDocument), null);
1301+
((IBsonSerializable)documentWrapper).Serialize(bsonWriter, typeof(BsonDocument), null);
13011302
}
13021303
else
13031304
{

Bson/Serialization/BsonSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static object Deserialize(BsonReader bsonReader, Type nominalType, IBsonS
201201
if (nominalType == typeof(BsonDocument))
202202
{
203203
var bsonDocument = new BsonDocument();
204-
return bsonDocument.Deserialize(bsonReader, nominalType, options);
204+
return ((IBsonSerializable)bsonDocument).Deserialize(bsonReader, nominalType, options);
205205
}
206206

207207
// if nominalType is an interface find out the actualType and use it instead

Bson/Serialization/Serializers/BsonValueSerializers.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public override bool GetDocumentId(
601601
out IIdGenerator idGenerator)
602602
{
603603
var bsonDocument = (BsonDocument)document;
604-
return bsonDocument.GetDocumentId(out id, out idNominalType, out idGenerator);
604+
return ((IBsonSerializable)bsonDocument).GetDocumentId(out id, out idNominalType, out idGenerator);
605605
}
606606

607607
/// <summary>
@@ -623,8 +623,8 @@ public override void Serialize(
623623
}
624624
else
625625
{
626-
var document = (BsonDocument)value;
627-
document.Serialize(bsonWriter, nominalType, options);
626+
var bsonDocument = (BsonDocument)value;
627+
((IBsonSerializable)bsonDocument).Serialize(bsonWriter, nominalType, options);
628628
}
629629
}
630630

@@ -636,7 +636,7 @@ public override void Serialize(
636636
public override void SetDocumentId(object document, object id)
637637
{
638638
var bsonDocument = (BsonDocument)document;
639-
bsonDocument.SetDocumentId(id);
639+
((IBsonSerializable)bsonDocument).SetDocumentId(id);
640640
}
641641
}
642642

@@ -702,8 +702,8 @@ public override void Serialize(
702702
}
703703
else
704704
{
705-
var documentWrapper = (BsonDocumentWrapper)value;
706-
documentWrapper.Serialize(bsonWriter, nominalType, options);
705+
var bsonDocumentWrapper = (BsonDocumentWrapper)value;
706+
((IBsonSerializable)bsonDocumentWrapper).Serialize(bsonWriter, nominalType, options);
707707
}
708708
}
709709
}

0 commit comments

Comments
 (0)