From 30a6d945c25c1fe5795706ca965671a8d8ef7568 Mon Sep 17 00:00:00 2001 From: Ivan Maximov Date: Tue, 17 Sep 2019 14:17:29 +0300 Subject: [PATCH] add support for default keyword for non primitive structs --- src/PublicApiGenerator/ApiGenerator.cs | 10 ++++++++-- src/PublicApiGeneratorTests/Method_parameters.cs | 5 +++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/PublicApiGenerator/ApiGenerator.cs b/src/PublicApiGenerator/ApiGenerator.cs index 01ddae6..4ac50e3 100644 --- a/src/PublicApiGenerator/ApiGenerator.cs +++ b/src/PublicApiGenerator/ApiGenerator.cs @@ -644,9 +644,15 @@ static bool IsExtensionMethod(ICustomAttributeProvider method) } } - static object FormatParameterConstant(IConstantProvider parameter) + static object FormatParameterConstant(ParameterDefinition parameter) { - return parameter.Constant is string ? string.Format(CultureInfo.InvariantCulture, "\"{0}\"", parameter.Constant) : (parameter.Constant ?? "null"); + if (parameter.Constant is string) + return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", parameter.Constant); + + if (parameter.Constant != null) + return parameter.Constant; + + return parameter.ParameterType.IsValueType ? "default" : "null"; } static MemberAttributes GetMethodAttributes(MethodDefinition method) diff --git a/src/PublicApiGeneratorTests/Method_parameters.cs b/src/PublicApiGeneratorTests/Method_parameters.cs index 7c4ce9a..ee4205d 100644 --- a/src/PublicApiGeneratorTests/Method_parameters.cs +++ b/src/PublicApiGeneratorTests/Method_parameters.cs @@ -1,4 +1,5 @@ using PublicApiGeneratorTests.Examples; +using System.Threading; using Xunit; namespace PublicApiGeneratorTests @@ -168,7 +169,7 @@ public void Should_output_default_values() public class MethodWithDefaultValues { public MethodWithDefaultValues() { } - public void Method(int value1 = 42, string value2 = ""hello world"") { } + public void Method(int value1 = 42, string value2 = ""hello world"", System.Threading.CancellationToken token = default, int value3 = 0, string value4 = null) { } } }"); } @@ -351,7 +352,7 @@ public void Method(int value1, string value2, ComplexType value3) public class MethodWithDefaultValues { - public void Method(int value1 = 42, string value2 = "hello world") + public void Method(int value1 = 42, string value2 = "hello world", CancellationToken token = default, int value3 = default, string value4 = default) { } }