Skip to content

CSHARP-5697: Fix Client.BulkWrite failure in case complex type is being used as Document's Id #1749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/MongoDB.Driver/BulkWriteInsertOneResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

using System;
using MongoDB.Bson;

namespace MongoDB.Driver
Expand All @@ -25,6 +26,19 @@ public class BulkWriteInsertOneResult
/// <summary>
/// The id of the inserted document.
/// </summary>
public BsonValue InsertedId { get; init; }
[Obsolete("InsertedId is deprecated and will be removed in future versions. Use DocumentId instead.")]
public BsonValue InsertedId
{
get => BsonValue.Create(DocumentId);
init
{
DocumentId = value;
}
}

/// <summary>
/// The id of the inserted document.
/// </summary>
public object DocumentId { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class ClientBulkWriteOperation : RetryableWriteCommandOperationB
{
private readonly bool? _bypassDocumentValidation;
private readonly bool _errorsOnly;
private readonly Dictionary<int, BsonValue> _idsMap = new();
private readonly Dictionary<int, object> _idsMap = new();
private readonly BsonDocument _let;
private readonly RenderArgs<BsonDocument> _renderArgs;
private readonly IBatchableSource<BulkWriteModel> _writeModels;
Expand Down Expand Up @@ -331,7 +331,7 @@ private void PopulateIndividualResponses(IEnumerable<BsonDocument> individualRes
_idsMap.TryGetValue(operationIndex, out var insertedId);
bulkWriteResult.InsertResults.Add(operationIndex, new()
{
InsertedId = insertedId
DocumentId = insertedId
});
}
else if (writeModelType == typeof(BulkWriteUpdateOneModel<>) || writeModelType == typeof(BulkWriteUpdateManyModel<>) || writeModelType == typeof(BulkWriteReplaceOneModel<>))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ internal sealed class ClientBulkWriteOpsCommandMessageSection : BatchableCommand
{
public ClientBulkWriteOpsCommandMessageSection(
IBatchableSource<BulkWriteModel> operations,
Dictionary<int, BsonValue> idsMap,
Dictionary<int, object> idsMap,
int? maxBatchCount,
int? maxDocumentSize,
RenderArgs<BsonDocument> renderArgs)
Expand All @@ -144,7 +144,7 @@ public ClientBulkWriteOpsCommandMessageSection(
RenderArgs = renderArgs;
}

public Dictionary<int, BsonValue> IdsMap { get; }
public Dictionary<int, object> IdsMap { get; }
public new IBatchableSource<BulkWriteModel> Documents { get; }
public RenderArgs<BsonDocument> RenderArgs { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal sealed class ClientBulkWriteOpsSectionFormatter : ICommandMessageSectio
private MemoryStream _nsInfoMemoryStream;
private BsonBinaryWriter _nsInfoWriter;
private IBsonSerializerRegistry _serializerRegistry;
private Dictionary<int, BsonValue> _idsMap;
private Dictionary<int, object> _idsMap;
private int _currentIndex;

public ClientBulkWriteOpsSectionFormatter(long? maxSize)
Expand Down Expand Up @@ -150,7 +150,7 @@ public void RenderInsertOne<TDocument>(RenderArgs<BsonDocument> renderArgs, Bson
WriteStartModel(serializationContext, "insert", model);
var documentSerializer = _serializerRegistry.GetSerializer<TDocument>();
var documentId = documentSerializer.SetDocumentIdIfMissing(null, model.Document);
_idsMap[_currentIndex] = BsonValue.Create(documentId);
_idsMap[_currentIndex] = documentId;
serializationContext.Writer.WriteName("document");
documentSerializer.Serialize(serializationContext, model.Document);
WriteEndModel(serializationContext);
Expand Down
106 changes: 106 additions & 0 deletions tests/MongoDB.Driver.Tests/Jira/CSharp5697Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* Copyright 2010-present MongoDB 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.Threading.Tasks;
using FluentAssertions;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.TestHelpers;
using MongoDB.TestHelpers.XunitExtensions;
using Xunit;

namespace MongoDB.Driver.Tests.Jira;

public class CSharp5697Tests : IntegrationTest<CSharp5697Tests.ClassFixture>
{
public CSharp5697Tests(ClassFixture fixture)
: base(fixture, server => server.Supports(Feature.ClientBulkWrite))
{
}

[Theory]
[ParameterAttributeData]
public async Task ClientBulkWrite_supports_complex_id([Values(true, false)] bool async)
{
var id = async ? "1" : "2";
var options = new ClientBulkWriteOptions { VerboseResult = true };
BulkWriteModel[] models =
[
new BulkWriteInsertOneModel<Document>(
Fixture.Collection.CollectionNamespace,
new Document(new DocumentId(id)))
];

var result = async ?
await Fixture.Client.BulkWriteAsync(models, options) :
Fixture.Client.BulkWrite(models, options);

result.InsertResults[0].DocumentId.Should().BeOfType<DocumentId>()
.Subject.Key.Should().Be(id);
}

public class Document
{
public Document(DocumentId id)
{
Id = id;
}

public DocumentId Id { get; }
}

public class DocumentId
{
public DocumentId(string key)
{
Key = key;
}

public string Key { get; }
}

public sealed class DocumentIdSerializer : IBsonSerializer<DocumentId>
{
public Type ValueType => typeof(DocumentId);

public DocumentId Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
=> new DocumentId(context.Reader.ReadString());

public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, DocumentId value)
=> context.Writer.WriteString(value.Key);

public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
{
var id = (DocumentId)value;
Serialize(context, args, id);
}

object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
=> Deserialize(context, args);
}

public class ClassFixture : MongoCollectionFixture<Document>
{
public ClassFixture()
{
BsonSerializer.RegisterSerializer( new DocumentIdSerializer());
}

protected override IEnumerable<Document> InitialData => null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static BsonDocument ConvertClientBulkWriteResult(ClientBulkWriteResult re
{ "deletedCount", (int)result.DeletedCount },
{
"insertResults", ConvertResults(result.InsertResults,
item => new() { { "insertedId", item.InsertedId } })
item => new() { { "insertedId", BsonValue.Create(item.DocumentId) } })
},
{
"updateResults", ConvertResults(result.UpdateResults,
Expand Down