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

We're no longer able to use dynamic as property types

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.19.0
    • Component/s: Serialization
    • Labels:
      None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Hi

      We're having problems with the `ObjectSerializer` for the MongoDB.Driver.Core driver version `2.19.0`.

      We're storing "fingerprints" of objects where we use dynamic to save the properties used to calculate what we call a "fingerprint"-hash.

      We have the following class
      ```c#
      public class Fingerprint
      {
      [BsonElement("id"), BsonId, BsonIgnoreIfDefault, BsonRepresentation(BsonType.ObjectId)]
      public string Id

      { get; set; }
      [BsonElement("createdAt"), BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
      public DateTime CreatedAt { get; set; }

      = DateTime.Now;
      [BsonElement("hash")]
      public string Hash

      { get; set; }
      [BsonElement("data")]
      public object Data { get; set; }

      [BsonElement("metadata")]
      public object Metadata

      { get; set; }

      }
      ```

      We're then creating an instance of this object that populate our dynamic fields with C# base types (`int`, `long`, `boolean` etc.)

      ```c#
      public Models.Fingerprint ToFingerprint => new Models.Fingerprint
      {
      Hash = Fingerprint,
      Data = new
      {
      Data.UserId,
      Data.Name,
      Data.Type,
      Data.Domain,
      Data.SiteName,
      Data.Role,
      Camps = new

      { Count = CampsData.CampCount, NotInvoiced = CampsData.NotInvoicedCount }

      ,
      Club = new

      { ClubData.Id, ClubData.Name, ClubData.AttributeCount, ClubData.AttributeCategoryCount, ClubData.GroupsInClubCount, ClubData.HasActivatedBilling, ClubData.HasActivatedCriminalRecordSettings, ClubData.HasActivatedLokIntegration, ClubData.HasActivatedMembershipCards, ClubData.HasClubLogo, ClubData.HasFacebookConnection, ClubData.KakserviceSaleCreatedAt, ClubData.MembershipInvoicesCount, ClubData.MissingCriminalRecordCount, ClubData.NewbodySaleCreatedAt, ClubData.PersonsCount, ClubData.YoucalAgreementStatus }

      ,
      Package = new

      { Packages.Type, Packages.Package }

      },
      Metadata = new

      { Data.Id }

      };
      ```

      When using Newtonsoft we get the following result

      ```json
      {
          "id": null,
          "createdAt": "2023-02-17T18:06:40.3767748+01:00",
          "hash": "E929580C21FD36E878CCA03E38D95C57CD1DB7507A55965DFD036C2F9BFFE2AF821C616485EDAA7CF68A48E66A153F9A688829E692F440C78B56F7377AC37F5E",
          "data": {
              "userId": 1024439,
              "name": "Eds FF",
              "type": 3,
              "domain": "laget.se",
              "siteName": "EdsFF",
              "role": 1,
              "camps":

      {             "count": 0,             "notInvoiced": 0         }

      ,
              "club":

      {             "id": 22633,             "name": "Eds FF",             "attributeCount": 0,             "attributeCategoryCount": 0,             "groupsInClubCount": 44,             "hasActivatedBilling": true,             "hasActivatedCriminalRecordSettings": false,             "hasActivatedLokIntegration": true,             "hasActivatedMembershipCards": true,             "hasClubLogo": true,             "hasFacebookConnection": true,             "kakserviceSaleCreatedAt": null,             "membershipInvoicesCount": 1878,             "missingCriminalRecordCount": 73,             "newbodySaleCreatedAt": null,             "personsCount": 1010,             "youcalAgreementStatus": 2         }

      ,
              "package": {
                  "type": 0,
                  "package":

      {                 "siteId": 61958,                 "siteType": 3,                 "packageType": 3,                 "daysLeft": 4930             }

              }
          },
          "metadata":

      {         "id": 61958     }

      }
      ```

      Why can't we longer use `dynamic` as a propety type?

      Kind Regards
      Erik

            Assignee:
            boris.dogadov@mongodb.com Boris Dogadov
            Reporter:
            erik.hennerfors@laget.se Erik Hennerfors
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: