Geta / geta-optimizely-genericlinks

An extensive alternative to LinkItemCollection in Optimizely.
Apache License 2.0
4 stars 0 forks source link

GetAttribute throws exception when passing Enum.Parse<Type> #21

Closed jevgenijsp closed 1 year ago

jevgenijsp commented 1 year ago

After updating to v 1.6.1, I got an exception in the custom LinkData model, the previous version was ok with that.

Custom LinkData model:

public class ButtonLinkData : LinkData
{
    [Display(Name = "Font Awesome Icon", Order = 1)]
    [FontAwesomeIcon]
    public virtual string? Icon
    {
        get => GetAttribute(v => v);
        set => SetAttribute(value, v => v);
    }

    [Display(Name = "Icon alignment", Order = 2)]
    [LocalizedSelectOneEnum(typeof(IconAlign))]
    public virtual IconAlign IconAlign
    {
        get => GetAttribute(Enum.Parse<IconAlign>);
        set => SetAttribute(value, v => v.ToString());
    }

    [Display(Name = "Button action", Order = 250)]
    [LocalizedSelectOneEnum(typeof(ButtonAction))]
    [ScaffoldColumn(false)] // Hidden until implemented properly. Should we support OpenModal action?
    public virtual ButtonAction ButtonAction
    {
        get => GetAttribute(Enum.Parse<ButtonAction>); << EXCEPTION THROWS HERE
        set => SetAttribute(value, v => v.ToString());
    }

    [Display(Name = "Button style", Order = 260)]
    [LocalizedSelectOneEnum(typeof(ButtonStyle))]
    public virtual ButtonStyle ButtonStyle
    {
        get => GetAttribute(Enum.Parse<ButtonStyle>);
        set => SetAttribute(value, v => v.ToString());
    }

    [Display(Name = "Button size", Order = 270)]
    [LocalizedSelectOneEnum(typeof(ButtonSize))]
    public virtual ButtonSize ButtonSize
    {
        get => GetAttribute(Enum.Parse<ButtonSize>);
        set => SetAttribute(value, v => v.ToString());
    }
}

Exception:

  An unhandled exception has occurred while executing the request.
      System.ArgumentException: Must specify valid information for parsing in the string. (Parameter 'value')
         at System.Enum.TryParse[TEnum](ReadOnlySpan`1 value, Boolean ignoreCase, Boolean throwOnFailure, TEnum& result)
         at System.Enum.TryParse[TEnum](String value, Boolean ignoreCase, Boolean throwOnFailure, TEnum& result)
         at System.Enum.Parse[TEnum](String value, Boolean ignoreCase)
         at System.Enum.Parse[TEnum](String value)
         at Geta.Optimizely.GenericLinks.LinkData.GetAttribute[T](Func`2 conversion, String key)
         at Geta.Phoenix.Repositories.Infrastructure.Cms.ButtonLinkData.get_ButtonAction() in C:\Projects\geta-paragon\sub\geta-phoenix\src\Geta.Phoenix.Repositories\Infrastructure\Cms\ButtonLinkData.cs:line 32

Exception throws on ButtonAction property when calling GetAttribute. Has something changed on the GetAttribute level? Or should we do some code updates?

svenrog commented 1 year ago

The null value handling was changed in 1.5.0, it's probably related to that. I'll try to reproduce this and fix it.

svenrog commented 1 year ago

I've added some default value handling to exit out before that exception is thrown in 1.7.0. Should be available on https://nuget.optimizely.com/ shortly.

jevgenijsp commented 1 year ago

Issue resolved.