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

Read only properties do not respect custom conventions

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 2.5
    • Component/s: Serialization
    • None
    • Environment:
      netstandard 2.0

      Imagine a class to store in a collection :

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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 :

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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 :

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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:

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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.

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

              Created:
              Updated:
              Resolved: