always write nullable context
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
@@ -159,6 +160,36 @@ public class MethodGenerationTests
|
||||
var _ = _sut.StringMethod();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StringMethodNullable_IsImplemented()
|
||||
{
|
||||
var method =
|
||||
typeof(IMethodsTestService).GetMethod(nameof(MethodsTestService.StringMethodNullable))
|
||||
?? throw new InvalidOperationException();
|
||||
|
||||
method.Should().NotBeNull();
|
||||
method.ReturnType.Should().Be(typeof(string));
|
||||
IsNullable(method.ReturnType).Should().BeTrue();
|
||||
|
||||
var parameters = method.GetParameters();
|
||||
parameters.Should().BeEmpty();
|
||||
|
||||
var _ = _sut.StringMethod();
|
||||
}
|
||||
|
||||
private static bool IsNullable(Type type)
|
||||
{
|
||||
var nullableContextAttribute = type.GetCustomAttribute<NullableContextAttribute>();
|
||||
|
||||
// NullableContextAttribute exists and has a flag indicating nullable annotations
|
||||
if (nullableContextAttribute != null && nullableContextAttribute.Flag == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericVoidMethod_IsImplemented()
|
||||
{
|
||||
@@ -312,6 +343,11 @@ internal class MethodsTestService : IMethodsTestService
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string? StringMethodNullable()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public void GenericVoidMethod<TX, TY>() { }
|
||||
|
||||
public void GenericVoidMethodWithGenericParam<TX, TY>(TX a) { }
|
||||
|
||||
@@ -176,10 +176,8 @@ public class AutoInterfaceGenerator : ISourceGenerator
|
||||
var visibilityModifier = InferVisibilityModifier(implTypeSymbol, attributeData);
|
||||
|
||||
//https://stackoverflow.com/questions/55492214/the-annotation-for-nullable-reference-types-should-only-be-used-in-code-within-a fix for nullable
|
||||
if (implTypeSymbol.NullableAnnotation == NullableAnnotation.Annotated)
|
||||
{
|
||||
codeWriter.WriteLine("#nullable enable");
|
||||
}
|
||||
|
||||
codeWriter.WriteLine("#nullable enable");
|
||||
codeWriter.WriteLine("namespace {0}", namespaceName);
|
||||
codeWriter.WriteLine("{");
|
||||
|
||||
@@ -198,10 +196,7 @@ public class AutoInterfaceGenerator : ISourceGenerator
|
||||
--codeWriter.Indent;
|
||||
|
||||
codeWriter.WriteLine("}");
|
||||
if (implTypeSymbol.NullableAnnotation == NullableAnnotation.Annotated)
|
||||
{
|
||||
codeWriter.WriteLine("#nullable restore");
|
||||
}
|
||||
codeWriter.WriteLine("#nullable restore");
|
||||
|
||||
codeWriter.Flush();
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
Reference in New Issue
Block a user