Skip to content

Commit 5a7f050

Browse files
authored
[PowerShell] Support multiple types in Accept header (#17765)
* Update api_client.mustache Allow Accept header to contain multiple MIME types * Changes after build * Update api_client.mustache Return JSON MIME first on -Multiple -JsonFirst * updated PetStore files
1 parent 795f079 commit 5a7f050

3 files changed

Lines changed: 96 additions & 36 deletions

File tree

modules/openapi-generator/src/main/resources/powershell/api_client.mustache

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
5151
}
5252

5353
# accept, content-type headers
54-
$Accept = SelectHeaders -Headers $Accepts
54+
$Accept = SelectHeaders -Headers $Accepts -Multiple -JsonFirst
5555
if ($Accept) {
5656
$HeaderParameters['Accept'] = $Accept
5757
}
@@ -115,8 +115,6 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
115115
}
116116
}
117117

118-
119-
120118
if ($Body -or $IsBodyNullable) {
121119
$RequestBody = $Body
122120
if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) {
@@ -194,24 +192,46 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
194192
}
195193
}
196194

197-
# Select JSON MIME if present, otherwise choose the first one if available
195+
# Filter MIME types for Accept:/Content-Type: headers
198196
function SelectHeaders {
199197
Param(
200198
[Parameter(Mandatory)]
201199
[AllowEmptyCollection()]
202-
[String[]]$Headers
200+
[String[]]$Headers,
201+
[Parameter(Mandatory=$false)]
202+
[switch]$Multiple,
203+
[Parameter(Mandatory=$false)]
204+
[switch]$JsonFirst
203205
)
204206
205-
foreach ($Header in $Headers) {
206-
if (IsJsonMIME -MIME $Header) {
207-
return $Header
208-
}
209-
}
210-
207+
# if no MIME type is provided return null
211208
if (!($Headers) -or $Headers.Count -eq 0) {
212209
return $null
210+
}
211+
212+
if ($Multiple) {
213+
# return multiple MIME types (for Accept: header)
214+
if ($JsonFirst) {
215+
# sort input to return JSON MIME types first
216+
$mimeHeaders = @()
217+
$otherHeaders = @()
218+
foreach ($Header in $Headers) {
219+
if (IsJsonMIME -MIME $Header) {
220+
$mimeHeaders += $Header
221+
} else {
222+
$otherHeaders += $Header
223+
}
224+
}
225+
$Headers = $($mimeHeaders; $otherHeaders)
226+
}
227+
return [string]::Join(', ', $Headers) # join multiple types if they are provided
213228
} else {
214-
return $Headers[0] # return the first one
229+
foreach ($Header in $Headers) {
230+
if (IsJsonMIME -MIME $Header) {
231+
return $Header # return the first type matching a JSON MIME
232+
}
233+
}
234+
return $Headers[0] # else return the first one
215235
}
216236
}
217237

samples/client/echo_api/powershell/src/PSOpenAPITools/Private/ApiClient.ps1

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function Invoke-ApiClient {
5858
}
5959

6060
# accept, content-type headers
61-
$Accept = SelectHeaders -Headers $Accepts
61+
$Accept = SelectHeaders -Headers $Accepts -Multiple -JsonFirst
6262
if ($Accept) {
6363
$HeaderParameters['Accept'] = $Accept
6464
}
@@ -122,8 +122,6 @@ function Invoke-ApiClient {
122122
}
123123
}
124124

125-
126-
127125
if ($Body -or $IsBodyNullable) {
128126
$RequestBody = $Body
129127
if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) {
@@ -182,24 +180,46 @@ function Invoke-ApiClient {
182180
}
183181
}
184182

185-
# Select JSON MIME if present, otherwise choose the first one if available
183+
# Filter MIME types for Accept:/Content-Type: headers
186184
function SelectHeaders {
187185
Param(
188186
[Parameter(Mandatory)]
189187
[AllowEmptyCollection()]
190-
[String[]]$Headers
188+
[String[]]$Headers,
189+
[Parameter(Mandatory=$false)]
190+
[switch]$Multiple,
191+
[Parameter(Mandatory=$false)]
192+
[switch]$JsonFirst
191193
)
192194

193-
foreach ($Header in $Headers) {
194-
if (IsJsonMIME -MIME $Header) {
195-
return $Header
196-
}
197-
}
198-
195+
# if no MIME type is provided return null
199196
if (!($Headers) -or $Headers.Count -eq 0) {
200197
return $null
198+
}
199+
200+
if ($Multiple) {
201+
# return multiple MIME types (for Accept: header)
202+
if ($JsonFirst) {
203+
# sort input to return JSON MIME types first
204+
$mimeHeaders = @()
205+
$otherHeaders = @()
206+
foreach ($Header in $Headers) {
207+
if (IsJsonMIME -MIME $Header) {
208+
$mimeHeaders += $Header
209+
} else {
210+
$otherHeaders += $Header
211+
}
212+
}
213+
$Headers = $($mimeHeaders; $otherHeaders)
214+
}
215+
return [string]::Join(', ', $Headers) # join multiple types if they are provided
201216
} else {
202-
return $Headers[0] # return the first one
217+
foreach ($Header in $Headers) {
218+
if (IsJsonMIME -MIME $Header) {
219+
return $Header # return the first type matching a JSON MIME
220+
}
221+
}
222+
return $Headers[0] # else return the first one
203223
}
204224
}
205225

samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function Invoke-PSApiClient {
5757
}
5858

5959
# accept, content-type headers
60-
$Accept = SelectHeaders -Headers $Accepts
60+
$Accept = SelectHeaders -Headers $Accepts -Multiple -JsonFirst
6161
if ($Accept) {
6262
$HeaderParameters['Accept'] = $Accept
6363
}
@@ -121,8 +121,6 @@ function Invoke-PSApiClient {
121121
}
122122
}
123123

124-
125-
126124
if ($Body -or $IsBodyNullable) {
127125
$RequestBody = $Body
128126
if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) {
@@ -198,24 +196,46 @@ function Invoke-PSApiClient {
198196
}
199197
}
200198

201-
# Select JSON MIME if present, otherwise choose the first one if available
199+
# Filter MIME types for Accept:/Content-Type: headers
202200
function SelectHeaders {
203201
Param(
204202
[Parameter(Mandatory)]
205203
[AllowEmptyCollection()]
206-
[String[]]$Headers
204+
[String[]]$Headers,
205+
[Parameter(Mandatory=$false)]
206+
[switch]$Multiple,
207+
[Parameter(Mandatory=$false)]
208+
[switch]$JsonFirst
207209
)
208210

209-
foreach ($Header in $Headers) {
210-
if (IsJsonMIME -MIME $Header) {
211-
return $Header
212-
}
213-
}
214-
211+
# if no MIME type is provided return null
215212
if (!($Headers) -or $Headers.Count -eq 0) {
216213
return $null
214+
}
215+
216+
if ($Multiple) {
217+
# return multiple MIME types (for Accept: header)
218+
if ($JsonFirst) {
219+
# sort input to return JSON MIME types first
220+
$mimeHeaders = @()
221+
$otherHeaders = @()
222+
foreach ($Header in $Headers) {
223+
if (IsJsonMIME -MIME $Header) {
224+
$mimeHeaders += $Header
225+
} else {
226+
$otherHeaders += $Header
227+
}
228+
}
229+
$Headers = $($mimeHeaders; $otherHeaders)
230+
}
231+
return [string]::Join(', ', $Headers) # join multiple types if they are provided
217232
} else {
218-
return $Headers[0] # return the first one
233+
foreach ($Header in $Headers) {
234+
if (IsJsonMIME -MIME $Header) {
235+
return $Header # return the first type matching a JSON MIME
236+
}
237+
}
238+
return $Headers[0] # else return the first one
219239
}
220240
}
221241

0 commit comments

Comments
 (0)