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

TryParseGenericTypeName error in non generic type

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.19.0
    • Component/s: Serialization
    • None

      Prerequisite

      I have the following document:

      {
          "State": {
              "Date": null,
              "Time": null,
              "Filters": {
                  "_t": "System.Collections.Generic.List`1[[JobManager.Common.TimeFilter, JobManager.Common]]",
                  "_v": [
                  {
                      "Type": 2,
                      "Values": [
                      6
                      ],
                      "Mode": 1
                  }
                  ]
              }
          }
      }
      

      *irrelevant parts omitted

      Mapped to the following c# Type

      public class ObjectDescriptor
      {
          public string? Id { get; set; }
          public string? TypeName { get; set; }
          public ObjectState? State { get; set; }
      }
      

      where ObjectState

       public class ObjectState : Dictionary<string, object?>
      

      The actual instance of the dictionary contains a key called Filters ,
      with a value of type  List<TimeFilter>

      The class TimeFilter

      public class TimeFilter
      {
          public TimeFilterType Type { get; set; }   //Enum     
          public IList<int>? Values { get; set; }        
          public TimeFilterMode Mode { get; set; } //Enum
      }
      

      Problem

      The method GetActualType in class TypeNameDiscriminator calls {{TryParseGenericTypeName }}passing the following typeName:

      System.Collections.Generic.List`1[[JobManager.Common.TimeFilter, JobManager.Common]]
      

      At first call, returns as typeArgumentNames the following string:

      [JobManager.Common.TimeFilter, JobManager.Common]

      The subsequential recursive call to GetActualType with that argument as typeName,
      results in a wrong match in TryParseGenericTypeName (wrongly parsed as generic),
      returning an empty string ("") in typeArgumentNames

      The empty string causes an exception when is called:

      Type.GetType(typeName);
      

      The exception is:

      String cannot have zero length
      

      Framework: .NET 7.0.200-preview.22628.1
      MongoDB.Driver: 2.19.0

            Assignee:
            boris.dogadov@mongodb.com Boris Dogadov
            Reporter:
            maillist@eusoft.net Andrea Guerrieri
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: