Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize SimpleCollectionRepresentation fails (HAL+XML format only) #69

Closed
Sebastien34 opened this issue Sep 2, 2014 · 1 comment

Comments

@Sebastien34
Copy link

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;

        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

@panmanphil
Copy link
Collaborator

closing stale issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants