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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 ReadonlyCollectionPropertiesBehavior does not account for collection property being null #1340

Open
ArturDorochowicz opened this issue Apr 8, 2022 · 0 comments
Labels
bug triage Something that's being investigated
Milestone

Comments

@ArturDorochowicz
Copy link

Describe the Bug

ReadonlyCollectionPropertiesBehavior does not account for the collection property being null.

The code in ReadonlyCollectionPropertiesCommand retrieves the value of the collection property and immediately passes it to the InstanceMethodQuery constructor which throws ArgumentNullException when the value is null.

foreach (var pi in this.PropertyQuery.SelectProperties(specimenType))
{
var addMethod = new InstanceMethodQuery(pi.GetValue(specimen), nameof(ICollection<object>.Add))
.SelectMethods()
.SingleOrDefault();
if (addMethod == null) continue;

Scenario

using System.Collections.Generic;
using System.Linq;
using AutoFixture;
using Xunit;

#nullable enable

public class Bugs
{
    class NullCollectionModel
    {
        public Dictionary<int, int> Items { get; } = default!;
    }

    [Fact]
    public void NullCollection()
    {
        var fixture = new Fixture();
        fixture.Behaviors.Add(new ReadonlyCollectionPropertiesBehavior());

        // Throws
        fixture.Create<NullCollectionModel>();
    }
}
/*
System.ArgumentNullException
Value cannot be null. (Parameter 'owner')
   at AutoFixture.Kernel.ReadonlyCollectionPropertiesCommand.Execute(Object specimen, ISpecimenContext context)
   at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at AutoFixture.Kernel.SeedIgnoringRelay.Create(Object request, ISpecimenContext context)
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
*/

Expected Behavior

The code above should not throw.

More Information

Using AutoFixture 4.17

@aivascu aivascu added the triage Something that's being investigated label May 25, 2022
@aivascu aivascu added this to the v4.0 milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug triage Something that's being investigated
Projects
None yet
Development

No branches or pull requests

2 participants