[CSHARP-2354] BsonDocument constructor fails to copy documents with duplicate element names Created: 09/Aug/18 Updated: 31/Mar/22 |
|
| Status: | Backlog |
| Project: | C# Driver |
| Component/s: | BSON |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Adam Milazzo | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Backwards Compatibility: | Minor Change |
| Description |
|
The following code fails: var doc = new BsonDocument() { AllowDuplicateNames = true }; doc.Add("x", 1); doc.Add("x", 2); var doc2 = new BsonDocument(doc); // throws
This is because the parameter is treated as an IEnumerable<BsonElement>. This can be fixed by adding a new constructor: /// <summary> public BsonDocument(BsonDocument document) _allowDuplicateNames = document.AllowDuplicateNames;
This could be ambiguous if somebody tried to pass a RawBsonDocument or LazyBsonDocument, so two additional constructors may be useful: /// <summary> public BsonDocument(LazyBsonDocument document) : this((BsonDocument)document) { } /// <summary> public BsonDocument(RawBsonDocument document) : this((BsonDocument)document) { } |
| Comments |
| Comment by Adam Milazzo [ 09/Aug/18 ] |
|
Created pull request: https://github.com/mongodb/mongo-csharp-driver/pull/338 |
| Comment by Adam Milazzo [ 09/Aug/18 ] |
|
Also necessary is to add this line to the RawBsonDocument and LazyBsonDocument constructors: AllowDuplicateNames = true; // raw BSON always supports duplicate names |