Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1284

Projection to object with string Id member yields CreateCannotDeserializeFromBsonTypeException

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.0
    • Serialization
    • None

    Description

      This appears to occur with any projection built with Builders<T>.Projection.

      The model class has an Id member which is a string, but no annotation attributes, for example:

      public class SomeClass { public string Id

      {get;set;} public string Name {get;set;}

      }

      var result = _collection.FindOneAndUpdateAsync(filter, updateDef, new FindOneAndUpdateOptions
      {
      IsUpsert = true,
      Projection = Builders<T>.Projection.Expression(u => new

      { U.Id }

      ),
      ReturnDocument = ReturnDocument.Before
      }).Result;

      The exception is thrown in StringSerializer.DeserializeValue. In this case, the local variable bsonType is ObjectId, and _representation is String.

      Am I correct in thinking a check for this, followed by a call to bsonReader.ReadString(), would be the fix?

      If so, I'd like to take the opportunity to build a Pull Request, but am unsure of the testing/submission guidelines, since the page linked to in the docs either no longer exists or has moved from where GitHub points to. Could you advise please?

      Attachments

        Activity

          People

            craig.wilson@mongodb.com Craig Wilson
            kierenj Kieren Johnstone
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: