I am having an issue exposing a polymorphic collection using Web API. Here's a simple example of what I'm trying to do:
public class DemoController : ApiController
{
public HttpResponseMessage<List<TestParentClass>> TestPolymorphicList()
{
var response = new HttpResponseMessage<List<TestParentClass>>(HttpStatusCode.OK);
var content = new List<TestParentClass>
{
new TestParentClass
{
ParentClassField = "Parent Field A"
},
new TestChildClass1
{
ParentClassField = "Parent Field B",
ChildClassField = "Child Field B"
}
}
response.CreateContent(content);
return response;
}
}
public class TestParentClass
{
public string ParentClassField { get; set; }
}
public class TestChildClass
{
public string ChildClassField {get; set; }
}
When this action is invoked with application/json, it just returns null. If I take the TestChildClass object out of the list, I can return the parent just fine, but if I add any derived classes it falls back to returning null. I can attach and step through
the code to see that my method is executed correctly and no exceptions are thrown, and the content is set and returned from my method correctly.
At first I thought this was just a serialization problem. I understand that DataContractJsonSerializer is the default serializer for application/json, so I tried serializing my content manually. I added a KnownType attribute to my TestParentClass, then used
WriteObject to serialize my list to a memory stream. I was able to serialize it manually with no issues at all.
Has anyone run across similar issues? Is this a known limitation of Web API, or is there a good way to work around it? My next idea is to try using Json.NET as the serializer just to rule out serialization issues entirely, but other than that I'm not sure
what to try.
You seem to be using beta since HttpResponseMessage<T> is no more.
There are so many changes since then that your code will not compile at all, on the other hand with a bit of change the issue you are experiencing will go away as the defualt formatter for JSON is the one using Json.NET which provides better features.
So better download nightly build and use them.
Marked as answer by Tim Copenhaver on May 22, 2012 02:06 PM
Thanks for that tip. It took a little bit of time to figure out all the changes to HttpRequest/Response and work out other random method and property name changes, but it looks like it worked. With the current nightly build, this issue doesn't happen.
Tim Copenhav...
Member
3 Points
8 Posts
Polymorphism Issue
May 21, 2012 09:38 PM|LINK
I am having an issue exposing a polymorphic collection using Web API. Here's a simple example of what I'm trying to do:
public class DemoController : ApiController { public HttpResponseMessage<List<TestParentClass>> TestPolymorphicList() { var response = new HttpResponseMessage<List<TestParentClass>>(HttpStatusCode.OK); var content = new List<TestParentClass> { new TestParentClass { ParentClassField = "Parent Field A" }, new TestChildClass1 { ParentClassField = "Parent Field B", ChildClassField = "Child Field B" } } response.CreateContent(content); return response; } } public class TestParentClass { public string ParentClassField { get; set; } } public class TestChildClass { public string ChildClassField {get; set; } }When this action is invoked with application/json, it just returns null. If I take the TestChildClass object out of the list, I can return the parent just fine, but if I add any derived classes it falls back to returning null. I can attach and step through the code to see that my method is executed correctly and no exceptions are thrown, and the content is set and returned from my method correctly.
At first I thought this was just a serialization problem. I understand that DataContractJsonSerializer is the default serializer for application/json, so I tried serializing my content manually. I added a KnownType attribute to my TestParentClass, then used WriteObject to serialize my list to a memory stream. I was able to serialize it manually with no issues at all.
Has anyone run across similar issues? Is this a known limitation of Web API, or is there a good way to work around it? My next idea is to try using Json.NET as the serializer just to rule out serialization issues entirely, but other than that I'm not sure what to try.
aliostad
Member
228 Points
55 Posts
Re: Polymorphism Issue
May 21, 2012 10:19 PM|LINK
You seem to be using beta since HttpResponseMessage<T> is no more.
There are so many changes since then that your code will not compile at all, on the other hand with a bit of change the issue you are experiencing will go away as the defualt formatter for JSON is the one using Json.NET which provides better features.
So better download nightly build and use them.
Tim Copenhav...
Member
3 Points
8 Posts
Re: Polymorphism Issue
May 22, 2012 02:06 PM|LINK
Thanks for that tip. It took a little bit of time to figure out all the changes to HttpRequest/Response and work out other random method and property name changes, but it looks like it worked. With the current nightly build, this issue doesn't happen.