Skip to content

Commit

Permalink
Fix nullability detection for arguments (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 authored Oct 31, 2021
1 parent 267c3ca commit c3bb6ee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/GraphQL.DI/DIObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,11 @@ protected virtual bool GetNullability(MethodInfo method, ParameterInfo parameter
nullable = (Nullability)(byte)attribute.ConstructorArguments[0].Value;
}

var nullabilityBytes = attribute?.ConstructorArguments[0].Value as byte[];
if ((nullabilityBytes != null && nullabilityBytes[0] == (byte)Nullability.Nullable) || (nullabilityBytes == null && nullable == Nullability.Nullable))
var nullabilityBytes = attribute?.ConstructorArguments[0].Value as IList<CustomAttributeTypedArgument>;
if ((nullabilityBytes != null && (byte)nullabilityBytes[0].Value == (byte)Nullability.Nullable) || (nullabilityBytes == null && nullable == Nullability.Nullable))
return true;
if (nullabilityBytes != null)
return (Nullability)nullabilityBytes[0] != Nullability.NonNullable;
return (Nullability)(byte)nullabilityBytes[0].Value != Nullability.NonNullable;
return nullable != Nullability.NonNullable;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Tests/DIObjectGraphTypeTests/Nullable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,19 @@ public void Method(Type type, string methodName, bool expected)
[InlineData(typeof(NullableClass13), "Field2", "arg3", false)]
[InlineData(typeof(NullableClass13), "Field2", "arg4", true)]
[InlineData(typeof(NullableClass13), "Field2", "arg5", true)]
[InlineData(typeof(NullableClass13), "Field2", "arg6", false)]
[InlineData(typeof(NullableClass13), "Field2", "arg7", false)]
[InlineData(typeof(NullableClass13), "Field2", "arg8", true)]
[InlineData(typeof(NullableClass13), "Field2", "arg9", true)]
[InlineData(typeof(NullableClass14), "Field2", "arg1", true)]
[InlineData(typeof(NullableClass14), "Field2", "arg2", false)]
[InlineData(typeof(NullableClass14), "Field2", "arg3", false)]
[InlineData(typeof(NullableClass14), "Field2", "arg4", true)]
[InlineData(typeof(NullableClass14), "Field2", "arg5", false)]
[InlineData(typeof(NullableClass14), "Field2", "arg6", false)]
[InlineData(typeof(NullableClass14), "Field2", "arg7", false)]
[InlineData(typeof(NullableClass14), "Field2", "arg8", true)]
[InlineData(typeof(NullableClass14), "Field2", "arg9", true)]
public void Argument(Type type, string methodName, string argumentName, bool expected)
{
var method = type.GetMethod(methodName);
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/NullabilityTestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public class NullableClass12
public class NullableClass13
{
public static string Field1() => "test";
public static string Field2(string arg1, string? arg2, int arg3, int? arg4, [Optional] string arg5) => "test";
public static string Field2(string arg1, string? arg2, int arg3, int? arg4, [Optional] string arg5, IEnumerable<string> arg6, IEnumerable<string?> arg7, IEnumerable<string>? arg8, IEnumerable<string?>? arg9) => "test";
}

public class NullableClass14
{
public static string? Field1() => "test";
public static string? Field2(string? arg1, string arg2, int arg3, int? arg4, [Required] string? arg5) => "test";
public static string? Field2(string? arg1, string arg2, int arg3, int? arg4, [Required] string? arg5, IEnumerable<string> arg6, IEnumerable<string?> arg7, IEnumerable<string>? arg8, IEnumerable<string?>? arg9) => "test";
}

public class NullableClass15
Expand Down

0 comments on commit c3bb6ee

Please sign in to comment.