-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Bug]: Wrong exception thrown by MemberInfoExtensions.IsDictionaryValueNonNullable #3070
Comments
For anyone who'd like to pick this up, the affected line of code is here: Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs Lines 108 to 111 in 09a1282
Instead, we need to recurse down through the type hierarchy until we get to the dictionary class, and then extract the type arguments from there. Otherwise there's no guarantee the first two type parameters will be the key and value, for example: public class MyObtuseDictionary<TWhatever, TValue, TKey> : Dictionary<TKey, TValue>; In this case, the key is generic argument |
The code that wrongly throws the exception is actually the block starting line 88. |
Thanks for clarifying. Both pieces of code are wrong with respect to your scenario (deriving from a dictionary with more than 2 type parameters), it just depends on which TFM your app uses as to which variant is executed. One throws, the other returns the wrong result. |
I have the same problem with my custom dictionary implementation:
Is there any kind of workaround or fix planned? |
- Do not throw if we cannot determine the nullability of a dictionary. - Clean-up some code analysis suggestions. Resolves domaindrivendev#3070. Resolves domaindrivendev#2793.
Describe the bug
Starting from version 6.7.2,
MemberInfoExtensions.IsDictionaryValueNonNullable
contains a bug that throws an exception when the declaring type of a class property that is assignable toDictionary{TKey,TValue}
has a number of generic type arguments that is not 2. This is easily possible when the declaring type extendsDictionary{TKey,TValue}
, e.g.public class MyDictionary: Dictionary<string, string>
Instead of picking the generic type arguments of the declaring type, you need to look at the type arguments of the
IDictionary
-interface of the declaring type.Expected behavior
That no exception is thrown, just like in versions <= 6.7.1.
Actual behavior
An exception is thrown on initialization, causing Swagger to fail to load API definition.
Steps to reproduce
No response
Exception(s) (if any)
No response
Swashbuckle.AspNetCore version
6.7.2
.NET Version
8
Anything else?
No response
The text was updated successfully, but these errors were encountered: