Skip to content

Commit bd316f3

Browse files
committed
Fix handling of parameters with default null
1 parent becdf12 commit bd316f3

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

AutomaticInterface/AutomaticInterface/Builder.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
45
using Microsoft.CodeAnalysis;
56
using Microsoft.CodeAnalysis.CSharp;
67
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -9,6 +10,9 @@ namespace AutomaticInterface;
910

1011
public static class Builder
1112
{
13+
private static Regex ParamWithDefaultValueRegex { get; } =
14+
new(pattern: @"^(?<type>\S+)(?<rest>\s+\S+\s+=.+)$", options: RegexOptions.Compiled);
15+
1216
private static string InheritDoc(ISymbol source) =>
1317
$"/// <inheritdoc cref=\"{source.ToDisplayString().Replace('<', '{').Replace('>', '}')}\" />"; // we use inherit doc because that should be able to fetch documentation from base classes.
1418

@@ -155,7 +159,19 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth
155159

156160
var paramResult = new HashSet<string>();
157161
method
158-
.Parameters.Select(x => x.ToDisplayString(FullyQualifiedDisplayFormat))
162+
.Parameters.Select(x =>
163+
{
164+
var dispString = x.ToDisplayString(FullyQualifiedDisplayFormat);
165+
if (x.HasExplicitDefaultValue && x.ExplicitDefaultValue is null && x.NullableAnnotation != NullableAnnotation.Annotated)
166+
{
167+
var match = ParamWithDefaultValueRegex.Match(dispString);
168+
if (match.Success)
169+
{
170+
dispString = $"{match.Groups["type"].Value}?{match.Groups["rest"].Value}";
171+
}
172+
}
173+
return dispString;
174+
})
159175
.ToList()
160176
.ForEach(x => paramResult.Add(x));
161177

@@ -193,7 +209,7 @@ private static void ActivateNullableIfNeeded(
193209
IMethodSymbol method
194210
)
195211
{
196-
var hasNullableParameter = method.Parameters.Any(x => IsNullable(x.Type));
212+
var hasNullableParameter = method.Parameters.Any(x => IsNullable(x.Type) || (x.HasExplicitDefaultValue && x.ExplicitDefaultValue is null));
197213

198214
var hasNullableReturn = IsNullable(method.ReturnType);
199215

AutomaticInterface/Tests/Methods/Methods.BooleanWithNonNull.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace AutomaticInterfaceExample
1616
public partial interface IDemoClass
1717
{
1818
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.GetFinalDocumentsByIDFails(string, string, bool, bool?, System.Threading.CancellationToken)" />
19-
global::System.Threading.Tasks.Task<global::System.IO.Stream?> GetFinalDocumentsByIDFails(string agreementID, string docType, bool amt = false, bool? attachSupportingDocuments = true, global::System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken));
19+
global::System.Threading.Tasks.Task<global::System.IO.Stream?> GetFinalDocumentsByIDFails(string agreementID, string docType, bool amt = false, bool? attachSupportingDocuments = true, global::System.Threading.CancellationToken? cancellationToken = default(global::System.Threading.CancellationToken));
2020

2121
}
2222
}

AutomaticInterface/Tests/Methods/Methods.WorksWithOptionalNullParameters.verified.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
// </auto-generated>
77
//--------------------------------------------------------------------------------------------------
88

9+
#nullable enable
910
namespace AutomaticInterfaceExample
1011
{
1112
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
1213
public partial interface IDemoClass
1314
{
1415
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.TryStartTransaction(string)" />
15-
bool TryStartTransaction(string data = null);
16+
bool TryStartTransaction(string? data = null);
1617

1718
}
1819
}
20+
#nullable restore

AutomaticInterface/Tests/Misc/Misc.CustomNameSpace.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace CustomNameSpace
1616
public partial interface IDemoClass
1717
{
1818
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.GetFinalDocumentsByIDFails(string, string, bool, bool?, System.Threading.CancellationToken)" />
19-
global::System.Threading.Tasks.Task<global::System.IO.Stream?> GetFinalDocumentsByIDFails(string agreementID, string docType, bool amt = false, bool? attachSupportingDocuments = true, global::System.Threading.CancellationToken cancellationToken = default(global::System.Threading.CancellationToken));
19+
global::System.Threading.Tasks.Task<global::System.IO.Stream?> GetFinalDocumentsByIDFails(string agreementID, string docType, bool amt = false, bool? attachSupportingDocuments = true, global::System.Threading.CancellationToken? cancellationToken = default(global::System.Threading.CancellationToken));
2020

2121
}
2222
}

AutomaticInterface/Tests/Misc/Misc.WorksWithOptionalStructParameters.verified.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
// </auto-generated>
77
//--------------------------------------------------------------------------------------------------
88

9+
#nullable enable
910
namespace AutomaticInterfaceExample
1011
{
1112
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
1213
public partial interface IDemoClass
1314
{
1415
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.TryStartTransaction(AutomaticInterfaceExample.MyStruct)" />
15-
bool TryStartTransaction(global::AutomaticInterfaceExample.MyStruct data = default(global::AutomaticInterfaceExample.MyStruct));
16+
bool TryStartTransaction(global::AutomaticInterfaceExample.MyStruct? data = default(global::AutomaticInterfaceExample.MyStruct));
1617

1718
}
1819
}
20+
#nullable restore

0 commit comments

Comments
 (0)