microsoft / typechat.net

MIT License
189 stars 26 forks source link

IEnumerable<T> not handled #218

Open guyrt opened 5 months ago

guyrt commented 5 months ago

Common practice is to define a class like this

public sealed class Pizza
{
   public IEnumerable<string> Toppings { get; set; }

   public string Size {get; set;}
}

Typechat.net will fail on the IEnumerable with the argument System.ArgumentException: 'IEnumerable`1 must be a class'. JSON serdes in .net know to model the IEnumerable as a JSON array. Can typechat do the same?

guyrt commented 5 months ago

If you swap to a List, you get an unnecessary interface that doesn't appear in a string[]:

interface Pizza {
  Toppings: List_String;
  Size: string;
}
interface List_String {
  Capacity: number;
  Count: number;
  Item?: string;
}

I would think the ideal implementation for all IEnumerables that naturally translate to a JSON array is to return something like T[] where T is defined inline for simple types.