You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I'm using WebApi.Hal in both server and client sides of my API.
On the client side, when deserializing SimpleListRepresentation<> in 'HAL+XML', ResourceList collection remains empty. This works fine with HAL+JSON.
I drilled down into the source code to try to spot in the problem and discovered that the issue is located in XmlHalMEdiaTypeFormatter.cs / object ReadHalResource(Type type, XElement xml) method when dealing with collections.
First issue :
the test
if (typeof(IRepresentationList).IsAssignableFrom(type))
is never true when the resource is of type SimpleListRepresentation.
Second issue :
the line
var genericTypeArg = type.GetGenericArguments().Single();
throws an exception because the class that inherits from SimpleListCollection<> isn't itself generic. The generic type should be infered from the base class.
if (xml == null)
{
return null;
}
// First, determine if Resource of type Generic List and construct Instance with respective Parameters
//if (typeof(IRepresentationList).IsAssignableFrom(type))
if (type.BaseType.Name == "SimpleListRepresentation`1") //patch : SimpleListRepresentation isn't an IRepresentationList. My patch is ugly there must be a smarter way to do this check
{
var resourceListXml = xml.Elements("resource"); // .Where(x => x.Attribute("rel").Value == "item");
//var genericTypeArg = type.GetGenericArguments().Single();
var genericTypeArg = type.BaseType.GetGenericArguments().Single(); //patch : use the base class to infer generic argument
var resourceList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(genericTypeArg));
foreach (var resourceListItem in resourceListXml.Select(resourceXml => ReadHalResource(genericTypeArg, resourceXml)))
{
resourceList.Add(resourceListItem);
}
As mentioned, I'm not happy with the check, there must be a cleaner way to achieve that, but it works...
Hope this helps and thanks for you work.
Regards
/Sebastien
The text was updated successfully, but these errors were encountered:
Hi,
I'm using WebApi.Hal in both server and client sides of my API.
On the client side, when deserializing SimpleListRepresentation<> in 'HAL+XML', ResourceList collection remains empty. This works fine with HAL+JSON.
I drilled down into the source code to try to spot in the problem and discovered that the issue is located in XmlHalMEdiaTypeFormatter.cs / object ReadHalResource(Type type, XElement xml) method when dealing with collections.
First issue :
the test
if (typeof(IRepresentationList).IsAssignableFrom(type))
is never true when the resource is of type SimpleListRepresentation.
Second issue :
the line
var genericTypeArg = type.GetGenericArguments().Single();
throws an exception because the class that inherits from SimpleListCollection<> isn't itself generic. The generic type should be infered from the base class.
Proposed patch (XmlHalMediaTypeFormatter.cs line 66 - 2 lines changed):
static object ReadHalResource(Type type, XElement xml)
{
Representation representation;
As mentioned, I'm not happy with the check, there must be a cleaner way to achieve that, but it works...
Hope this helps and thanks for you work.
Regards
/Sebastien
The text was updated successfully, but these errors were encountered: