[PS] Select Content-Type in the response (#5872)

* better return type handling

* update tempalte

* better documentation

* fix greater than
This commit is contained in:
William Cheng
2020-04-09 20:06:26 +08:00
committed by GitHub
parent c73f3c5eb2
commit 7342077cb1
9 changed files with 187 additions and 17 deletions

View File

@@ -795,6 +795,10 @@ public class PowerShellExperimentalClientCodegen extends DefaultCodegen implemen
} else {
methodNames.add(op.vendorExtensions.get("x-powershell-method-name"));
}
if (op.produces != null && op.produces.size() > 1) {
op.vendorExtensions.put("x-powershell-select-accept", true);
}
}
processedModelMaps.clear();

View File

@@ -15,6 +15,12 @@
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{/allParams}}
{{#vendorExtensions.x-powershell-select-accept}}
.PARAMETER ReturnType
Select the return type (optional): {{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}
{{/vendorExtensions.x-powershell-select-accept}}
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -38,6 +44,11 @@ function {{{vendorExtensions.x-powershell-method-name}}} {
${<%paramName%>},
<%={{ }}=%>
{{/allParams}}
{{#vendorExtensions.x-powershell-select-accept}}
[String]
[ValidateSet({{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}})]
$ReturnType,
{{/vendorExtensions.x-powershell-select-accept}}
[Switch]
$WithHttpInfo
)
@@ -61,6 +72,13 @@ function {{{vendorExtensions.x-powershell-method-name}}} {
$LocalVarAccepts = @({{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}})
{{/hasProduces}}
{{#vendorExtensions.x-powershell-select-accept}}
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
{{/vendorExtensions.x-powershell-select-accept}}
{{#hasConsumes}}
# HTTP header 'Content-Type'
$LocalVarContentTypes = @({{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}})

View File

@@ -108,7 +108,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
}
return @{
Response = DeserializeResponse -Response $Response -ReturnType $ReturnType
Response = DeserializeResponse -Response $Response -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"]
StatusCode = $Response.StatusCode
Headers = $Response.Headers
}
@@ -174,7 +174,10 @@ function DeserializeResponse {
[string]$ReturnType,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Response
[string]$Response,
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[string[]]$ContentTypes
)
If ([string]::IsNullOrEmpty($ReturnType)) { # void response
@@ -183,7 +186,22 @@ function DeserializeResponse {
return ConvertFrom-Json $Response
} Elseif (@("String", "Boolean", "System.DateTime") -contains $ReturnType) { # string, boolean ,datetime
return $Response
} Else { # model
return ConvertFrom-Json $Response
} Else { # others (e.g. model, file)
if ($ContentTypes) {
$ContentType = $null
if ($ContentTypes.Count -gt 1) {
$ContentType = SelectContentTypeHeaders -ContentTypes $ContentTypes
} else {
$ContentType = $ContentTypes[0]
}
if (IsJsonMIME -MIME $ContentType) { # JSON
return ConvertFrom-Json $Response
} else { # XML, file, etc
return $Response
}
} else { # no content type in response header, returning raw response
return $Response
}
}
}

View File

@@ -16,7 +16,7 @@ $body = (Initialize-PSUser -Id 123 -Username "Username_example" -FirstName "Fi
$Id = 38369
#$result = Update-PSPetWithForm
try {
#try {
$pet = Initialize-PSPet -Id $Id -Name 'foo' -Category (
Initialize-PSCategory -Id $Id -Name 'bar'
) -PhotoUrls @(
@@ -29,14 +29,18 @@ try {
#Write-Host $pet
$Result = Add-PSPet -Pet $pet
Set-PSConfigurationApiKey -Id "api_key" -ApiKey "zzZZZZZZZZZZZZZ"
$Result2 = Get-PSPetById -petId ($Id + 10) -Verbose -WithHttpInfo #-testHeader "testing only" -testQuery "testing something here"
Write-Host $Result2.GetType()
} catch {
#Write-Host ("Exception occured when calling '': {0}" -f ($_.ErrorDetails | ConvertFrom-Json))
#Write-Host ("Response headers: {0}" -f ($_.Exception.Response.Headers | ConvertTo-Json))
}
$Result2 = Get-PSPetById -petId ($Id) -Verbose -WithHttpInfo #-testHeader "testing only" -testQuery "testing something here"
Write-Host $Result2["Headers"]["Content-Type"]
$Result3 = Get-PSPetById -petId ($Id) -Verbose -WithHttpInfo -ReturnType "application/xml" #-testHeader "testing only" -testQuery "testing something here"
Write-Host $Result3["Headers"]["Content-Type"]
Write-Host $Result3["Response"]
#} catch {
# Write-Host ("Exception occured when calling '': {0}" -f ($_.ErrorDetails | ConvertFrom-Json))
# Write-Host ("Response headers: {0}" -f ($_.Exception.Response.Headers | ConvertTo-Json))
#}
Write-Host "Before exit $($Result2.GetType())"
#$Result = Add-PSPet -Pet $pet -ReturnType "application/xml"
#Write-Host "Before exit $($Result2.GetType())"
#$result | Write-Host

View File

@@ -17,6 +17,10 @@ No description available.
.PARAMETER Pet
Pet object that needs to be added to the store
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -31,6 +35,9 @@ function Add-PSPet {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[PSCustomObject]
${Pet},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -52,6 +59,11 @@ function Add-PSPet {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
# HTTP header 'Content-Type'
$LocalVarContentTypes = @('application/json', 'application/xml')
@@ -175,6 +187,10 @@ No description available.
.PARAMETER Status
Status values that need to be considered for filter
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -190,6 +206,9 @@ function Find-PSPetsByStatus {
[ValidateSet("available", "pending", "sold")]
[String[]]
${Status},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -211,6 +230,11 @@ function Find-PSPetsByStatus {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
$LocalVarUri = '/pet/findByStatus'
if (!$Status) {
@@ -250,6 +274,10 @@ No description available.
.PARAMETER Tags
Tags to filter by
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -264,6 +292,9 @@ function Find-PSPetsByTags {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[String[]]
${Tags},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -285,6 +316,11 @@ function Find-PSPetsByTags {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
$LocalVarUri = '/pet/findByTags'
if (!$Tags) {
@@ -324,6 +360,10 @@ No description available.
.PARAMETER PetId
ID of pet to return
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -338,6 +378,9 @@ function Get-PSPetById {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[Int64]
${PetId},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -359,6 +402,11 @@ function Get-PSPetById {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
$LocalVarUri = '/pet/{petId}'
if (!$PetId) {
throw "Error! The required parameter `PetId` missing when calling getPetById."
@@ -401,6 +449,10 @@ No description available.
.PARAMETER Pet
Pet object that needs to be added to the store
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -415,6 +467,9 @@ function Update-PSPet {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[PSCustomObject]
${Pet},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -436,6 +491,11 @@ function Update-PSPet {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
# HTTP header 'Content-Type'
$LocalVarContentTypes = @('application/json', 'application/xml')

View File

@@ -153,6 +153,10 @@ No description available.
.PARAMETER OrderId
ID of pet that needs to be fetched
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -167,6 +171,9 @@ function Get-PSOrderById {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[Int64]
${OrderId},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -188,6 +195,11 @@ function Get-PSOrderById {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
$LocalVarUri = '/store/order/{orderId}'
if (!$OrderId) {
throw "Error! The required parameter `OrderId` missing when calling getOrderById."
@@ -225,6 +237,10 @@ No description available.
.PARAMETER Order
order placed for purchasing the pet
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -239,6 +255,9 @@ function Invoke-PSPlaceOrder {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[PSCustomObject]
${Order},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -260,6 +279,11 @@ function Invoke-PSPlaceOrder {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
# HTTP header 'Content-Type'
$LocalVarContentTypes = @('application/json')

View File

@@ -328,6 +328,10 @@ No description available.
.PARAMETER Username
The name that needs to be fetched. Use user1 for testing.
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -342,6 +346,9 @@ function Get-PSUserByName {
[Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[String]
${Username},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -363,6 +370,11 @@ function Get-PSUserByName {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
$LocalVarUri = '/user/{username}'
if (!$Username) {
throw "Error! The required parameter `Username` missing when calling getUserByName."
@@ -403,6 +415,10 @@ The user name for login
.PARAMETER Password
The password for login in clear text
.PARAMETER ReturnType
Select the return type (optional): application/xml, application/json
.PARAMETER WithHttpInfo
A switch when turned on will return a hash table of Response, StatusCode and Headers instead of just the Response
@@ -420,6 +436,9 @@ function Invoke-PSLoginUser {
[Parameter(Position = 1, ValueFromPipelineByPropertyName = $true, Mandatory = $false)]
[String]
${Password},
[String]
[ValidateSet("application/xml", "application/json")]
$ReturnType,
[Switch]
$WithHttpInfo
)
@@ -441,6 +460,11 @@ function Invoke-PSLoginUser {
# HTTP header 'Accept' (if needed)
$LocalVarAccepts = @('application/xml', 'application/json')
if ($ReturnType) {
# use the return type (MIME) provided by the user
$LocalVarAccepts = @($ReturnType)
}
$LocalVarUri = '/user/login'
if (!$Username) {

View File

@@ -3,7 +3,7 @@
#
# Generated by: OpenAPI Generator Team
#
# Generated on: 4/8/20
# Generated on: 4/9/20
#
@{

View File

@@ -114,7 +114,7 @@ function Invoke-PSApiClient {
}
return @{
Response = DeserializeResponse -Response $Response -ReturnType $ReturnType
Response = DeserializeResponse -Response $Response -ReturnType $ReturnType -ContentTypes $Response.Headers["Content-Type"]
StatusCode = $Response.StatusCode
Headers = $Response.Headers
}
@@ -180,7 +180,10 @@ function DeserializeResponse {
[string]$ReturnType,
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Response
[string]$Response,
[Parameter(Mandatory)]
[AllowEmptyCollection()]
[string[]]$ContentTypes
)
If ([string]::IsNullOrEmpty($ReturnType)) { # void response
@@ -189,7 +192,22 @@ function DeserializeResponse {
return ConvertFrom-Json $Response
} Elseif (@("String", "Boolean", "System.DateTime") -contains $ReturnType) { # string, boolean ,datetime
return $Response
} Else { # model
return ConvertFrom-Json $Response
} Else { # others (e.g. model, file)
if ($ContentTypes) {
$ContentType = $null
if ($ContentTypes.Count -gt 1) {
$ContentType = SelectContentTypeHeaders -ContentTypes $ContentTypes
} else {
$ContentType = $ContentTypes[0]
}
if (IsJsonMIME -MIME $ContentType) { # JSON
return ConvertFrom-Json $Response
} else { # XML, file, etc
return $Response
}
} else { # no content type in response header, returning raw response
return $Response
}
}
}