diff --git a/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache b/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache index 684a801a8243..fdd6bda48cef 100644 --- a/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache @@ -121,7 +121,7 @@ function Set-{{{apiNamePrefix}}}Configuration { If ($BaseUrl) { # validate URL $URL = $BaseUrl -as [System.URI] - if (!($URL.AbsoluteURI -ne $null -and $URL.Scheme -match '[http|https]')) { + if (!($null -ne $URL.AbsoluteURI -and $URL.Scheme -match '[http|https]')) { throw "Invalid URL '$($BaseUrl)' cannot be used in the base URL." } @@ -349,15 +349,15 @@ function Get-{{apiNamePrefix}}UrlFromHostSetting { $Hosts = Get-{{apiNamePrefix}}HostSetting # check array index out of bound - if ($Index -lt 0 -or $Index -gt $Hosts.Length) { + if ($Index -lt 0 -or $Index -ge $Hosts.Length) { throw "Invalid index $index when selecting the host. Must be less than $($Hosts.Length)" } - $Host = $Hosts[$Index]; - $Url = $Host["Url"]; + $MyHost = $Hosts[$Index]; + $Url = $MyHost["Url"]; # go through variable and assign a value - foreach ($h in $Host["Variables"].GetEnumerator()) { + foreach ($h in $MyHost["Variables"].GetEnumerator()) { if ($Variables.containsKey($h.Name)) { # check to see if it's in the variables provided by the user if ($h.Value["EnumValues"] -Contains $Variables[$h.Name]) { $Url = $Url.replace("{$($h.Name)}", $Variables[$h.Name]) diff --git a/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 b/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 index aaaed8bda676..67577773b1d2 100644 --- a/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 +++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 @@ -127,7 +127,7 @@ function Set-PSConfiguration { If ($BaseUrl) { # validate URL $URL = $BaseUrl -as [System.URI] - if (!($URL.AbsoluteURI -ne $null -and $URL.Scheme -match '[http|https]')) { + if (!($null -ne $URL.AbsoluteURI -and $URL.Scheme -match '[http|https]')) { throw "Invalid URL '$($BaseUrl)' cannot be used in the base URL." } @@ -365,15 +365,15 @@ function Get-PSUrlFromHostSetting { $Hosts = Get-PSHostSetting # check array index out of bound - if ($Index -lt 0 -or $Index -gt $Hosts.Length) { + if ($Index -lt 0 -or $Index -ge $Hosts.Length) { throw "Invalid index $index when selecting the host. Must be less than $($Hosts.Length)" } - $Host = $Hosts[$Index]; - $Url = $Host["Url"]; + $MyHost = $Hosts[$Index]; + $Url = $MyHost["Url"]; # go through variable and assign a value - foreach ($h in $Host["Variables"].GetEnumerator()) { + foreach ($h in $MyHost["Variables"].GetEnumerator()) { if ($Variables.containsKey($h.Name)) { # check to see if it's in the variables provided by the user if ($h.Value["EnumValues"] -Contains $Variables[$h.Name]) { $Url = $Url.replace("{$($h.Name)}", $Variables[$h.Name]) diff --git a/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 b/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 index 30cd15600d50..adf55dc4bfe4 100644 --- a/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 +++ b/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 @@ -133,8 +133,8 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' { Get-PSUrlFromHostSetting -Index 0 | Should Be "http://petstore.swagger.io:80/v2" Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2" #Get-PSUrlFromHostSetting -Index 2 | Should -Throw -ExceptionType ([RuntimeException]) - #Get-PSUrlFromHostSetting -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2" - #Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080" + #Get-PSUrlFromHostSetting -Index 2 -ErrorAction Stop | Should -Throw "RuntimeException: Invalid index 2 when selecting the host. Must be less than 2" + #Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "1234" } -ErrorAction Stop | Should -Throw "RuntimeException: The variable 'port' in the host URL has invalid value 1234. Must be 80,8080" }