Basaingeal / Razor.SweetAlert2

A Razor class library for interacting with SweetAlert2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FireAsync with SweetAlertOptions not working with custom JsRuntime serialization

Gambero81 opened this issue · comments

Hi,

if i call Swal.FireAsync(title, message, icon) all work well

but if i use the SweetAlertOptions parameter it show an empty dialog and chrome dev tools give me below warnings.

var settings = new SweetAlertOptions
            {
                Text = message,
                Title = title,
                ConfirmButtonText = confirmButtonText,
                CancelButtonText = cancelButtonText,
                ShowCancelButton = true,
                Icon = SweetAlertIcon.Warning
            };

            var result = await Swal.FireAsync(settings);

image

the cause of this is the js runtime serialization settings who i have modified keeping the properties case as below:

public static IServiceProvider ConfigureJsRuntimeSerialization(this IServiceProvider services)
        {
            try
            {
                //https://github.com/dotnet/aspnetcore/issues/12685
                //Keep property case in serialization with jsRuntime.InvokeAsync
                var jsRuntime = services.GetService<IJSRuntime>();
                var prop = typeof(JSRuntime).GetProperty("JsonSerializerOptions", BindingFlags.NonPublic | BindingFlags.Instance);
                var value = (JsonSerializerOptions)Convert.ChangeType(prop.GetValue(jsRuntime, null), typeof(JsonSerializerOptions));
                value.PropertyNamingPolicy = null;
                value.DictionaryKeyPolicy = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ConfigureJsRuntimeSerialization Error: {ex.Message}");
            }

            return services;
        }

can sweetAlert2 keep compatibility with this settings?

i think it will require only to set the [JsonPropertyName("lowerCase")] in the SweetAlertOptionPOCO class properties

Thanks

You're right about the cause and fix. I didn't anticipate a user changing the serialization settings. This will be included in the next version in the next few days.

Hi @Basaingeal, any news on this issue? when will be fixed?

Thanks

Hoping to have it out today. The sweetalert2 library has some major changes coming that was keeping me busy, but this should be out in a few hours.