[CSHARP-1723] BsonConstructorAttribute doesn't seem to behave as expected with named parameters Created: 25/Jul/16  Updated: 19/Oct/16  Resolved: 19/Oct/16

Status: Closed
Project: C# Driver
Component/s: BSON, Serialization
Affects Version/s: 2.2.4
Fix Version/s: 2.4

Type: Bug Priority: Minor - P4
Reporter: Tristan Kilani-Freeman [X] Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Documentation suggests here would suggest that it's possible to map a constructor's parameters to various members by specifying them in overloads of the BsonConstructorAttribute.

As far as I can tell, this attribute has only one overload, which takes a sequence of strings. Assuming that these strings are supposed to be the names of the members to map to, this does not appear to be working as intended.

The following class will not serialise:

public class Product
        {
            public int Id { get; private set; }
 
            [BsonIgnoreIfNull]
            public string Name { get; private set; }
 
            [BsonIgnoreIfNull]
            public int OtherField { get; private set; }
 
            [BsonConstructor("Id","Name")]
            public Product(int id, string val)
            {
                Id = id;
                Name = val;
            }
 
            [BsonConstructor("Id","OtherField")]
            public Product(int id, int val)
            {
                Id = id;
                OtherField = val;
            }
        }

The following which follows the default naming convention does work however:

public class Product
        {
            public int Id { get; private set; }
 
            [BsonIgnoreIfNull]
            public string Name { get; private set; }
 
            [BsonIgnoreIfNull]
            public int OtherField { get; private set; }
            
            [BsonConstructor]
            public Product(int id, string name)
            {
                Id = id;
                Name = name;
            }
            
            [BsonConstructor]
            public Product(int id, int otherField)
            {
                Id = id;
                OtherField = otherField;
            }
        }



 Comments   
Comment by Githook User [ 19/Oct/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1723: Improve error messages and add more tests.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/b11826da95ee3415f68a7337e8febaf7dbfc00af

Comment by Githook User [ 19/Oct/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1723: Fixed BsonConstructorAttribute with named parameters.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/843f075f58b4408b8558ab974a81f01ff3297895

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