Skip to content

Remove nullable from typename for inputs and outputs #753

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

Merged
merged 3 commits into from
Jun 27, 2025
Merged
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
12 changes: 10 additions & 2 deletions src/Transform/TransformBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ commandInfo is CmdletInfo ||
// We can also simplify the string if desired.
private string GetAdjustedTypename(Type t, bool simplify = false)
{
string tName = simplify ? LanguagePrimitives.ConvertTo<string>(t) : t.FullName;
var t2 = Nullable.GetUnderlyingType(t) ?? t;
string tName = simplify ? LanguagePrimitives.ConvertTo<string>(t2) : t2.FullName;
return tName;
}

Expand Down Expand Up @@ -847,7 +848,14 @@ protected List<InputOutput> GetInputOutputItemsFromHelp(dynamic typesInfo)
// We also will remove trailing [] because we should generally return singletons
private string FixUpTypeName(string typename)
{
string fixedString = typename.Trim();
// If the type is a generic type, we need to remove the backtick and the number.
string fixedString = typename.Replace("System.Nullable`1[[", string.Empty).Trim();
int commaIndex = fixedString.IndexOf(',');
if (commaIndex >= 0)
{
fixedString = fixedString.Substring(0, commaIndex).Trim();
}

if (fixedString.EndsWith("[]"))
{
fixedString = fixedString.Remove(fixedString.Length - 2);
Expand Down
22 changes: 20 additions & 2 deletions test/Pester/ConvertToCommandHelp.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ Describe "New-CommandHelp tests" {
BeforeAll {
$cmd = Get-Command Get-Command
$ch = $cmd | New-CommandHelp

function global:Test-InputOutputTypes
{
[CmdletBinding()]
[OutputType()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Nullable[int]]$InputString
)

Write-Output "Processed: $InputString"
}

$io = New-CommandHelp -Command (Get-Command Test-InputOutputTypes)
}

It "Should have the proper number of output types" {
Expand Down Expand Up @@ -176,12 +190,16 @@ Describe "New-CommandHelp tests" {
@{ Type = "System.Management.Automation.CommandTypes" }
@{ Type = "System.Int32" }
@{ Type = "Microsoft.PowerShell.Commands.ModuleSpecification[]" }
@{ Type = "System.String" }
@{ Type = "System.String[]" }
@{ Type = "System.Management.Automation.SwitchParameter" }
) {
param ($type)
$ch.inputs.typename | Should -Contain $type
}
}

It "Should not have Nullable in the input type" {
$io.inputs.typename | Should -Not -Contain "Nullable"
$io.inputs.typename | Should -Be "System.Int32"
}
}
}