Skip to content

If joining 1-* when the * contains 0 items NullRefs can be thrown #13

@lukemcgregor

Description

@lukemcgregor

If you use something like the following:

select * from People
left join PhoneNumbers on ...

If there are no phone numbers found you get back:

  1, Joe, Bloggs, null, null

This causes a null ref when Slapper tries to map it as the Id on the phone number is null.

This should produce an empty list

See test below.

public class PersonWithProperties
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<PhoneNumber> Numbers { get; set; }
}

public class PhoneNumber
{
    public int Id { get; set; }
    public string Number { get; set; }

}

[Test]
public void Can_Map_Empty_Lists()
{
    // Arrange
    const int id = 1;
    const string firstName = "Joe";
    const string lastName = "Bloggs";

    var dictionary = new Dictionary<string, object>
        {
            { "Id", id },
            { "FirstName", firstName },
            { "LastName", lastName },
            { "Numbers_Id", null },
            { "Numbers_Number", null }
        };

    // Act
    var customer = Slapper.AutoMapper.Map<PersonWithProperties>(dictionary);

    // Assert
    Assert.NotNull(customer);
    Assert.That(customer.Id == id);
    Assert.That(customer.FirstName == firstName);
    Assert.That(customer.LastName == lastName);
    Assert.NotNull(customer.Numbers);
    Assert.That(customer.Numbers.Count() == 0);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions