Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions PSSwagger/Paths.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -206,7 +225,7 @@ function Get-SwaggerSpecPathInfo
OperationType = $operationType
EndpointRelativePath = $EndpointRelativePath
PathCommonParameters = $PathCommonParameters
Priority = 100 # Default
Priority = $Priority
'x-ms-pageable' = $x_ms_pageableObject
}

Expand All @@ -228,10 +247,6 @@ function Get-SwaggerSpecPathInfo
}
}

if ($approximateVerb.StartsWith("List")) {
$ParameterSetDetail.Priority = 0
}

$commandNames | ForEach-Object {
$FunctionDetails = @{}
if ($PathFunctionDetails.ContainsKey($_.name)) {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions PSSwagger/SwaggerUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-not $cmdNounSuffix.StartsWith('By', [System.StringComparison]::OrdinalIgnoreCase) [](start = 23, length = 82)

Quick note for anyone looking at this change - this is temporary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR resolves the feedback received from Azure PS team.
In future, if we come up with more heuristics, the entire functionality of the this Get-PathCommandName can be changed.

if(($cmdNoun -notmatch $cmdNounSuffix) -and ($cmdNounSuffix -notmatch $cmdNoun)) {
$cmdNoun = $cmdNoun + (Get-PascalCasedString -Name $cmdNounSuffix)
}
elseif($cmdNounSuffix -match $cmdNoun) {
$cmdNoun = $cmdNounSuffix
}
}
}
}
Expand Down
22 changes: 19 additions & 3 deletions Tests/PSSwaggerScenario.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down