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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to generate a type with a delegate crashes dotnet #417

Open
hahn-kev opened this issue Dec 11, 2024 · 7 comments
Open

trying to generate a type with a delegate crashes dotnet #417

hahn-kev opened this issue Dec 11, 2024 · 7 comments

Comments

@hahn-kev
Copy link

I came across this when trying to generate a bunch of types
exception

Fatal error. Internal CLR error. (0x80131506)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(System.Object, System.Span`1<System.Object>, System.Reflection.BindingFlags)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at System.Reflection.RuntimeConstructorInfo.Invoke(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at System.Reflection.ConstructorInfo.Invoke(System.Object[])
   at Soenneker.Reflection.Cache.Constructors.CachedConstructor.Invoke(System.Object[])
   at Soenneker.Utils.AutoBogus.AutoFakerBinder.CreateInstance[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Soenneker.Utils.AutoBogus.Context.AutoFakerContext, Soenneker.Reflection.Cache.Types.CachedType)
   at Soenneker.Utils.AutoBogus.AutoFakerBinder.CreateInstanceWithRecursionGuard[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Soenneker.Utils.AutoBogus.Context.AutoFakerContext, Soenneker.Reflection.Cache.Types.CachedType)
   at Soenneker.Utils.AutoBogus.Generators.Types.TypeGenerator`1[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Soenneker.Utils.AutoBogus.Generators.Abstract.IAutoFakerGenerator.Generate(Soenneker.Utils.AutoBogus.Context.AutoFakerContext)
   at Soenneker.Utils.AutoBogus.Extensions.AutoGenerateContextExtension.Generate[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Soenneker.Utils.AutoBogus.Context.AutoFakerContext)
   at Soenneker.Utils.AutoBogus.AutoFaker.Generate[[System.__Canon, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]()
   at LcmCrdt.Tests.Changes.ChangeSerializationTests.FindBadType()

something as simple as this will reproduce the crash:

new AutoFaker().Generate<Action<string>>();

I don't think generating Actions is even possible or should be supported, but I do think a good error should be thrown, In my case I was trying to generate a bunch of types and it was quite difficult to track down the offender because I didn't know what was going on. A simple test like type.IsAssignableTo(typeof(Delegate) should work in my testing.

@soenneker
Copy link
Owner

I fixed some types like this recently, I'm surprised this got through. Thanks for the replication, I'll check it out.

@soenneker
Copy link
Owner

@hahn-kev are you using the latest version? I've tested this and it doesn't exception.

@hahn-kev
Copy link
Author

I'm so sorry, I did not try the latest version, we updated last month so I didn't think to check for a newer version. It does not crash anymore, however it just returns null which I didn't expect as it's not annotated to return null. I would prefer to configure it to throw an exception if it can't generate a type for some reason, would that be possible? This is especially important when generating an object graph and some dependency deep down can't be generated.

@soenneker
Copy link
Owner

@hahn-kev perhaps that's an option that could be added (e.g. ThrowIfCannotGenerate) ... I'm leery of making the return types all nullable. Thoughts?

@hahn-kev
Copy link
Author

I kinda feel like if it can return null then the API should communicate that to the developer. If I don't know then my code will throw at some point when it tries to use it. Your code can throw early or my code can throw later.

@soenneker
Copy link
Owner

@hahn-kev I wanted to make sure you're clear that even if we mark the generate() method as possibly returning null, it might generate a top level object with an instance but downstream objects could still be null. Does the nullable return value still help you considering this?

@hahn-kev
Copy link
Author

Not really, which is why I would still want an option to throw, otherwise a test might fail or code throw when something is null that shouldn't be null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants