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

Read only properties do not respect custom conventions

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Critical - P2 Critical - P2
    • None
    • 2.5
    • Serialization
    • None
    • netstandard 2.0

    Description

      Imagine a class to store in a collection :

      enum MyEnum
      {
        Value1
      }
       
      class MyDoc
      {
        public string Id { get; set; }
        public MyEnum Prop { get; set; }
        public MyEnum Prop2
        {
          get { return MyEnum.Value1;  }
        }
      }
      

      Mapping is declared as it to allow serialization of read only property :

      BsonClassMap.RegisterClassMap<MyDoc>(cm =>
      {
        cm.AutoMap();
        cm.MapIdProperty(x => x.Id)
          .SetSerializer(new StringSerializer(BsonType.ObjectId))
          .SetIdGenerator(StringObjectIdGenerator.Instance);
        cm.MapMember(x => x.Prop2);
      });
      

      These conventions are added :

      var pack= new ConventionPack();
      pack.Add(new CamelCaseElementNameConvention());
      pack.Add(new EnumRepresentationConvention(BsonType.String));
      ConventionRegistry.Register("custom", pack, t => true);
      

      When this instance is inserted into the collection:

      var doc = new MyDoc
      {
        Prop = MyEnum.Value1
      }
      

      Then the resulting document in MongoDB is :

      {
        "_id":"5a7f07f1ac141637ec1ba0c9",
        "prop": "Value1",
        "Prop2": 0
      }
      

      The conventions are not respected for the read only property Prop2 :

      • its name is not camel case (first letter is upper case)
      • its value is stored with the enum value instead of enum key.

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            fredgate Frédéric Barrière
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: