diff --git a/PSSwagger/Paths.psm1 b/PSSwagger/Paths.psm1 index ec3e18a..435348d 100644 --- a/PSSwagger/Paths.psm1 +++ b/PSSwagger/Paths.psm1 @@ -196,6 +196,25 @@ function Get-SwaggerSpecPathInfo } else { $commandNames = Get-PathCommandName -OperationId $operationId } + + # Priority of a parameterset will be used to determine the default parameterset of a cmdlet. + $Priority = 0 + $ParametersCount = Get-HashtableKeyCount -Hashtable $ParametersTable + if($ParametersCount) { + # Priority for parameter sets with mandatory parameters starts at 100 + $Priority = 100 + + $ParametersTable.GetEnumerator() | ForEach-Object { + if($_.Value.ContainsKey('Mandatory') -and $_.Value.Mandatory -eq '$true') { + $Priority++ + } + } + + # If there are no mandatory parameters, use the parameter count as the priority. + if($Priority -eq 100) { + $Priority = $ParametersCount + } + } $ParameterSetDetail = @{ Description = $FunctionDescription @@ -206,7 +225,7 @@ function Get-SwaggerSpecPathInfo OperationType = $operationType EndpointRelativePath = $EndpointRelativePath PathCommonParameters = $PathCommonParameters - Priority = 100 # Default + Priority = $Priority 'x-ms-pageable' = $x_ms_pageableObject } @@ -228,10 +247,6 @@ function Get-SwaggerSpecPathInfo } } - if ($approximateVerb.StartsWith("List")) { - $ParameterSetDetail.Priority = 0 - } - $commandNames | ForEach-Object { $FunctionDetails = @{} if ($PathFunctionDetails.ContainsKey($_.name)) { @@ -839,7 +854,7 @@ function New-SwaggerPath if ($nonUniqueParameterSets.Length -gt 1) { # Pick the highest priority set among $nonUniqueParameterSets, but really it doesn't matter, cause... # Print warning that this generated cmdlet has ambiguous parameter sets - $defaultParameterSet = $nonUniqueParameterSets | Sort-Object -Property Priority | Select-Object -First 1 + $defaultParameterSet = $nonUniqueParameterSets | Sort-Object {$_.Priority} | Select-Object -First 1 $DefaultParameterSetName = $defaultParameterSet.OperationId $description = $defaultParameterSet.Description $synopsis = $defaultParameterSet.Synopsis diff --git a/PSSwagger/SwaggerUtils.psm1 b/PSSwagger/SwaggerUtils.psm1 index 972e810..8a0f891 100644 --- a/PSSwagger/SwaggerUtils.psm1 +++ b/PSSwagger/SwaggerUtils.psm1 @@ -1402,10 +1402,16 @@ function Get-PathCommandName # This is still empty when a verb match is found that is the entire string, but it might not be worth checking for that case and skipping the below operation $cmdNounSuffix = $UnapprovedVerb.Substring($beginningOfSuffix) # Add command noun suffix only when the current noun doesn't contain it or vice-versa. - if(-not $cmdNoun -or (($cmdNoun -notmatch $cmdNounSuffix) -and ($cmdNounSuffix -notmatch $cmdNoun))) { - $cmdNoun = $cmdNoun + (Get-PascalCasedString -Name $UnapprovedVerb.Substring($beginningOfSuffix)) - } elseif($cmdNounSuffix -match $cmdNoun) { - $cmdNoun = $cmdNounSuffix + if(-not $cmdNoun) { + $cmdNoun = Get-PascalCasedString -Name $cmdNounSuffix + } + elseif(-not $cmdNounSuffix.StartsWith('By', [System.StringComparison]::OrdinalIgnoreCase)) { + if(($cmdNoun -notmatch $cmdNounSuffix) -and ($cmdNounSuffix -notmatch $cmdNoun)) { + $cmdNoun = $cmdNoun + (Get-PascalCasedString -Name $cmdNounSuffix) + } + elseif($cmdNounSuffix -match $cmdNoun) { + $cmdNoun = $cmdNounSuffix + } } } } diff --git a/Tests/PSSwaggerScenario.Tests.ps1 b/Tests/PSSwaggerScenario.Tests.ps1 index 068d68e..2c1d29b 100644 --- a/Tests/PSSwaggerScenario.Tests.ps1 +++ b/Tests/PSSwaggerScenario.Tests.ps1 @@ -223,7 +223,7 @@ Describe "Optional parameter tests" -Tag ScenarioTest { } It "Generates cmdlet using optional path parameters" { - $results = Get-CupcakeByMaker -Flavor "chocolate" -Maker "bob" + $results = Get-Cupcake -Flavor "chocolate" -Maker "bob" $results.Length | should be 1 } @@ -528,10 +528,10 @@ Describe "AzureExtensions" -Tag @('AzureExtension','ScenarioTest') { } It "Test x-ms-paths generated cmdlets" { - $results = Get-CupcakeById -Id 1 + $results = Get-Cupcake -Id 1 $results.Count | should be 1 - $results = Get-CupcakeByFlavor -Flavor 'vanilla' + $results = Get-Cupcake -Flavor 'vanilla' $results.Count | should be 1 } @@ -546,6 +546,22 @@ Describe "AzureExtensions" -Tag @('AzureExtension','ScenarioTest') { $cmdInfo = Should BeNullOrEmpty $ev.FullyQualifiedErrorId | Should Be 'CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand' } + + It "Validate default parameterset name of generated cmdlet" { + $CommandInfo = Get-Command -Name Get-Cupcake + $DefaultParameterSet = 'Cupcake_List' + $ParameterSetNames = @( + $DefaultParameterSet, + 'Cupcake_GetById', + 'Cupcake_GetByFlavor' + ) + $CommandInfo.DefaultParameterSet | Should Be $DefaultParameterSet + $CommandInfo.ParameterSets.Count | Should Be $ParameterSetNames.Count + + $ParameterSetNames | ForEach-Object { + $CommandInfo.ParameterSets.Name -contains $_ | Should Be $true + } + } } AfterAll {