Skip to content

Fix handling of parameters with default null #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -9,6 +10,9 @@ namespace AutomaticInterface;

public static class Builder
{
private static Regex ParamWithDefaultValueRegex { get; } =
new(pattern: @"^(?<type>\S+)(?<rest>\s+\S+\s+=.+)$", options: RegexOptions.Compiled);

private static string InheritDoc(ISymbol source) =>
$"/// <inheritdoc cref=\"{source.ToDisplayString().Replace('<', '{').Replace('>', '}')}\" />"; // we use inherit doc because that should be able to fetch documentation from base classes.

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

var paramResult = new HashSet<string>();
method
.Parameters.Select(x => x.ToDisplayString(FullyQualifiedDisplayFormat))
.Parameters.Select(x =>
{
var dispString = x.ToDisplayString(FullyQualifiedDisplayFormat);
if (x.HasExplicitDefaultValue && x.ExplicitDefaultValue is null && x.NullableAnnotation != NullableAnnotation.Annotated)
{
var match = ParamWithDefaultValueRegex.Match(dispString);
if (match.Success)
{
dispString = $"{match.Groups["type"].Value}?{match.Groups["rest"].Value}";
}
}
return dispString;
})
.ToList()
.ForEach(x => paramResult.Add(x));

Expand Down Expand Up @@ -193,7 +209,7 @@ private static void ActivateNullableIfNeeded(
IMethodSymbol method
)
{
var hasNullableParameter = method.Parameters.Any(x => IsNullable(x.Type));
var hasNullableParameter = method.Parameters.Any(x => IsNullable(x.Type) || (x.HasExplicitDefaultValue && x.ExplicitDefaultValue is null));

var hasNullableReturn = IsNullable(method.ReturnType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace AutomaticInterfaceExample
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.GetFinalDocumentsByIDFails(string, string, bool, bool?, System.Threading.CancellationToken)" />
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));
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));

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
// </auto-generated>
//--------------------------------------------------------------------------------------------------

#nullable enable
namespace AutomaticInterfaceExample
{
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.TryStartTransaction(string)" />
bool TryStartTransaction(string data = null);
bool TryStartTransaction(string? data = null);

}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CustomNameSpace
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.GetFinalDocumentsByIDFails(string, string, bool, bool?, System.Threading.CancellationToken)" />
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));
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));

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
// </auto-generated>
//--------------------------------------------------------------------------------------------------

#nullable enable
namespace AutomaticInterfaceExample
{
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.TryStartTransaction(AutomaticInterfaceExample.MyStruct)" />
bool TryStartTransaction(global::AutomaticInterfaceExample.MyStruct data = default(global::AutomaticInterfaceExample.MyStruct));
bool TryStartTransaction(global::AutomaticInterfaceExample.MyStruct? data = default(global::AutomaticInterfaceExample.MyStruct));

}
}
#nullable restore