I have a Web Api project that has the following class definitions:
public abstract class Organisation
{}
public class Agency : Organisation
{}
public class Company : Organisation
{}
And I have an action defined as the following:
[Route("api/organisation")]
[HttpGet]
[ResponseType(typeof(ICollection<Organisation>))]
public IHttpActionResult GetAll()
{}
I then generate the Swagger definition and import it into my project using VS -> Add REST Api Client.
Using the definition file out of the box it generates the different classes as separate classes, no inheritance between them. The method is defined as return just a list of Organisations
which obviously isn't correct as they could be an Agency or Company.
Based on this previous question I've added the code and now the Swashbuckle definition generates the classes correctly (Agency and Company use Organisation as a base class).
However, the response is still deserialised to an Organistion. I tried including the type in the serialisation using GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
so that it knew what type it was but that resulted in the error
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
Any ideas how the client can know what type of Organisation the response is?
Thanks