-
Type: Bug
-
Resolution: Works as Designed
-
Priority: 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.