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 causes InvalidCastException when used with OmitOnRecursionBehavior #1341

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

Comments

@ArturDorochowicz
Copy link

Describe the Bug

When ReadonlyCollectionPropertiesBehavior and OmitOnRecursionBehavior are used together, there may be an InvalidCastException thrown from CreateMany call in ReadonlyCollectionPropertiesCommand

InvalidCastException
Unable to cast object of type 'AutoFixture.Kernel.OmitSpecimen' to type 'System.Collections.Generic.IEnumerable1[System.Object]'.

var valuesToAdd = SpecimenFactory.CreateMany(
context,
addMethod.Parameters.Single().ParameterType);

Scenario

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

#nullable enable

public class Bugs
{

public class RecursionModel
{
    public RecursionItem? Item { get; set; }
}

public class RecursionItem
{
    public List<RecursionItem> Items { get; } = new();
}

    [Fact]
    public void OmitRecursion()
    {
        var fixture = new Fixture();
        fixture.Behaviors.Add(new ReadonlyCollectionPropertiesBehavior());
        fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
            .ForEach(b => fixture.Behaviors.Remove(b));
        fixture.Behaviors.Add(new OmitOnRecursionBehavior());
 
        // Throws
        fixture.Create<RecursionModel>();
    }
}
/*
System.InvalidCastException
Unable to cast object of type 'AutoFixture.Kernel.OmitSpecimen' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'.
   at AutoFixture.SpecimenFactory.CreateMany(ISpecimenContext context, Type type)
   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. The first level collection should probably be empty

More Information

Using AutoFixture 4.17

@aivascu aivascu added the triage Something that's being investigated label May 25, 2022
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