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

Unable to deserialize C# DateOnly objects

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 3.2.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Needed
    • Hide

      We added the DocumentFormat property to DateOnlySerializer, that allows to decide the format to use when serializing DateOnly with a BsonType.Document representation.
      DocumentFormat is of type DateOnlyDocumentFormat, an enum with two values:

      • DateTimeTicks: The document will contain "DateTime" (BsonType.DateTime) and "Ticks" (BsonType.Int64). This is the format that has been used since this serializer was added (version 3.0) and will be the default format used
      • YearMonthDay: The document will contain "Year", "Month" and "Day" (all BsonType.Int32). This is the format that was used in pre 3.0 versions (because DateOnlys were being serialized using the BsonClassMapSerializer). This is probably more human readable.

      It's possible to setup the format also using the new BsonDateOnlyOptionsAttribute attribute.

      Show
      We added the DocumentFormat property to DateOnlySerializer, that allows to decide the format to use when serializing DateOnly with a BsonType.Document representation. DocumentFormat is of type DateOnlyDocumentFormat, an enum with two values: DateTimeTicks: The document will contain "DateTime" (BsonType.DateTime) and "Ticks" (BsonType.Int64). This is the format that has been used since this serializer was added (version 3.0) and will be the default format used YearMonthDay: The document will contain "Year", "Month" and "Day" (all BsonType.Int32). This is the format that was used in pre 3.0 versions (because DateOnlys were being serialized using the BsonClassMapSerializer). This is probably more human readable. It's possible to setup the format also using the new BsonDateOnlyOptionsAttribute attribute.

      Summary

      When calling collection that contains a representation of a C# DateOnly struct the new v3 driver is unable to deserialize the response.

      The cluster is standalone and on version 7.0.14

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      How to Reproduce

      var filter = Builders<Employee>.Filter.Eq(e => e.Id, employeeId);
      var projection = Builders<Employee>.Projection
          .Include(e => e.Alias)
          .Include(e => e.FirstName)
          .Include(e => e.Surname)
          .Include(e => e.Qualifications);
      var employee = await _collection.Find(filter).Project<Employee>(projection).FirstOrDefaultAsync();

      The above code fails when calling .Find()

            Assignee:
            ferdinando.papale@mongodb.com Ferdinando Papale
            Reporter:
            joeraymond@gmail.com Joe Raymond
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: