Skip to content

Commit

Permalink
Fix nullability detection (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Oct 29, 2021
1 parent 6529e50 commit 267c3ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/GraphQL.DI/DIObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ protected virtual bool GetNullability(MethodInfo method)
nullable = (Nullability)(byte)attribute.ConstructorArguments[0].Value;
}

var nullabilityBytes = attribute?.ConstructorArguments[0].Value as byte[];
var nullabilityBytes = attribute?.ConstructorArguments[0].Value as IList<CustomAttributeTypedArgument>;
var index = 0;
nullable = Consider(method.ReturnType);
return nullable != Nullability.NonNullable;
Expand All @@ -278,7 +278,7 @@ Nullability Consider(Type t)
return Nullability.Nullable;
if (t.IsValueType)
return Nullability.NonNullable;
if ((nullabilityBytes != null && nullabilityBytes[index] == (byte)Nullability.Nullable) || (nullabilityBytes == null && nullable == Nullability.Nullable))
if ((nullabilityBytes != null && (byte)nullabilityBytes[index].Value == (byte)Nullability.Nullable) || (nullabilityBytes == null && nullable == Nullability.Nullable))
return Nullability.Nullable;
if (g == typeof(IDataLoaderResult<>) || g == typeof(Task<>)) {
index++;
Expand All @@ -287,7 +287,7 @@ Nullability Consider(Type t)
if (t == typeof(IDataLoaderResult))
return Nullability.Nullable;
if (nullabilityBytes != null)
return (Nullability)nullabilityBytes[index];
return (Nullability)(byte)nullabilityBytes[index].Value;
return nullable;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Tests/DIObjectGraphTypeTests/Nullable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Nullable
[InlineData(typeof(NullableClass15), null, null)]
[InlineData(typeof(NullableClass16), 1, 0)]
[InlineData(typeof(NullableClass16.NestedClass1), null, 0)]
[InlineData(typeof(NullableClass17), 1, 0)]
public void VerifyTestClass(Type type, int? nullableContext, int? nullable)
{
var actualHasNullableContext = type.CustomAttributes.FirstOrDefault(
Expand Down Expand Up @@ -100,8 +101,13 @@ public void VerifyTestClass(Type type, int? nullableContext, int? nullable)
[InlineData(typeof(NullableClass15), "Field4", false, true)]
[InlineData(typeof(NullableClass16), "Field1", false, false)]
[InlineData(typeof(NullableClass16), "Field2", false, false)]
[InlineData(typeof(NullableClass16), "Field3", false, true)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field1", false, false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field2", false, false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field3", false, true)]
[InlineData(typeof(NullableClass17), "Field1", false, false)]
[InlineData(typeof(NullableClass17), "Field2", false, false)]
[InlineData(typeof(NullableClass17), "Field3", true, false)]
public void VerifyTestMethod(Type type, string methodName, bool hasNullable, bool hasNullableContext)
{
var method = type.GetMethod(methodName);
Expand Down Expand Up @@ -189,8 +195,13 @@ public void VerifyTestArgument(Type type, string methodName, string argumentName
[InlineData(typeof(NullableClass15), "Field4", true)]
[InlineData(typeof(NullableClass16), "Field1", false)]
[InlineData(typeof(NullableClass16), "Field2", false)]
[InlineData(typeof(NullableClass16), "Field3", true)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field1", false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field2", false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field3", true)]
[InlineData(typeof(NullableClass17), "Field1", false)]
[InlineData(typeof(NullableClass17), "Field2", false)]
[InlineData(typeof(NullableClass17), "Field3", true)]
public void Method(Type type, string methodName, bool expected)
{
var method = type.GetMethod(methodName);
Expand Down
9 changes: 9 additions & 0 deletions src/Tests/NullabilityTestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,20 @@ public class NullableClass16
{
public static string Field1() => "test";
public static string Field2() => "test";
public static string? Field3() => "test";

public class NestedClass1
{
public static string Field1() => "test";
public static string Field2() => "test";
public static string? Field3() => "test";
}
}

public class NullableClass17
{
public static Task<string> Field1() => null!;
public static Task<string> Field2() => null!;
public static Task<string?> Field3() => null!;
}
}

0 comments on commit 267c3ca

Please sign in to comment.