diff --git a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache index 2c40d93977a..e9c5df5648f 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -91,7 +91,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { # include form parameters in the request body if ($FormParameters -and $FormParameters.Count -gt 0) { $RequestBody = $FormParameters - } + } if ($Body -or $IsBodyNullable) { $RequestBody = $Body diff --git a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 index 240631615ac..10f81382c88 100644 --- a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 +++ b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 @@ -43,7 +43,10 @@ function Invoke-ApiClient { $Configuration = Get-Configuration $RequestUri = $Configuration["BaseUrl"] + $Uri + $DefaultHeaders = $Configuration["DefaultHeaders"] + # should make sure that SkipCertificateCheck is not set for PowerShell 5 $SkipCertificateCheck = $Configuration["SkipCertificateCheck"] + $Proxy = $Configuration["Proxy"] # cookie parameters foreach ($Parameter in $CookieParameters.GetEnumerator()) { @@ -63,8 +66,8 @@ function Invoke-ApiClient { $HeaderParameters['Accept'] = $Accept } - [string]$MultiPartBoundary = $null - $ContentType= SelectHeaders -Headers $ContentTypes + # Content-Type and multipart handling + $ContentType = SelectHeaders -Headers $ContentTypes if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType if ($ContentType -eq 'multipart/form-data') { @@ -73,7 +76,7 @@ function Invoke-ApiClient { } # add default headers if any - foreach ($header in $Configuration["DefaultHeaders"].GetEnumerator()) { + foreach ($header in $DefaultHeaders.GetEnumerator()) { $HeaderParameters[$header.Name] = $header.Value } @@ -95,7 +98,7 @@ function Invoke-ApiClient { # include form parameters in the request body if ($FormParameters -and $FormParameters.Count -gt 0) { $RequestBody = $FormParameters - } + } if ($Body -or $IsBodyNullable) { $RequestBody = $Body @@ -104,32 +107,29 @@ function Invoke-ApiClient { } } - # syntax must be adapted to version - $PSMajorVersion = $PSVersionTable.PSVersion.Major - # use splatting to pass parameters $Params = @{} $Params.Uri = $UriBuilder.Uri $Params.Method = $Method $Params.Headers = $HeaderParameters - $Params.Body = $RequestBody $Params.ErrorAction = 'Stop' - # SkipCertificateCheck not defined for PS5 - if ($SkipCertificateCheck -eq $true -and $PSMajorVersion -gt 5) { + + if ($SkipCertificateCheck -eq $true) { $Params.SkipCertificateCheck = $true } - # do not set proxy if it is null or same as target Uri - if ($null -ne $Configuration["Proxy"]) { - $proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) - if ($null -ne $proxy -and $proxy.AbsoluteUri -ne $UriBuilder.Uri) { - $Params.Proxy = $proxy.AbsoluteUri + + if ($null -ne $Proxy) { + $effectiveProxy = $Proxy.GetProxy($UriBuilder.Uri) + # do not set proxy if it is null or same as target Uri + if ($null -ne $effectiveProxy -and $effectiveProxy.AbsoluteUri -ne $UriBuilder.Uri) { + $Params.Proxy = $effectiveProxy.AbsoluteUri $Params.ProxyUseDefaultCredentials = $true } } # use Invoke-RestApi if Content-Type is 'multipart/form-data', Invoke-WebRequest otherwise if ($MultiPart) { - if ($PSMajorVersion -eq 5) { + if ($PSVersionTable.PSVersion.Major -eq 5) { # preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null $ResponseStatusCode = $null @@ -147,6 +147,7 @@ function Invoke-ApiClient { Headers = $ResponseHeaders } } else { + $Params.Body = $RequestBody $Params.UseBasicParsing = $true $Response = Invoke-WebRequest @Params diff --git a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 index 73ef74147e6..c3e71fa7894 100755 --- a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 @@ -42,7 +42,10 @@ function Invoke-PSApiClient { $Configuration = Get-PSConfiguration $RequestUri = $Configuration["BaseUrl"] + $Uri + $DefaultHeaders = $Configuration["DefaultHeaders"] + # should make sure that SkipCertificateCheck is not set for PowerShell 5 $SkipCertificateCheck = $Configuration["SkipCertificateCheck"] + $Proxy = $Configuration["Proxy"] # cookie parameters foreach ($Parameter in $CookieParameters.GetEnumerator()) { @@ -62,8 +65,8 @@ function Invoke-PSApiClient { $HeaderParameters['Accept'] = $Accept } - [string]$MultiPartBoundary = $null - $ContentType= SelectHeaders -Headers $ContentTypes + # Content-Type and multipart handling + $ContentType = SelectHeaders -Headers $ContentTypes if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType if ($ContentType -eq 'multipart/form-data') { @@ -72,7 +75,7 @@ function Invoke-PSApiClient { } # add default headers if any - foreach ($header in $Configuration["DefaultHeaders"].GetEnumerator()) { + foreach ($header in $DefaultHeaders.GetEnumerator()) { $HeaderParameters[$header.Name] = $header.Value } @@ -94,7 +97,7 @@ function Invoke-PSApiClient { # include form parameters in the request body if ($FormParameters -and $FormParameters.Count -gt 0) { $RequestBody = $FormParameters - } + } if ($Body -or $IsBodyNullable) { $RequestBody = $Body @@ -120,32 +123,29 @@ function Invoke-PSApiClient { } } - # syntax must be adapted to version - $PSMajorVersion = $PSVersionTable.PSVersion.Major - # use splatting to pass parameters $Params = @{} $Params.Uri = $UriBuilder.Uri $Params.Method = $Method $Params.Headers = $HeaderParameters - $Params.Body = $RequestBody $Params.ErrorAction = 'Stop' - # SkipCertificateCheck not defined for PS5 - if ($SkipCertificateCheck -eq $true -and $PSMajorVersion -gt 5) { + + if ($SkipCertificateCheck -eq $true) { $Params.SkipCertificateCheck = $true } - # do not set proxy if it is null or same as target Uri - if ($null -ne $Configuration["Proxy"]) { - $proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) - if ($null -ne $proxy -and $proxy.AbsoluteUri -ne $UriBuilder.Uri) { - $Params.Proxy = $proxy.AbsoluteUri + + if ($null -ne $Proxy) { + $effectiveProxy = $Proxy.GetProxy($UriBuilder.Uri) + # do not set proxy if it is null or same as target Uri + if ($null -ne $effectiveProxy -and $effectiveProxy.AbsoluteUri -ne $UriBuilder.Uri) { + $Params.Proxy = $effectiveProxy.AbsoluteUri $Params.ProxyUseDefaultCredentials = $true } } # use Invoke-RestApi if Content-Type is 'multipart/form-data', Invoke-WebRequest otherwise if ($MultiPart) { - if ($PSMajorVersion -eq 5) { + if ($PSVersionTable.PSVersion.Major -eq 5) { # preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null $ResponseStatusCode = $null @@ -163,6 +163,7 @@ function Invoke-PSApiClient { Headers = $ResponseHeaders } } else { + $Params.Body = $RequestBody $Params.UseBasicParsing = $true $Response = Invoke-WebRequest @Params