|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace Swashbuckle.AspNetCore.SwaggerGen; |
| 7 | + |
| 8 | +#nullable enable |
| 9 | + |
| 10 | +public static class MemberInfoExtensionsTests |
| 11 | +{ |
| 12 | + [Theory] |
| 13 | + [InlineData(typeof(MyClass), nameof(MyClass.DictionaryInt32NonNullable), true)] |
| 14 | + [InlineData(typeof(MyClass), nameof(MyClass.DictionaryInt32Nullable), false)] |
| 15 | + [InlineData(typeof(MyClass), nameof(MyClass.DictionaryStringNonNullable), true)] |
| 16 | + [InlineData(typeof(MyClass), nameof(MyClass.DictionaryStringNullable), false)] |
| 17 | + [InlineData(typeof(MyClass), nameof(MyClass.IDictionaryInt32NonNullable), true)] |
| 18 | + [InlineData(typeof(MyClass), nameof(MyClass.IDictionaryInt32Nullable), false)] |
| 19 | + [InlineData(typeof(MyClass), nameof(MyClass.IDictionaryStringNonNullable), true)] |
| 20 | + [InlineData(typeof(MyClass), nameof(MyClass.IDictionaryStringNullable), false)] |
| 21 | + [InlineData(typeof(MyClass), nameof(MyClass.IReadOnlyDictionaryInt32NonNullable), true)] |
| 22 | + [InlineData(typeof(MyClass), nameof(MyClass.IReadOnlyDictionaryInt32Nullable), false)] |
| 23 | + [InlineData(typeof(MyClass), nameof(MyClass.IReadOnlyDictionaryStringNonNullable), true)] |
| 24 | + [InlineData(typeof(MyClass), nameof(MyClass.IReadOnlyDictionaryStringNullable), false)] |
| 25 | + [InlineData(typeof(MyClass), nameof(MyClass.StringDictionary), false)] // There is no way to inspect the nullability of the base class' TValue argument |
| 26 | + [InlineData(typeof(MyClass), nameof(MyClass.NullableStringDictionary), false)] |
| 27 | + [InlineData(typeof(MyClass), nameof(MyClass.SameTypesDictionary), true)] |
| 28 | + [InlineData(typeof(MyClass), nameof(MyClass.CustomDictionaryStringNullable), false)] |
| 29 | + [InlineData(typeof(MyClass), nameof(MyClass.CustomDictionaryStringNonNullable), true)] |
| 30 | + public static void IsDictionaryValueNonNullable_Returns_Correct_Value(Type type, string memberName, bool expected) |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var memberInfo = type.GetMember(memberName).First(); |
| 34 | + |
| 35 | + // Act |
| 36 | + var actual = memberInfo.IsDictionaryValueNonNullable(); |
| 37 | + |
| 38 | + // Assert |
| 39 | + Assert.Equal(expected, actual); |
| 40 | + } |
| 41 | + |
| 42 | + public class MyClass |
| 43 | + { |
| 44 | + public Dictionary<string, int> DictionaryInt32NonNullable { get; set; } = []; |
| 45 | + |
| 46 | + public Dictionary<string, int?> DictionaryInt32Nullable { get; set; } = []; |
| 47 | + |
| 48 | + public Dictionary<string, string> DictionaryStringNonNullable { get; set; } = []; |
| 49 | + |
| 50 | + public Dictionary<string, string?> DictionaryStringNullable { get; set; } = []; |
| 51 | + |
| 52 | + public IDictionary<string, int> IDictionaryInt32NonNullable { get; set; } = new Dictionary<string, int>(); |
| 53 | + |
| 54 | + public IDictionary<string, int?> IDictionaryInt32Nullable { get; set; } = new Dictionary<string, int?>(); |
| 55 | + |
| 56 | + public IDictionary<string, string> IDictionaryStringNonNullable { get; set; } = new Dictionary<string, string>(); |
| 57 | + |
| 58 | + public IDictionary<string, string?> IDictionaryStringNullable { get; set; } = new Dictionary<string, string?>(); |
| 59 | + |
| 60 | + public IReadOnlyDictionary<string, int> IReadOnlyDictionaryInt32NonNullable { get; set; } = new Dictionary<string, int>(); |
| 61 | + |
| 62 | + public IReadOnlyDictionary<string, int?> IReadOnlyDictionaryInt32Nullable { get; set; } = new Dictionary<string, int?>(); |
| 63 | + |
| 64 | + public IReadOnlyDictionary<string, string> IReadOnlyDictionaryStringNonNullable { get; set; } = new Dictionary<string, string>(); |
| 65 | + |
| 66 | + public IReadOnlyDictionary<string, string?> IReadOnlyDictionaryStringNullable { get; set; } = new Dictionary<string, string?>(); |
| 67 | + |
| 68 | + public StringDictionary StringDictionary { get; set; } = []; |
| 69 | + |
| 70 | + public NullableStringDictionary NullableStringDictionary { get; set; } = []; |
| 71 | + |
| 72 | + public SameTypesDictionary<string> SameTypesDictionary { get; set; } = []; |
| 73 | + |
| 74 | + public CustomDictionary<string, string?> CustomDictionaryStringNullable { get; set; } = []; |
| 75 | + |
| 76 | + public CustomDictionary<string, string> CustomDictionaryStringNonNullable { get; set; } = []; |
| 77 | + } |
| 78 | + |
| 79 | + public class StringDictionary : Dictionary<string, string>; |
| 80 | + |
| 81 | + public class NullableStringDictionary : Dictionary<string, string?>; |
| 82 | + |
| 83 | + public class SameTypesDictionary<T> : Dictionary<T, T> where T : notnull; |
| 84 | + |
| 85 | + public class CustomDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TKey : notnull; |
| 86 | +} |
0 commit comments