NJsonApiCore icon indicating copy to clipboard operation
NJsonApiCore copied to clipboard

Fixed serialization bug

Open NicholasMiller opened this issue 8 years ago • 1 comments

Fixed an issue where serialization fails if a model contains multiple linked entities of the same type. Take the following simple example:

public class PersonModel
{
    public int Id { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
}

public class MusicLessonModel
{
    public DateTime Date { get; set; }
    public int TeacherPersonId  { get; set; }
    public PersonModel TeacherPerson { get; set; }
    public int StudentPersonId  { get; set; }
    public PersonModel StudentPerson { get; set; }
}

Assuming the appropriate controllers have been setup, calling http://hostname/music-lessons?include=person results in a 500 response prior to the code change in this pr.

NicholasMiller avatar Dec 29 '16 19:12 NicholasMiller

Thanks for the PR! Sorry for the slow reply, a baby has arrived in the house. There's definitely a bug in there but it's not the one you've fixed, so I'm afraid I'm going to reject the PR.

The line:

var relationship = currentMapping.Relationships.SingleOrDefault(x => x.RelatedBaseResourceType == part);

is incorrect because it is matching on the RelatedBaseResourceType. It should be matching on the Name of the resource. Using your example, the request should be:

GET http://hostname/music-lessons?include=teacherperson

As property names are unique, the SingleOrDefault should return a single linked resource.

brainwipe avatar Jan 19 '17 14:01 brainwipe