(Enquiry) working discriminator element as a property in class

XMLWordPrintableJSON

    • None
    • Dotnet Drivers
    • 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?
    • None
    • None
    • None
    • None
    • None
    • None

      As reported in this StackOverflow question: MongoDB.Bson.BsonSerializationException after C# driver update to 3.5.0(https://stackoverflow.com/q/79780239/8017690),

      I believe this bug happened after the introduction of [Serialization ignores discriminator field when checking for duplicate field names](http://jira.mongodb.org/browse/CSHARP-4040).

       

      Here is how the Post owner code looks:

       
      public record IdentifierSheet
      {
      public required string Identifier

      { get; init; }
      public Security SecurityType { get; init; }
      public required string Symbol { get; init; }
      public Exchange Exchange { get; init; }
      public string? Name { get; init; }
      }

      public record FutureIdentifierSheet : IdentifierSheet
      {
      public string Underlying { get; init; }

      = string.Empty;

      public required DateTime ExpirationDate { get; init; }

      public required string ProductCode { get; init; }

      public required DateTime YearMonth { get; init; }
      }
      private class SecurityTypeDiscriminatorConvention : IDiscriminatorConvention
      {
      public string ElementName => nameof(IdentifierSheet.SecurityType);

      public Type GetActualType(IBsonReader bsonReader, Type nominalType)
      {
      var bookmark = bsonReader.GetBookmark();

      bsonReader.ReadStartDocument();
      var actualType = typeof(IdentifierSheet);

      while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
      {
      string? name = bsonReader.ReadName();
      if (name == ElementName)
      {
      var enumValue = (SecurityType.Security)(object)bsonReader.ReadInt32();
      actualType = enumValue switch

      { SecurityType.Security.Future => typeof(FutureIdentifierSheet), SecurityType.Security.Option => typeof(OptionIdentifierSheet), _ => actualType }

      ;
      break;
      }

      bsonReader.SkipValue();
      }

      bsonReader.ReturnToBookmark(bookmark);
      return actualType;
      }

      public BsonValue GetDiscriminator(Type nominalType, Type actualType)
      {
      if (actualType == typeof(FutureIdentifierSheet))
      return (int)SecurityType.Security.Future;
      if (actualType == typeof(OptionIdentifierSheet))
      return (int)SecurityType.Security.Option;

      return (int)SecurityType.Security.NoSecurity; // fallback or base
      }
      }
       

      My question is:

      1. Is it still possible to have the discriminator as a property in the class?
      2. If yes, please provide the workaround example to achieve.

       

      Thanks.

       

       

       

            Assignee:
            Ferdinando Papale
            Reporter:
            Kok Yong Shun
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: