Browse Source

Fixed CSHARP-98. GetActualType needs to be a little more precise about where the reader is sitting when it is called.

pull/27/head
rstam 15 years ago
parent
commit
71e1836d45
  1. 54
      Bson/DefaultSerializer/Conventions/DiscriminatorConventions.cs
  2. 1
      DriverOnlineTests/DriverOnlineTests.csproj
  3. 56
      DriverOnlineTests/Jira/CSharp98Tests.cs

54
Bson/DefaultSerializer/Conventions/DiscriminatorConventions.cs

@ -68,34 +68,38 @@ namespace MongoDB.Bson.DefaultSerializer.Conventions {
BsonReader bsonReader,
Type nominalType
) {
var bsonType = bsonReader.CurrentBsonType;
Type primitiveType = null;
switch (bsonType) {
case BsonType.Boolean: primitiveType = typeof(bool); break;
case BsonType.Binary:
var bookmark = bsonReader.GetBookmark();
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType == BsonBinarySubType.Uuid && bytes.Length == 16) {
primitiveType = typeof(Guid);
}
bsonReader.ReturnToBookmark(bookmark);
break;
case BsonType.DateTime: primitiveType = typeof(DateTime); break;
case BsonType.Double: primitiveType = typeof(double); break;
case BsonType.Int32: primitiveType = typeof(int); break;
case BsonType.Int64: primitiveType = typeof(long); break;
case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
case BsonType.String: primitiveType = typeof(string); break;
}
if (bsonReader.ReadState == BsonReadState.Value) {
Type primitiveType = null;
switch (bsonReader.CurrentBsonType) {
case BsonType.Boolean: primitiveType = typeof(bool); break;
case BsonType.Binary:
var bookmark = bsonReader.GetBookmark();
byte[] bytes;
BsonBinarySubType subType;
bsonReader.ReadBinaryData(out bytes, out subType);
if (subType == BsonBinarySubType.Uuid && bytes.Length == 16) {
primitiveType = typeof(Guid);
}
bsonReader.ReturnToBookmark(bookmark);
break;
case BsonType.DateTime: primitiveType = typeof(DateTime); break;
case BsonType.Double: primitiveType = typeof(double); break;
case BsonType.Int32: primitiveType = typeof(int); break;
case BsonType.Int64: primitiveType = typeof(long); break;
case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
case BsonType.String: primitiveType = typeof(string); break;
}
if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) {
return primitiveType;
if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) {
return primitiveType;
}
}
if (bsonType == BsonType.Document) {
if (
bsonReader.ReadState == BsonReadState.Initial ||
bsonReader.ReadState == BsonReadState.Done ||
(bsonReader.ReadState == BsonReadState.Value && bsonReader.CurrentBsonType == BsonType.Document)
) {
var bookmark = bsonReader.GetBookmark();
bsonReader.ReadStartDocument();
var actualType = nominalType;

1
DriverOnlineTests/DriverOnlineTests.csproj

@ -76,6 +76,7 @@
<Compile Include="Jira\CSharp77Tests.cs" />
<Compile Include="Jira\CSharp92Tests.cs" />
<Compile Include="Jira\CSharp93Tests.cs" />
<Compile Include="Jira\CSharp98Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Samples\MagicDiscriminatorTests.cs" />
</ItemGroup>

56
DriverOnlineTests/Jira/CSharp98Tests.cs

@ -0,0 +1,56 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson;
using MongoDB.Bson.DefaultSerializer;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace MongoDB.DriverOnlineTests.Jira.CSharp98 {
[TestFixture]
public class CSharp98Tests {
private class A {
[BsonId]
public ObjectId Id { get; set; }
public int PA { get; set; }
}
private class B : A {
public int PB { get; set; }
}
[Test]
public void TestDeserializationOfTwoBs() {
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var database = server["onlinetests"];
var collection = database.GetCollection<A>("csharp98");
collection.RemoveAll();
var b1 = new B { PA = 1, PB = 2 };
var b2 = new B { PA = 3, PB = 4 };
collection.Insert<A>(b1);
collection.Insert<A>(b2);
var docs = collection.FindAll().ToList();
}
}
}
Loading…
Cancel
Save