[CSHARP-3686] Enums with underlying value as 0 does not save Created: 25/May/21 Updated: 27/Oct/23 Resolved: 03/Jun/21 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Serialization |
| Affects Version/s: | 2.12.3 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | João Vitor Pina | Assignee: | James Kovacs |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
When I use the IgnoreIfDefaultConvention, enums that starts at 0 does not save the first element of the enum. When I change the starting number to 1, it starts saving again normally. I think that when it goes to check the convention, it checks the number and mark as default because integers default is 0 in C#, and the convention make the driver ignore it, but it is wrong because it's not a default value for Enum, it has a value, it's just the underlying value that is the same as the default for integer.
Example public class Model() { public LogType Type \{ get; set; }}
|
| Comments |
| Comment by James Kovacs [ 03/Jun/21 ] | |||||||||||
|
Hi tecnologia@rissicontabilidade.com.br, The IgnoreIfDefaultConvention instructs the .NET/C# driver to not save default values to the database. The default value for an enum is the zero value. For instance consider the following C# program:
The output of this program is the string Info. If we change the LogType enum to start at 1, then the output of this program is 0 (zero) because there is no corresponding enum string defined for zero. Zero is the default value for any enum type. Thus when you use the IgnoreIfDefaultConvention, zero values for enums are not saved to the database. If you want to save zero values for enums, you can modify your type filter when registering the IgnoreIfDefaultConvention.
Alternately you can write your own convention that doesn't ignore zero values for enums. You could write this convention as a class map convention or a member map convention depending on your needs. See Custom Conventions in our documentation for more information and examples. Lastly you can customize your BsonClassMap for any type to override the IgnoreIfDefaultConvention similar to the following:
Please let us know if you have any additional questions. Sincerely, | |||||||||||
| Comment by Mikalai Mazurenka (Inactive) [ 28/May/21 ] | |||||||||||
|
Hi tecnologia@rissicontabilidade.com.br Thanks for reporting this issue! We need some time to investigate it and will come back to you. |