nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
431 stars 50 forks source link

System.InvalidOperationException when class inherits from Dictionary #37

Closed sdbrannum closed 4 years ago

sdbrannum commented 4 years ago

It seems AutoBogus throws an exception when trying to generate a class that inherits from a typed Dictionary. Should this work or will I need to use the AutoGeneratorOverride?

Reproduction

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var faker = new AutoBogus.AutoFaker<Bar>();
        var bars = faker.Generate(10);

        foreach(var bar in bars) 
        {
            Console.WriteLine(bar.Id);
        }
    }

    public class Bar 
    {
        public Guid Id {get;set;}
        public Foo Properties {get;set;}
    }

    public class Foo : Dictionary<Guid, string>
    {
        public Foo() {}
        public Foo(IDictionary<Guid, string> dictionary) : base(dictionary) {}
    }
}

Stack Trace:

Unhandled exception. System.InvalidOperationException: Sequence contains more than one element
   at System.Linq.ThrowHelper.ThrowMoreThanOneElementException()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at AutoBogus.AutoGeneratorFactory.ResolveGenerator(AutoGenerateContext context)
   at AutoBogus.AutoGeneratorFactory.GetGenerator(AutoGenerateContext context)
   at AutoBogus.AutoBinder.PopulateInstance[TType](Object instance, AutoGenerateContext context, IEnumerable`1 members)
   at AutoBogus.Generators.TypeGenerator`1.AutoBogus.IAutoGenerator.Generate(AutoGenerateContext context)
   at AutoBogus.AutoBinder.PopulateInstance[TType](Object instance, AutoGenerateContext context, IEnumerable`1 members)
   at AutoBogus.Generators.TypeGenerator`1.AutoBogus.IAutoGenerator.Generate(AutoGenerateContext context)
   at AutoBogus.AutoFaker`1.<>c__DisplayClass28_0.<PrepareCreate>b__0(Faker faker)
   at Bogus.Faker`1.Generate(String ruleSets) in C:\projects\bogus\Source\Bogus\Faker[T].cs:line 458
   at AutoBogus.AutoFaker`1.Generate(String ruleSets)
   at Bogus.Faker`1.<>c__DisplayClass49_0.<Generate>b__0(Int32 i) in C:\projects\bogus\Source\Bogus\Faker[T].cs:line 480
   at System.Linq.Enumerable.SelectRangeIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Bogus.Faker`1.Generate(Int32 count, String ruleSets) in C:\projects\bogus\Source\Bogus\Faker[T].cs:line 479
   at AutoBogus.AutoFaker`1.Generate(Int32 count, String ruleSets)
   at Program.Main()
nickdodd79 commented 4 years ago

Hey @sdbrannum,

I have taken a look at this and it seems that inheriting from Dictionary is making AutoBogus attempt to populate all the inherited properties as well, but can't resolve some of the types. I have put a fix in place which I am testing and will let you know when the new version is published.

Nick.

sdbrannum commented 4 years ago

@nickdodd79 it looks like 2.9.0 resolved this issue. I appreciate the package and the quick turnaround!