Skip to content

Commit 35b887c

Browse files
Remove nullable from typename for inputs and outputs (#753)
1 parent a434c5c commit 35b887c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Transform/TransformBase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ commandInfo is CmdletInfo ||
100100
// We can also simplify the string if desired.
101101
private string GetAdjustedTypename(Type t, bool simplify = false)
102102
{
103-
string tName = simplify ? LanguagePrimitives.ConvertTo<string>(t) : t.FullName;
103+
var t2 = Nullable.GetUnderlyingType(t) ?? t;
104+
string tName = simplify ? LanguagePrimitives.ConvertTo<string>(t2) : t2.FullName;
104105
return tName;
105106
}
106107

@@ -847,7 +848,14 @@ protected List<InputOutput> GetInputOutputItemsFromHelp(dynamic typesInfo)
847848
// We also will remove trailing [] because we should generally return singletons
848849
private string FixUpTypeName(string typename)
849850
{
850-
string fixedString = typename.Trim();
851+
// If the type is a generic type, we need to remove the backtick and the number.
852+
string fixedString = typename.Replace("System.Nullable`1[[", string.Empty).Trim();
853+
int commaIndex = fixedString.IndexOf(',');
854+
if (commaIndex >= 0)
855+
{
856+
fixedString = fixedString.Substring(0, commaIndex).Trim();
857+
}
858+
851859
if (fixedString.EndsWith("[]"))
852860
{
853861
fixedString = fixedString.Remove(fixedString.Length - 2);

test/Pester/ConvertToCommandHelp.Tests.ps1

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ Describe "New-CommandHelp tests" {
141141
BeforeAll {
142142
$cmd = Get-Command Get-Command
143143
$ch = $cmd | New-CommandHelp
144+
145+
function global:Test-InputOutputTypes
146+
{
147+
[CmdletBinding()]
148+
[OutputType()]
149+
param(
150+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
151+
[Nullable[int]]$InputString
152+
)
153+
154+
Write-Output "Processed: $InputString"
155+
}
156+
157+
$io = New-CommandHelp -Command (Get-Command Test-InputOutputTypes)
144158
}
145159

146160
It "Should have the proper number of output types" {
@@ -176,12 +190,16 @@ Describe "New-CommandHelp tests" {
176190
@{ Type = "System.Management.Automation.CommandTypes" }
177191
@{ Type = "System.Int32" }
178192
@{ Type = "Microsoft.PowerShell.Commands.ModuleSpecification[]" }
179-
@{ Type = "System.String" }
193+
@{ Type = "System.String[]" }
180194
@{ Type = "System.Management.Automation.SwitchParameter" }
181195
) {
182196
param ($type)
183197
$ch.inputs.typename | Should -Contain $type
184198
}
185-
}
186199

200+
It "Should not have Nullable in the input type" {
201+
$io.inputs.typename | Should -Not -Contain "Nullable"
202+
$io.inputs.typename | Should -Be "System.Int32"
203+
}
204+
}
187205
}

0 commit comments

Comments
 (0)