From 5f122f806ba58710415f755fecd00b9d41460e7a Mon Sep 17 00:00:00 2001 From: condorcorde Date: Thu, 4 Apr 2024 17:53:29 +0200 Subject: [PATCH 1/9] Support multipart requests --- .../resources/powershell/api_client.mustache | 115 +++++++---------- .../src/PSOpenAPITools/Private/ApiClient.ps1 | 117 +++++++----------- .../src/PSPetstore/Private/PSApiClient.ps1 | 115 +++++++---------- 3 files changed, 133 insertions(+), 214 deletions(-) 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 6594994bc60..62223b4e7df 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -87,34 +87,6 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() - # include form parameters in the request body - if ($FormParameters -and $FormParameters.Count -gt 0) { - if (![string]::IsNullOrEmpty($MultiPartBoundary)) { - $RequestBody = "" - $LF = "`r`n" - $FormParameters.Keys | ForEach-Object { - $value = $FormParameters[$_] - $isFile = $value.GetType().FullName -eq "System.IO.FileInfo" - - $RequestBody += "--$MultiPartBoundary$LF" - $RequestBody += "Content-Disposition: form-data; name=`"$_`"" - if ($isFile) { - $fileName = $value.Name - $RequestBody += "; filename=`"$fileName`"$LF" - $RequestBody += "Content-Type: application/octet-stream$LF$LF" - $RequestBody += Get-Content -Path $value.FullName - } else { - $RequestBody += "$LF$LF" - $RequestBody += ([string]$value) - } - $RequestBody += "$LF--$MultiPartBoundary" - } - $RequestBody += "--" - } else { - $RequestBody = $FormParameters - } - } - if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { @@ -141,54 +113,55 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } {{/hasHttpSignatureMethods}} + # Use splatting to pass optional parameters + $Params = @{} if ($SkipCertificateCheck -eq $true) { - if ($null -eq $Configuration["Proxy"]) { - # skip certification check, no proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -SkipCertificateCheck + $Params.SkipCertificateCheck = $true + } + if ($null -ne $Configuration["Proxy"]) { + $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) + $Params.ProxyUseDefaultCredentials = $true + } + if ($Multipart) { + if ($PSVersionTable.PSVersion.Major -eq 5) { + # Preset null return values as not supported by Invoke-RestMethod on PS5 + $ResponseHeaders = $null + $ResponseStatusCode = $null } else { - # skip certification check, use proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -SkipCertificateCheck ` - -Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) ` - -ProxyUseDefaultCredentials - } - } else { - if ($null -eq $Configuration["Proxy"]) { - # perform certification check, no proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing - } else { - # perform certification check, use proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) ` - -ProxyUseDefaultCredentials + # Preset return variables + $Params.ResponseHeadersVariable = "ResponseHeaders" + $Params.StatusCodeVariable = "ResponseStatusCode" } } - return @{ - Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] - StatusCode = $Response.StatusCode - Headers = $Response.Headers + # Invoke request + if ($MultiPart) { + $Params.Form = $FormParameters + $Response = Invoke-RestMethod -Uri $UriBuilder.Uri ` + -Method $Method ` + -Headers $HeaderParameters ` + -ErrorAction Stop ` + @Params + + return @{ + Response = $Response + StatusCode = $ResponseStatusCode + Headers = $ResponseHeaders + } + } else { + $Params.Body = $RequestBody + $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` + -Method $Method ` + -Headers $HeaderParameters ` + -ErrorAction Stop ` + -UseBasicParsing ` + @Params + + return @{ + Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] + StatusCode = $Response.StatusCode + Headers = $Response.Headers + } } } 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 b7088aee767..fe76ab4cd69 100644 --- a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 +++ b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 @@ -94,89 +94,62 @@ function Invoke-ApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() - # include form parameters in the request body - if ($FormParameters -and $FormParameters.Count -gt 0) { - if (![string]::IsNullOrEmpty($MultiPartBoundary)) { - $RequestBody = "" - $LF = "`r`n" - $FormParameters.Keys | ForEach-Object { - $value = $FormParameters[$_] - $isFile = $value.GetType().FullName -eq "System.IO.FileInfo" - - $RequestBody += "--$MultiPartBoundary$LF" - $RequestBody += "Content-Disposition: form-data; name=`"$_`"" - if ($isFile) { - $fileName = $value.Name - $RequestBody += "; filename=`"$fileName`"$LF" - $RequestBody += "Content-Type: application/octet-stream$LF$LF" - $RequestBody += Get-Content -Path $value.FullName - } else { - $RequestBody += "$LF$LF" - $RequestBody += ([string]$value) - } - $RequestBody += "$LF--$MultiPartBoundary" - } - $RequestBody += "--" - } else { - $RequestBody = $FormParameters - } - } - - if ($Body -or $IsBodyNullable) { + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { $RequestBody = "null" } } + # Use splatting to pass optional parameters + $Params = @{} if ($SkipCertificateCheck -eq $true) { - if ($null -eq $Configuration["Proxy"]) { - # skip certification check, no proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -SkipCertificateCheck + $Params.SkipCertificateCheck = $true + } + if ($null -ne $Configuration["Proxy"]) { + $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) + $Params.ProxyUseDefaultCredentials = $true + } + if ($Multipart) { + if ($PSVersionTable.PSVersion.Major -eq 5) { + # Preset null return values as not supported by Invoke-RestMethod on PS5 + $ResponseHeaders = $null + $ResponseStatusCode = $null } else { - # skip certification check, use proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -SkipCertificateCheck ` - -Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) ` - -ProxyUseDefaultCredentials - } - } else { - if ($null -eq $Configuration["Proxy"]) { - # perform certification check, no proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing - } else { - # perform certification check, use proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) ` - -ProxyUseDefaultCredentials + # Preset return variables + $Params.ResponseHeadersVariable = "ResponseHeaders" + $Params.StatusCodeVariable = "ResponseStatusCode" } } - return @{ - Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] - StatusCode = $Response.StatusCode - Headers = $Response.Headers + # Invoke request + if ($MultiPart) { + $Params.Form = $FormParameters + $Response = Invoke-RestMethod -Uri $UriBuilder.Uri ` + -Method $Method ` + -Headers $HeaderParameters ` + -ErrorAction Stop ` + @Params + + return @{ + Response = $Response + StatusCode = $ResponseStatusCode + Headers = $ResponseHeaders + } + } else { + $Params.Body = $RequestBody + $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` + -Method $Method ` + -Headers $HeaderParameters ` + -ErrorAction Stop ` + -UseBasicParsing ` + @Params + + return @{ + Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] + StatusCode = $Response.StatusCode + Headers = $Response.Headers + } } } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 index 31d24ee50b4..888711216e7 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 @@ -93,34 +93,6 @@ function Invoke-PSApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() - # include form parameters in the request body - if ($FormParameters -and $FormParameters.Count -gt 0) { - if (![string]::IsNullOrEmpty($MultiPartBoundary)) { - $RequestBody = "" - $LF = "`r`n" - $FormParameters.Keys | ForEach-Object { - $value = $FormParameters[$_] - $isFile = $value.GetType().FullName -eq "System.IO.FileInfo" - - $RequestBody += "--$MultiPartBoundary$LF" - $RequestBody += "Content-Disposition: form-data; name=`"$_`"" - if ($isFile) { - $fileName = $value.Name - $RequestBody += "; filename=`"$fileName`"$LF" - $RequestBody += "Content-Type: application/octet-stream$LF$LF" - $RequestBody += Get-Content -Path $value.FullName - } else { - $RequestBody += "$LF$LF" - $RequestBody += ([string]$value) - } - $RequestBody += "$LF--$MultiPartBoundary" - } - $RequestBody += "--" - } else { - $RequestBody = $FormParameters - } - } - if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { @@ -145,54 +117,55 @@ function Invoke-PSApiClient { } } + # Use splatting to pass optional parameters + $Params = @{} if ($SkipCertificateCheck -eq $true) { - if ($null -eq $Configuration["Proxy"]) { - # skip certification check, no proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -SkipCertificateCheck + $Params.SkipCertificateCheck = $true + } + if ($null -ne $Configuration["Proxy"]) { + $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) + $Params.ProxyUseDefaultCredentials = $true + } + if ($Multipart) { + if ($PSVersionTable.PSVersion.Major -eq 5) { + # Preset null return values as not supported by Invoke-RestMethod on PS5 + $ResponseHeaders = $null + $ResponseStatusCode = $null } else { - # skip certification check, use proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -SkipCertificateCheck ` - -Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) ` - -ProxyUseDefaultCredentials - } - } else { - if ($null -eq $Configuration["Proxy"]) { - # perform certification check, no proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing - } else { - # perform certification check, use proxy - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -Body $RequestBody ` - -ErrorAction Stop ` - -UseBasicParsing ` - -Proxy $Configuration["Proxy"].GetProxy($UriBuilder.Uri) ` - -ProxyUseDefaultCredentials + # Preset return variables + $Params.ResponseHeadersVariable = "ResponseHeaders" + $Params.StatusCodeVariable = "ResponseStatusCode" } } - return @{ - Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] - StatusCode = $Response.StatusCode - Headers = $Response.Headers + # Invoke request + if ($MultiPart) { + $Params.Form = $FormParameters + $Response = Invoke-RestMethod -Uri $UriBuilder.Uri ` + -Method $Method ` + -Headers $HeaderParameters ` + -ErrorAction Stop ` + @Params + + return @{ + Response = $Response + StatusCode = $ResponseStatusCode + Headers = $ResponseHeaders + } + } else { + $Params.Body = $RequestBody + $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` + -Method $Method ` + -Headers $HeaderParameters ` + -ErrorAction Stop ` + -UseBasicParsing ` + @Params + + return @{ + Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] + StatusCode = $Response.StatusCode + Headers = $Response.Headers + } } } From b5868cedc7f6f7ab167fdd313ef2be0ee8fef780 Mon Sep 17 00:00:00 2001 From: condorcorde Date: Thu, 4 Apr 2024 18:02:12 +0200 Subject: [PATCH 2/9] Further code optimization --- .../resources/powershell/api_client.mustache | 28 ++++++++----------- .../src/PSOpenAPITools/Private/ApiClient.ps1 | 28 ++++++++----------- .../src/PSPetstore/Private/PSApiClient.ps1 | 28 ++++++++----------- 3 files changed, 33 insertions(+), 51 deletions(-) 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 62223b4e7df..a9b2d04c9a8 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -113,8 +113,12 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } {{/hasHttpSignatureMethods}} - # Use splatting to pass optional parameters + # Use splatting to pass parameters $Params = @{} + $Params.Uri = $UriBuilder.Uri + $Params.Method = $Method + $Params.Headers = $HeaderParameters + $Params.ErrorAction = 'Stop' if ($SkipCertificateCheck -eq $true) { $Params.SkipCertificateCheck = $true } @@ -122,7 +126,9 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) $Params.ProxyUseDefaultCredentials = $true } - if ($Multipart) { + + # Invoke request + if ($MultiPart) { if ($PSVersionTable.PSVersion.Major -eq 5) { # Preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null @@ -132,16 +138,8 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $Params.ResponseHeadersVariable = "ResponseHeaders" $Params.StatusCodeVariable = "ResponseStatusCode" } - } - - # Invoke request - if ($MultiPart) { $Params.Form = $FormParameters - $Response = Invoke-RestMethod -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -ErrorAction Stop ` - @Params + $Response = Invoke-RestMethod @Params return @{ Response = $Response @@ -150,12 +148,8 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } } else { $Params.Body = $RequestBody - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -ErrorAction Stop ` - -UseBasicParsing ` - @Params + $Params.UseBasicParsing = $true + $Response = Invoke-WebRequest @Params return @{ Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] 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 fe76ab4cd69..253b4506fe2 100644 --- a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 +++ b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 @@ -101,8 +101,12 @@ function Invoke-ApiClient { } } - # Use splatting to pass optional parameters + # Use splatting to pass parameters $Params = @{} + $Params.Uri = $UriBuilder.Uri + $Params.Method = $Method + $Params.Headers = $HeaderParameters + $Params.ErrorAction = 'Stop' if ($SkipCertificateCheck -eq $true) { $Params.SkipCertificateCheck = $true } @@ -110,7 +114,9 @@ function Invoke-ApiClient { $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) $Params.ProxyUseDefaultCredentials = $true } - if ($Multipart) { + + # Invoke request + if ($MultiPart) { if ($PSVersionTable.PSVersion.Major -eq 5) { # Preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null @@ -120,16 +126,8 @@ function Invoke-ApiClient { $Params.ResponseHeadersVariable = "ResponseHeaders" $Params.StatusCodeVariable = "ResponseStatusCode" } - } - - # Invoke request - if ($MultiPart) { $Params.Form = $FormParameters - $Response = Invoke-RestMethod -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -ErrorAction Stop ` - @Params + $Response = Invoke-RestMethod @Params return @{ Response = $Response @@ -138,12 +136,8 @@ function Invoke-ApiClient { } } else { $Params.Body = $RequestBody - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -ErrorAction Stop ` - -UseBasicParsing ` - @Params + $Params.UseBasicParsing = $true + $Response = Invoke-WebRequest @Params return @{ Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] diff --git a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 index 888711216e7..82ae93823a8 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 @@ -117,8 +117,12 @@ function Invoke-PSApiClient { } } - # Use splatting to pass optional parameters + # Use splatting to pass parameters $Params = @{} + $Params.Uri = $UriBuilder.Uri + $Params.Method = $Method + $Params.Headers = $HeaderParameters + $Params.ErrorAction = 'Stop' if ($SkipCertificateCheck -eq $true) { $Params.SkipCertificateCheck = $true } @@ -126,7 +130,9 @@ function Invoke-PSApiClient { $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) $Params.ProxyUseDefaultCredentials = $true } - if ($Multipart) { + + # Invoke request + if ($MultiPart) { if ($PSVersionTable.PSVersion.Major -eq 5) { # Preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null @@ -136,16 +142,8 @@ function Invoke-PSApiClient { $Params.ResponseHeadersVariable = "ResponseHeaders" $Params.StatusCodeVariable = "ResponseStatusCode" } - } - - # Invoke request - if ($MultiPart) { $Params.Form = $FormParameters - $Response = Invoke-RestMethod -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -ErrorAction Stop ` - @Params + $Response = Invoke-RestMethod @Params return @{ Response = $Response @@ -154,12 +152,8 @@ function Invoke-PSApiClient { } } else { $Params.Body = $RequestBody - $Response = Invoke-WebRequest -Uri $UriBuilder.Uri ` - -Method $Method ` - -Headers $HeaderParameters ` - -ErrorAction Stop ` - -UseBasicParsing ` - @Params + $Params.UseBasicParsing = $true + $Response = Invoke-WebRequest @Params return @{ Response = DeserializeResponse -Response $Response.Content -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"] From 3af8d66f8404c670621116669b1aecfe091fcde1 Mon Sep 17 00:00:00 2001 From: condorcorde Date: Thu, 4 Apr 2024 19:22:27 +0200 Subject: [PATCH 3/9] Update ApiClient.ps1 --- .../powershell/src/PSOpenAPITools/Private/ApiClient.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 253b4506fe2..df7bfd16438 100644 --- a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 +++ b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 @@ -94,7 +94,7 @@ function Invoke-ApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() - if ($Body -or $IsBodyNullable) { + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { $RequestBody = "null" From 8dc1c81c687ba48279567f6fa8328eaecb7ab5c7 Mon Sep 17 00:00:00 2001 From: condorcorde Date: Thu, 4 Apr 2024 22:05:40 +0200 Subject: [PATCH 4/9] Set $Multipart --- .../src/main/resources/powershell/api_client.mustache | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 a9b2d04c9a8..96ad51bae69 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -61,9 +61,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType if ($ContentType -eq 'multipart/form-data') { - [string]$MultiPartBoundary = [System.Guid]::NewGuid() - $MultiPartBoundary = "---------------------------$MultiPartBoundary" - $HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary" + $MultiPart = $true } } From 6e7d9677ba594747a8b327159c18e8397b5c376a Mon Sep 17 00:00:00 2001 From: condorcorde Date: Fri, 5 Apr 2024 20:04:35 +0200 Subject: [PATCH 5/9] Corrections after petstore tests --- .../resources/powershell/api_client.mustache | 31 +++++++++++----- .../src/PSOpenAPITools/Private/ApiClient.ps1 | 35 ++++++++++++------- .../src/PSPetstore/Private/PSApiClient.ps1 | 35 ++++++++++++------- 3 files changed, 68 insertions(+), 33 deletions(-) mode change 100644 => 100755 samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 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 96ad51bae69..bb0ef5075e2 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -85,6 +85,11 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() + # include form parameters in the request body + if ($FormParameters -and $FormParameters.Count -gt 0) { + $RequestBody = $FormParameters + } + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { @@ -111,28 +116,37 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } {{/hasHttpSignatureMethods}} - # Use splatting to pass parameters + # 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' - if ($SkipCertificateCheck -eq $true) { + # SkipCertificateCheck not defined for PS5 + if ($SkipCertificateCheck -eq $true -and $PSMajorVersion -gt 5) { $Params.SkipCertificateCheck = $true } + # do not set proxy if it is null or same as target Uri if ($null -ne $Configuration["Proxy"]) { - $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) - $Params.ProxyUseDefaultCredentials = $true + $proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) + if ($null -ne $proxy -and $proxy.AbsoluteUri -ne $UriBuilder.Uri) { + $Params.Proxy = $proxy.AbsoluteUri + $Params.ProxyUseDefaultCredentials = $true + } } - # Invoke request + # use Invoke-RestApi if Content-Type is 'multipart/form-data', Invoke-WebRequest otherwise if ($MultiPart) { - if ($PSVersionTable.PSVersion.Major -eq 5) { - # Preset null return values as not supported by Invoke-RestMethod on PS5 + if ($PSMajorVersion -eq 5) { + # preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null $ResponseStatusCode = $null } else { - # Preset return variables + # preset return variables $Params.ResponseHeadersVariable = "ResponseHeaders" $Params.StatusCodeVariable = "ResponseStatusCode" } @@ -145,7 +159,6 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { Headers = $ResponseHeaders } } else { - $Params.Body = $RequestBody $Params.UseBasicParsing = $true $Response = Invoke-WebRequest @Params 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 df7bfd16438..240631615ac 100644 --- a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 +++ b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 @@ -68,9 +68,7 @@ function Invoke-ApiClient { if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType if ($ContentType -eq 'multipart/form-data') { - [string]$MultiPartBoundary = [System.Guid]::NewGuid() - $MultiPartBoundary = "---------------------------$MultiPartBoundary" - $HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary" + $MultiPart = $true } } @@ -94,6 +92,11 @@ function Invoke-ApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() + # include form parameters in the request body + if ($FormParameters -and $FormParameters.Count -gt 0) { + $RequestBody = $FormParameters + } + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { @@ -101,28 +104,37 @@ function Invoke-ApiClient { } } - # Use splatting to pass parameters + # 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' - if ($SkipCertificateCheck -eq $true) { + # SkipCertificateCheck not defined for PS5 + if ($SkipCertificateCheck -eq $true -and $PSMajorVersion -gt 5) { $Params.SkipCertificateCheck = $true } + # do not set proxy if it is null or same as target Uri if ($null -ne $Configuration["Proxy"]) { - $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) - $Params.ProxyUseDefaultCredentials = $true + $proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) + if ($null -ne $proxy -and $proxy.AbsoluteUri -ne $UriBuilder.Uri) { + $Params.Proxy = $proxy.AbsoluteUri + $Params.ProxyUseDefaultCredentials = $true + } } - # Invoke request + # use Invoke-RestApi if Content-Type is 'multipart/form-data', Invoke-WebRequest otherwise if ($MultiPart) { - if ($PSVersionTable.PSVersion.Major -eq 5) { - # Preset null return values as not supported by Invoke-RestMethod on PS5 + if ($PSMajorVersion -eq 5) { + # preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null $ResponseStatusCode = $null } else { - # Preset return variables + # preset return variables $Params.ResponseHeadersVariable = "ResponseHeaders" $Params.StatusCodeVariable = "ResponseStatusCode" } @@ -135,7 +147,6 @@ 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 old mode 100644 new mode 100755 index 82ae93823a8..73ef74147e6 --- a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 @@ -67,9 +67,7 @@ function Invoke-PSApiClient { if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType if ($ContentType -eq 'multipart/form-data') { - [string]$MultiPartBoundary = [System.Guid]::NewGuid() - $MultiPartBoundary = "---------------------------$MultiPartBoundary" - $HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary" + $MultiPart = $true } } @@ -93,6 +91,11 @@ function Invoke-PSApiClient { $UriBuilder = [System.UriBuilder]($RequestUri) $UriBuilder.Query = $HttpValues.ToString() + # include form parameters in the request body + if ($FormParameters -and $FormParameters.Count -gt 0) { + $RequestBody = $FormParameters + } + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { @@ -117,28 +120,37 @@ function Invoke-PSApiClient { } } - # Use splatting to pass parameters + # 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' - if ($SkipCertificateCheck -eq $true) { + # SkipCertificateCheck not defined for PS5 + if ($SkipCertificateCheck -eq $true -and $PSMajorVersion -gt 5) { $Params.SkipCertificateCheck = $true } + # do not set proxy if it is null or same as target Uri if ($null -ne $Configuration["Proxy"]) { - $Params.Proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) - $Params.ProxyUseDefaultCredentials = $true + $proxy = $Configuration["Proxy"].GetProxy($UriBuilder.Uri) + if ($null -ne $proxy -and $proxy.AbsoluteUri -ne $UriBuilder.Uri) { + $Params.Proxy = $proxy.AbsoluteUri + $Params.ProxyUseDefaultCredentials = $true + } } - # Invoke request + # use Invoke-RestApi if Content-Type is 'multipart/form-data', Invoke-WebRequest otherwise if ($MultiPart) { - if ($PSVersionTable.PSVersion.Major -eq 5) { - # Preset null return values as not supported by Invoke-RestMethod on PS5 + if ($PSMajorVersion -eq 5) { + # preset null return values as not supported by Invoke-RestMethod on PS5 $ResponseHeaders = $null $ResponseStatusCode = $null } else { - # Preset return variables + # preset return variables $Params.ResponseHeadersVariable = "ResponseHeaders" $Params.StatusCodeVariable = "ResponseStatusCode" } @@ -151,7 +163,6 @@ function Invoke-PSApiClient { Headers = $ResponseHeaders } } else { - $Params.Body = $RequestBody $Params.UseBasicParsing = $true $Response = Invoke-WebRequest @Params From a9227a94dc9c2854662846144f6140763d4094d5 Mon Sep 17 00:00:00 2001 From: condorcorde Date: Sat, 6 Apr 2024 11:20:38 +0200 Subject: [PATCH 6/9] Update api_client.mustache --- .../resources/powershell/api_client.mustache | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) 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 bb0ef5075e2..feb57c7f099 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -36,7 +36,9 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $Configuration = Get-{{{apiNamePrefix}}}Configuration $RequestUri = $Configuration["BaseUrl"] + $Uri + # should make sure that SkipCertificateCheck is not set for PowerShell 5 $SkipCertificateCheck = $Configuration["SkipCertificateCheck"] + $Proxy = $Configuration["Proxy"] # cookie parameters foreach ($Parameter in $CookieParameters.GetEnumerator()) { @@ -56,7 +58,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $HeaderParameters['Accept'] = $Accept } - [string]$MultiPartBoundary = $null + # Content-Type and multipart handling $ContentType= SelectHeaders -Headers $ContentTypes if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType @@ -116,32 +118,29 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } {{/hasHttpSignatureMethods}} - # 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 @@ -159,6 +158,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { Headers = $ResponseHeaders } } else { + $Params.Body = $RequestBody $Params.UseBasicParsing = $true $Response = Invoke-WebRequest @Params From a636968b225d7afc75ec262aa522355bd542bdf3 Mon Sep 17 00:00:00 2001 From: condorcorde Date: Sat, 6 Apr 2024 15:56:25 +0200 Subject: [PATCH 7/9] Update api_client.nustache --- .../src/main/resources/powershell/api_client.mustache | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 feb57c7f099..2c40d93977a 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -36,6 +36,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $Configuration = Get-{{{apiNamePrefix}}}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"] @@ -59,7 +60,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } # Content-Type and multipart handling - $ContentType= SelectHeaders -Headers $ContentTypes + $ContentType = SelectHeaders -Headers $ContentTypes if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType if ($ContentType -eq 'multipart/form-data') { @@ -68,7 +69,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { } # add default headers if any - foreach ($header in $Configuration["DefaultHeaders"].GetEnumerator()) { + foreach ($header in $DefaultHeaders.GetEnumerator()) { $HeaderParameters[$header.Name] = $header.Value } From b14ff289b6eb9cfe607f55686aaa29a58340b7f8 Mon Sep 17 00:00:00 2001 From: condorcorde Date: Sat, 6 Apr 2024 21:00:28 +0200 Subject: [PATCH 8/9] Update api_client.mustache --- .../resources/powershell/api_client.mustache | 2 +- .../src/PSOpenAPITools/Private/ApiClient.ps1 | 33 ++++++++++--------- .../src/PSPetstore/Private/PSApiClient.ps1 | 33 ++++++++++--------- 3 files changed, 35 insertions(+), 33 deletions(-) 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 From 5d7a70a972a9bbafadab8de48cf7908d0e6e20ac Mon Sep 17 00:00:00 2001 From: condorcorde Date: Sun, 7 Apr 2024 11:37:34 +0200 Subject: [PATCH 9/9] Tabs removed --- .../src/main/resources/powershell/api_client.mustache | 2 +- .../powershell/src/PSOpenAPITools/Private/ApiClient.ps1 | 2 +- .../petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 e9c5df5648f..f760c7d5f56 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -132,7 +132,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { if ($null -ne $Proxy) { $effectiveProxy = $Proxy.GetProxy($UriBuilder.Uri) - # do not set proxy if it is null or same as target 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 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 10f81382c88..06bd189facf 100644 --- a/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 +++ b/samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1 @@ -120,7 +120,7 @@ function Invoke-ApiClient { if ($null -ne $Proxy) { $effectiveProxy = $Proxy.GetProxy($UriBuilder.Uri) - # do not set proxy if it is null or same as target 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 diff --git a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 index c3e71fa7894..3b60185eee7 100755 --- a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 @@ -136,7 +136,7 @@ function Invoke-PSApiClient { if ($null -ne $Proxy) { $effectiveProxy = $Proxy.GetProxy($UriBuilder.Uri) - # do not set proxy if it is null or same as target 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