Skip to content

Commit c2f17f1

Browse files
committed
CSHARP-702: fix ObjectId conversion to itself.
1 parent d8788b9 commit c2f17f1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

MongoDB.Bson/ObjectModel/ObjectId.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,10 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
554554
case TypeCode.String:
555555
return ((IConvertible)this).ToString(provider);
556556
case TypeCode.Object:
557+
if (conversionType == typeof(ObjectId))
558+
{
559+
return this;
560+
}
557561
if (conversionType == typeof(BsonObjectId))
558562
{
559563
return new BsonObjectId(this);

MongoDB.BsonUnitTests/ObjectModel/ObjectIdTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,5 +339,15 @@ public void TestTryParse()
339339
Assert.IsFalse(ObjectId.TryParse("00102030405060708090a0b0c", out objectId1)); // too long
340340
Assert.IsFalse(ObjectId.TryParse(null, out objectId1)); // should return false not throw ArgumentNullException
341341
}
342+
343+
[Test]
344+
public void TestConvertObjectIdToObjectId()
345+
{
346+
var oid = ObjectId.GenerateNewId();
347+
348+
var oidConverted = Convert.ChangeType(oid, typeof(ObjectId));
349+
350+
Assert.AreEqual(oid, oidConverted);
351+
}
342352
}
343353
}

0 commit comments

Comments
 (0)