Skip to content

Commit

Permalink
Merge a2213b5 into 1d2165d
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane32 committed Oct 28, 2021
2 parents 1d2165d + a2213b5 commit e4720ba
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
24 changes: 15 additions & 9 deletions src/GraphQL.DI/DIObjectGraphType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,10 @@ private Nullability GetMethodDefaultNullability(MethodInfo method)
var nullable = Nullability.Unknown;

// check the parent type first to see if there's a nullable context attribute set for it
var parentType = method.DeclaringType;
var attribute = parentType.CustomAttributes.FirstOrDefault(x =>
x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute" &&
x.ConstructorArguments.Count == 1 &&
x.ConstructorArguments[0].ArgumentType == typeof(byte));
if (attribute != null) {
nullable = (Nullability)(byte)attribute.ConstructorArguments[0].Value;
}
CheckDeclaringType(method.DeclaringType);

// now check the method to see if there's a nullable context attribute set for it
attribute = method.CustomAttributes.FirstOrDefault(x =>
var attribute = method.CustomAttributes.FirstOrDefault(x =>
x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute" &&
x.ConstructorArguments.Count == 1 &&
x.ConstructorArguments[0].ArgumentType == typeof(byte));
Expand All @@ -323,6 +316,19 @@ private Nullability GetMethodDefaultNullability(MethodInfo method)
}

return nullable;

void CheckDeclaringType(Type parentType)
{
if (parentType.DeclaringType != null)
CheckDeclaringType(parentType.DeclaringType);
var attribute = parentType.CustomAttributes.FirstOrDefault(x =>
x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute" &&
x.ConstructorArguments.Count == 1 &&
x.ConstructorArguments[0].ArgumentType == typeof(byte));
if (attribute != null) {
nullable = (Nullability)(byte)attribute.ConstructorArguments[0].Value;
}
}
}

/// <summary>
Expand Down
48 changes: 34 additions & 14 deletions src/Tests/DIObjectGraphTypeTests/Nullable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using GraphQL.DI;
Expand All @@ -16,20 +17,22 @@ public class Nullable
// tests may not be testing the anticipated scenarios
//Method and Argument should always pass
[Theory]
[InlineData(typeof(NullableClass1), 1)] //default not nullable
[InlineData(typeof(NullableClass2), 2)] //default nullable
[InlineData(typeof(NullableClass5), null)]
[InlineData(typeof(NullableClass6), null)]
[InlineData(typeof(NullableClass7), 1)] //default not nullable
[InlineData(typeof(NullableClass8), 2)] //default nullable
[InlineData(typeof(NullableClass9), null)]
[InlineData(typeof(NullableClass10), null)]
[InlineData(typeof(NullableClass11), 1)] //default not nullable
[InlineData(typeof(NullableClass12), null)]
[InlineData(typeof(NullableClass13), 1)] //default not nullable
[InlineData(typeof(NullableClass14), 2)] //default nullable
[InlineData(typeof(NullableClass15), null)]
public void VerifyTestClass(Type type, int? nullableContext)
[InlineData(typeof(NullableClass1), 1, 0)] //default not nullable
[InlineData(typeof(NullableClass2), 2, 0)] //default nullable
[InlineData(typeof(NullableClass5), null, null)]
[InlineData(typeof(NullableClass6), null, null)]
[InlineData(typeof(NullableClass7), 1, 0)] //default not nullable
[InlineData(typeof(NullableClass8), 2, 0)] //default nullable
[InlineData(typeof(NullableClass9), null, null)]
[InlineData(typeof(NullableClass10), null, null)]
[InlineData(typeof(NullableClass11), 1, 0)] //default not nullable
[InlineData(typeof(NullableClass12), null, null)]
[InlineData(typeof(NullableClass13), 1, 0)] //default not nullable
[InlineData(typeof(NullableClass14), 2, 0)] //default nullable
[InlineData(typeof(NullableClass15), null, null)]
[InlineData(typeof(NullableClass16), 1, 0)]
[InlineData(typeof(NullableClass16.NestedClass1), null, 0)]
public void VerifyTestClass(Type type, int? nullableContext, int? nullable)
{
var actualHasNullableContext = type.CustomAttributes.FirstOrDefault(
x => x.AttributeType.Name == "NullableContextAttribute");
Expand All @@ -39,6 +42,15 @@ public void VerifyTestClass(Type type, int? nullableContext)
actualHasNullableContext.ShouldNotBeNull();
actualHasNullableContext.ConstructorArguments[0].Value.ShouldBe(nullableContext);
}

var actualHasNullable = type.CustomAttributes.FirstOrDefault(
x => x.AttributeType.Name == "NullableAttribute");
if (nullable == null) {
actualHasNullable.ShouldBeNull();
} else {
actualHasNullable.ShouldNotBeNull();
actualHasNullable.ConstructorArguments[0].Value.ShouldBe(nullable);
}
}

[Theory]
Expand Down Expand Up @@ -86,6 +98,10 @@ public void VerifyTestClass(Type type, int? nullableContext)
[InlineData(typeof(NullableClass15), "Field2", true, false)]
[InlineData(typeof(NullableClass15), "Field3", true, false)]
[InlineData(typeof(NullableClass15), "Field4", false, true)]
[InlineData(typeof(NullableClass16), "Field1", false, false)]
[InlineData(typeof(NullableClass16), "Field2", false, false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field1", false, false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field2", false, false)]
public void VerifyTestMethod(Type type, string methodName, bool hasNullable, bool hasNullableContext)
{
var method = type.GetMethod(methodName);
Expand Down Expand Up @@ -171,6 +187,10 @@ public void VerifyTestArgument(Type type, string methodName, string argumentName
[InlineData(typeof(NullableClass15), "Field2", true)]
[InlineData(typeof(NullableClass15), "Field3", true)]
[InlineData(typeof(NullableClass15), "Field4", true)]
[InlineData(typeof(NullableClass16), "Field1", false)]
[InlineData(typeof(NullableClass16), "Field2", false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field1", false)]
[InlineData(typeof(NullableClass16.NestedClass1), "Field2", false)]
public void Method(Type type, string methodName, bool expected)
{
var method = type.GetMethod(methodName);
Expand Down
13 changes: 13 additions & 0 deletions src/Tests/NullabilityTestClasses.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System.Collections.Generic;
using System.Threading.Tasks;
using GraphQL.DataLoader;
using GraphQL.DI;
Expand Down Expand Up @@ -108,4 +109,16 @@ public class NullableClass15
public static Task<string>? Field3() => null!;
public static Task<string?>? Field4() => null!;
}

public class NullableClass16
{
public static string Field1() => "test";
public static string Field2() => "test";

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

0 comments on commit e4720ba

Please sign in to comment.