stoiveyp / Slack.NetStandard

.NET Core package that helps with Slack interactions
MIT License
41 stars 16 forks source link

Cannot Create Option Group from Option Group JSON #46

Closed GaryMcD closed 3 years ago

GaryMcD commented 3 years ago

Discovered this while receiving a view response for a view that had been made with an option group, it continued to fail until I dug in to find which object was causing Newtonsoft to throw an exception. Pasted my code example below of how to cause it. This seems to be caused by string.IsNullOrWhiteSpace(jObject.Value<string>("label")) within the OptionConverter

using Slack.NetStandard.Messages.Elements;
using Slack.NetStandard;
using System.Collections.Generic;
using Newtonsoft.Json;
using System;

namespace ExampleFailure
{
    class Program
    {
        static void Main(string[] args)
        {
            OptionGroup ExampleOptionGroup = new OptionGroup { Label = new PlainText { Text = "Example OptionGroup", Emoji = true }, Options = new List<Option>() };

            string ExampleOptionGroupAsString = JsonConvert.SerializeObject(ExampleOptionGroup);

            var Works = JsonConvert.DeserializeObject(ExampleOptionGroupAsString);
            OptionGroup Fails = JsonConvert.DeserializeObject<OptionGroup>(ExampleOptionGroupAsString);

            Console.ReadLine();
        }
    }
}
GaryMcD commented 3 years ago
var target = string.IsNullOrWhiteSpace(jObject.Value<string>("label")) ? (IOption)new Option() : new OptionGroup();

changed to

var target = jObject.ContainsKey("label") ? new OptionGroup() : (IOption)new Option();

should solve.

stoiveyp commented 3 years ago

Thanks for raising this @GaryMcD - this has been corrected in 2.15.1 👍