Skip to content

Commit

Permalink
CSHARP-702: fix ObjectId conversion to itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed Mar 13, 2013
1 parent d8788b9 commit c2f17f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MongoDB.Bson/ObjectModel/ObjectId.cs
Expand Up @@ -554,6 +554,10 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
case TypeCode.String:
return ((IConvertible)this).ToString(provider);
case TypeCode.Object:
if (conversionType == typeof(ObjectId))
{
return this;
}
if (conversionType == typeof(BsonObjectId))
{
return new BsonObjectId(this);
Expand Down
10 changes: 10 additions & 0 deletions MongoDB.BsonUnitTests/ObjectModel/ObjectIdTests.cs
Expand Up @@ -339,5 +339,15 @@ public void TestTryParse()
Assert.IsFalse(ObjectId.TryParse("00102030405060708090a0b0c", out objectId1)); // too long
Assert.IsFalse(ObjectId.TryParse(null, out objectId1)); // should return false not throw ArgumentNullException
}

[Test]
public void TestConvertObjectIdToObjectId()
{
var oid = ObjectId.GenerateNewId();

var oidConverted = Convert.ChangeType(oid, typeof(ObjectId));

Assert.AreEqual(oid, oidConverted);
}
}
}

0 comments on commit c2f17f1

Please sign in to comment.