Skip to content

Commit 5ff5bb4

Browse files
d1820claude
andcommitted
Fix nullable return type cref generation (issue #37) and add test coverage
NullableTypeSyntax (e.g. string?) was falling into the else branch of BuildComment and calling ToFullString(), producing invalid XML like <see cref="string?"/>. Fix recurses on the inner ElementType, stripping the ? before cref generation. Also updates MethodWithNullableReturnTestFixCode.cs to reflect the now-correct output (MethodTester instead of MethodTester?) and adds a non-cref nullable string return test case. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 64df47d commit 5ff5bb4

9 files changed

Lines changed: 53 additions & 5 deletions

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dotnet.preferCSharpExtension": true
3+
}

CodeDocumentor.Common/Constructors/BaseReturnTypeCommentConstruction.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public virtual string BuildComment(TypeSyntax returnType, ReturnTypeBuilderOptio
7777
{
7878
returnComment = GenerateGenericTypeComment(gst, options, wordMaps);
7979
}
80+
else if (returnType is NullableTypeSyntax nts)
81+
{
82+
returnComment = BuildComment(nts.ElementType, options, wordMaps);
83+
}
8084
else if (returnType is ArrayTypeSyntax ast)
8185
{
8286
var comment = string.Format(ArrayCommentTemplate, DocumentationHeaderHelper.DetermineSpecificObjectName(ast.ElementType, wordMaps, options.TryToIncludeCrefsForReturnTypes));

CodeDocumentor.Test/CodeDocumentor.Test.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
<Compile Remove="Methods\TestFiles\MethodWithMIxedExceptionTestFixCode.cs" />
8080
<Compile Remove="Methods\TestFiles\MethodWithNullableStructParameterTestCode.cs" />
8181
<Compile Remove="Methods\TestFiles\MethodWithNullableStructParameterTestFixCode.cs" />
82+
<Compile Remove="Methods\TestFiles\MethodWithNullableStringReturnTestCode.cs" />
83+
<Compile Remove="Methods\TestFiles\MethodWithNullableStringReturnTestFixCode.cs" />
8284
<Compile Remove="Methods\TestFiles\MethodWithObjectReturnTestCode.cs" />
8385
<Compile Remove="Methods\TestFiles\MethodWithObjectReturnTestFixCode.cs" />
8486
<Compile Remove="Methods\TestFiles\MethodWithParameterTestCode.cs" />
@@ -446,6 +448,12 @@
446448
<Content Include="Methods\TestFiles\MethodWithNullableStructParameterTestFixCode.cs">
447449
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
448450
</Content>
451+
<Content Include="Methods\TestFiles\MethodWithNullableStringReturnTestCode.cs">
452+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
453+
</Content>
454+
<Content Include="Methods\TestFiles\MethodWithNullableStringReturnTestFixCode.cs">
455+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
456+
</Content>
449457
<Content Include="Methods\TestFiles\MethodWithObjectReturnTestCode.cs">
450458
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
451459
</Content>

CodeDocumentor.Test/Methods/MethodUnitTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public async Task NoDiagnosticsShow(string testCode)
5555
[InlineData("MethodWithNullableStructParameterTestCode", "MethodWithNullableStructParameterTestFixCode", 9, 21)]
5656
[InlineData("MethodWithReturnTestCode", "MethodWithReturnTestFixCode", 9, 29)]
5757
[InlineData("MethodWithStringReturnTestCode", "MethodWithStringReturnTestFixCode", 9, 23)]
58+
[InlineData("MethodWithNullableStringReturnTestCode", "MethodWithNullableStringReturnTestFixCode", 9, 24)]
5859
[InlineData("MethodWithObjectReturnTestCode", "MethodWithObjectReturnTestFixCode", 9, 23)]
5960
[InlineData("MethodWithIntReturnTestCode", "MethodWithIntReturnTestFixCode", 9, 20)]
6061
[InlineData("MethodWithListIntReturnTestCode", "MethodWithListIntReturnTestFixCode", 9, 26)]

CodeDocumentor.Test/Methods/TestFiles/Crefs/MethodWithNullableReturnTestFixCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class MethodTester
99
/// <summary>
1010
/// Show method with return tester.
1111
/// </summary>
12-
/// <returns>A <see cref="MethodTester?"/></returns>
12+
/// <returns>A <see cref="MethodTester"/></returns>
1313
public MethodTester? ShowMethodWithReturnTester()
1414
{
1515
return null;

CodeDocumentor.Test/Methods/TestFiles/Crefs/MethodWithNullableStringReturnTestCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ConsoleApp40
66
{
77
public class MethodTester
88
{
9-
public string? ShowMethodWithStringReturnTester()
9+
public string? ShowMethodWithNullableStringReturnTester()
1010
{
1111
return null;
1212
}

CodeDocumentor.Test/Methods/TestFiles/Crefs/MethodWithNullableStringReturnTestFixCode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace ConsoleApp40
77
public class MethodTester
88
{
99
/// <summary>
10-
/// Show method with string return tester.
10+
/// Show method with nullable string return tester.
1111
/// </summary>
12-
/// <returns>A <see cref="string?"/></returns>
13-
public string? ShowMethodWithStringReturnTester()
12+
/// <returns>A <see cref="string"/></returns>
13+
public string? ShowMethodWithNullableStringReturnTester()
1414
{
1515
return null;
1616
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ConsoleApp4
6+
{
7+
public class MethodTester
8+
{
9+
public string? ShowMethodWithNullableStringReturnTester()
10+
{
11+
return null;
12+
}
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ConsoleApp4
6+
{
7+
public class MethodTester
8+
{
9+
/// <summary>
10+
/// Show method with nullable string return tester.
11+
/// </summary>
12+
/// <returns>A string</returns>
13+
public string? ShowMethodWithNullableStringReturnTester()
14+
{
15+
return null;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)