Skip to content

Commit f5b9db9

Browse files
committed
CSHARP-2622: IdGenerator not working for inserts after using oftype on collection.
1 parent 0543c63 commit f5b9db9

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/MongoDB.Driver/OfTypeSerializer.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson.Serialization;
1718
using MongoDB.Bson.Serialization.Serializers;
1819

1920
namespace MongoDB.Driver
2021
{
21-
internal sealed class OfTypeSerializer<TRootDocument, TDerivedDocument> : SerializerBase<TDerivedDocument>, IBsonDocumentSerializer
22+
internal sealed class OfTypeSerializer<TRootDocument, TDerivedDocument> : SerializerBase<TDerivedDocument>, IBsonDocumentSerializer, IBsonIdProvider
2223
where TDerivedDocument : TRootDocument
2324
{
2425
private readonly IBsonSerializer<TDerivedDocument> _derivedDocumentSerializer;
@@ -34,12 +35,35 @@ public override TDerivedDocument Deserialize(BsonDeserializationContext context,
3435
return _derivedDocumentSerializer.Deserialize(context, args);
3536
}
3637

38+
public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator)
39+
{
40+
if (_derivedDocumentSerializer is IBsonIdProvider idProvider)
41+
{
42+
return idProvider.GetDocumentId(document, out id, out idNominalType, out idGenerator);
43+
}
44+
else
45+
{
46+
id = null;
47+
idNominalType = null;
48+
idGenerator = null;
49+
return false;
50+
}
51+
}
52+
3753
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TDerivedDocument value)
3854
{
3955
args.NominalType = typeof(TRootDocument);
4056
_derivedDocumentSerializer.Serialize(context, args, value);
4157
}
4258

59+
public void SetDocumentId(object document, object id)
60+
{
61+
if (_derivedDocumentSerializer is IBsonIdProvider idProvider)
62+
{
63+
idProvider.SetDocumentId(document, id);
64+
}
65+
}
66+
4367
public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
4468
{
4569
var documentSerializer = _derivedDocumentSerializer as IBsonDocumentSerializer;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright 2019-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;
17+
using FluentAssertions;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Jira
21+
{
22+
public class CSharp2622Tests
23+
{
24+
// public methods
25+
[Fact]
26+
public void InsertOne_to_oftype_collection_should_generate_id()
27+
{
28+
var collectionNamespace = CoreTestConfiguration.GetCollectionNamespaceForTestClass(typeof(CSharp2622Tests));
29+
var databaseName = collectionNamespace.DatabaseNamespace.DatabaseName;
30+
var collectionName = collectionNamespace.CollectionName;
31+
var client = DriverTestConfiguration.Client;
32+
var database = client.GetDatabase(databaseName);
33+
var collection = database.GetCollection<C>(collectionName);
34+
var ofTypeCollection = collection.OfType<D>();
35+
database.DropCollection(collectionName);
36+
var document = new D { X = 1 };
37+
38+
ofTypeCollection.InsertOne(document);
39+
40+
var insertedDocuments = collection.FindSync("{}").ToList();
41+
insertedDocuments.Count.Should().Be(1);
42+
insertedDocuments[0].Should().BeOfType<D>();
43+
var insertedDocument = (D)insertedDocuments[0];
44+
insertedDocument.Id.Should().NotBeEmpty();
45+
insertedDocument.X.Should().Be(1);
46+
}
47+
48+
// nested types
49+
public class C
50+
{
51+
public Guid Id { get; set; }
52+
}
53+
54+
public class D : C
55+
{
56+
public int X { get; set; }
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)