[CSHARP-2353] BsonDocument.DeepClone() fails on documents with duplicate element names Created: 09/Aug/18  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.7.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Adam Milazzo Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Backwards Compatibility: Minor Change

 Description   

The following code fails:

var doc = new BsonDocument() { AllowDuplicateNames = true };

doc.Add("a", 1);

doc.Add("a", 2);

var doc2 = doc.DeepClone();

 

This is because DeepClone does:

{{ BsonDocument clone = new BsonDocument();}}
{{ foreach (BsonElement element in _elements)}}
{{ {}}
  clone.Add(element.DeepClone());
{{ }}}
{{ return clone;}}

Changing the first line to:

BsonDocument clone = new BsonDocument() { AllowDuplicateNames = AllowDuplicateNames };

fixes the problem.

 



 Comments   
Comment by Adam Milazzo [ 09/Aug/18 ]

Created pull request: https://github.com/mongodb/mongo-csharp-driver/pull/338

Generated at Wed Feb 07 21:42:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.