A DataListItemAttribute with custom value can sometime not deserialize correctly
See original GitHub issueWhich Contentment version are you using?
3.0.1
Which Umbraco version are you using? For example: 8.14.1 - don’t just write v8
9.1.2
Bug summary
When I have an enum with DataListItem attributes, if one of the attributes is a number it will use the enum value whose zero-based position in the enum matches that number rather than whose DataListItem attribute matches that number, when I access it as IPublishedContent.
Steps to reproduce
- Create an Enum, such as this:
public enum CustomEnum
{
[DataListItem(Value = "1")]
First,
[DataListItem(Value = "2")]
Second
}
- Create a datatype with a Contentment dropdown using the CustomEnum as the source
- Add that datatype to a document type
- Display the selected enum value in the template for the doc type
- Create a document using that document type, and select
First
as the value from the dropdown list - Navigate to the page for that document, and see that the selected value is displayed as
Second
Expected result / actual result
I would expect that when I select First from the Umbraco back-office, it would display First when I show the value on the front-end.
This appears to result from the IPublishedContent.Value call executing the property value converter, which ends up at https://github.com/leekelleher/umbraco-contentment/blob/master/src/Umbraco.Community.Contentment/DataEditors/DataList/DataSources/EnumDataListSource.cs#L188.
The Enum.Parse call at that line won’t just check the name of the value, but will also check the integer value of the enum against the contents of the string. To use this as a workaround, you have to manually assign integer values to all values in your enum, and as some of my enums have over 2,000 values that can become quite onerous. I believe the code fix would be to simply move line 188 referenced above down below the foreach
block that ends at line 200.
Do you have Umbraco ModelsBuilder enabled?
- Yes, it is enabled.
What browsers are you seeing the problem on?
Firefox, Chrome, Safari, Microsoft Edge
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
I got lots of good data, at the bottom, but the TLDR is that Enum.Parse is MUCH faster than the current loop, especially when you start working with large enums with lots of values. There is a better way though, as it seems that a dictionary lookup is 5-20x faster than Enum.Parse! With that in mind, I would propose changing the ConvertValue method as follows:
Here are the test results for reference. Let me know if you are interested in the code I used, and I can share that as well.
@benjaminc Just to let you know that I’ve implemented a dictionary/cache lookup for the Enum data-source, (see commit 361ff9a28a39795e263a0bcd75d78bb1ba07f96c). The code is different to what you offered as I’ve incorporated the
GetItems
in there too, since it was already iterating over the enum fields.This will be part of the next release, should be v3.2.0 - no timeframe on that just yet, soon-ish though.