From 01f02f6c57d406e0cec39724a59becfa7e30bb48 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 29 Mar 2020 21:44:18 +0800 Subject: [PATCH] [PS][Experimental] Add multiple server support (#5741) * code comment * add get host setting * add multiple server support --- .../PowerShellExperimentalClientCodegen.java | 7 +- .../configuration.mustache | 103 ++++++++++++++++ .../resources/3_0/powershell/petstore.yaml | 23 +++- .../powershell-experimental/README.md | 2 +- .../powershell-experimental/docs/PSPetApi.md | 2 +- .../docs/PSStoreApi.md | 8 +- .../powershell-experimental/docs/PSUserApi.md | 2 +- .../src/PSPetstore/API/PSStoreApi.ps1 | 4 +- .../src/PSPetstore/Client/PSConfiguration.ps1 | 115 +++++++++++++++++- .../src/PSPetstore/PSPetstore.psd1 | 5 +- .../tests/Petstore.Tests.ps1 | 25 ++++ 11 files changed, 280 insertions(+), 16 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java index 02ff6a12b1a..969bb3d5b92 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java @@ -309,7 +309,7 @@ public class PowerShellExperimentalClientCodegen extends DefaultCodegen implemen typeMapping.put("long", "Int64"); typeMapping.put("double", "Double"); typeMapping.put("number", "Decimal"); - typeMapping.put("object", "System.Hashtable"); + typeMapping.put("object", "System.Collections.Hashtable"); typeMapping.put("file", "System.IO.FileInfo"); typeMapping.put("ByteArray", "System.Byte[]"); typeMapping.put("binary", "System.IO.FileInfo"); @@ -374,8 +374,9 @@ public class PowerShellExperimentalClientCodegen extends DefaultCodegen implemen } if (StringUtils.isNotBlank(powershellGalleryUrl)) { + // get the last segment of the URL + // e.g. https://www.powershellgallery.com/packages/PSTwitter => PSTwitter additionalProperties.put("powershellGalleryId", powershellGalleryUrl.replaceFirst(".*/([^/?]+).*", "$1")); - //additionalProperties.put("powershellGalleryId", "something"); } if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) { @@ -569,7 +570,7 @@ public class PowerShellExperimentalClientCodegen extends DefaultCodegen implemen Schema inner = ap.getItems(); return getTypeDeclaration(inner) + "[]"; } else if (ModelUtils.isMapSchema(p)) { - return "Hashtable"; + return "System.Collections.Hashtable"; } else if (!languageSpecificPrimitives.contains(getSchemaType(p))) { return super.getTypeDeclaration(p); } 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 69d92b1e6fc..355b556c41d 100644 --- a/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache @@ -209,3 +209,106 @@ function Set-{{{apiNamePrefix}}}ConfigurationApiKeyPrefix { $Script:Configuration["ApiKeyPrefix"][$Id] = $ApiKeyPrefix } } + +<# +.SYNOPSIS + +Get the host setting. + +.DESCRIPTION + +Get the host setting in the form of array of hashtables. + +.OUTPUTS + +System.Collections.Hashtable[] +#> +function Get-{{apiNamePrefix}}HostSettings { + return @( + {{#servers}} + @{ + "Url" = "{{{url}}}"; + "Description" = "{{{description}}}{{^description}}No description provided{{/description}}"; + {{#variables}} + {{#-first}} + "Variables" = @{ + {{/-first}} + "{{{name}}}" = @{ + "Description" = "{{{description}}}{{^description}}No description provided{{/description}}"; + "DefaultValue" = "{{{defaultValue}}}"; + {{#enumValues}} + {{#-first}} + "EnumValues" = @( + {{/-first}} + "{{{.}}}"{{^-last}},{{/-last}} + {{#-last}} + ) + {{/-last}} + {{/enumValues}} + }{{^-last}};{{/-last}} + {{#-last}} + } + {{/-last}} + {{/variables}} + }{{^-last}},{{/-last}} + {{/servers}} + ) + +} + +<# +.SYNOPSIS + +Get the URL from the host settings. + +.PARAMETER Index +Index of the host settings (array) + +.PARAMETER Variables +Names and values of the variables (hashtable) + +.DESCRIPTION + +Get the URL from the host settings. + +.OUTPUTS + +String +#> +function Get-{{apiNamePrefix}}UrlFromHostSettings { + + [CmdletBinding()] + Param( + [Parameter(ValueFromPipeline = $true)] + [Int]$Index, + [Hashtable]$Variables = @{} + ) + + Process { + $Hosts = Get-{{apiNamePrefix}}HostSettings + + # check array index out of bound + if ($Index -lt 0 -or $Index -gt $Hosts.Length) { + throw "Invalid index $index when selecting the host. Must be less than $($Hosts.Length)" + } + + $Host = $Hosts[$Index]; + $Url = $Host["Url"]; + + # go through variable and assign a value + foreach ($h in $Host["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]) + } else { + throw "The variable '$($h.Name)' in the host URL has invalid value $($Variables[$h.Name]). Must be $($h.Value["EnumValues"] -join ",")" + } + } else { + $Url = $Url.replace("{$($h.Name)}", $h.Value["DefaultValue"]) + } + } + + return $Url; + + } +} diff --git a/modules/openapi-generator/src/test/resources/3_0/powershell/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/powershell/petstore.yaml index 0c39d314629..a2e1dbaefa6 100644 --- a/modules/openapi-generator/src/test/resources/3_0/powershell/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/powershell/petstore.yaml @@ -1,6 +1,27 @@ openapi: 3.0.0 servers: - - url: 'http://petstore.swagger.io/v2' + - url: 'http://{server}.swagger.io:{port}/v2' + description: petstore server + variables: + server: + enum: + - 'petstore' + - 'qa-petstore' + - 'dev-petstore' + default: 'petstore' + port: + enum: + - 80 + - 8080 + default: 80 + - url: https://localhost:8080/{version} + description: The local server + variables: + version: + enum: + - 'v1' + - 'v2' + default: 'v2' info: description: >- This is a sample server Petstore server. For this sample, you can use the api key diff --git a/samples/client/petstore/powershell-experimental/README.md b/samples/client/petstore/powershell-experimental/README.md index bb8561d9aff..9f3c38ef438 100644 --- a/samples/client/petstore/powershell-experimental/README.md +++ b/samples/client/petstore/powershell-experimental/README.md @@ -49,7 +49,7 @@ Invoker-Pester ## Documentation for API Endpoints -All URIs are relative to *http://petstore.swagger.io/v2* +All URIs are relative to *http://petstore.swagger.io:80/v2* Class | Method | HTTP request | Description ------------ | ------------- | ------------- | ------------- diff --git a/samples/client/petstore/powershell-experimental/docs/PSPetApi.md b/samples/client/petstore/powershell-experimental/docs/PSPetApi.md index 52168f02c91..0d6c2ae17a8 100644 --- a/samples/client/petstore/powershell-experimental/docs/PSPetApi.md +++ b/samples/client/petstore/powershell-experimental/docs/PSPetApi.md @@ -1,6 +1,6 @@ # PSPetstore.PSPetstore/Api.PSPetApi -All URIs are relative to *http://petstore.swagger.io/v2* +All URIs are relative to *http://petstore.swagger.io:80/v2* Method | HTTP request | Description ------------- | ------------- | ------------- diff --git a/samples/client/petstore/powershell-experimental/docs/PSStoreApi.md b/samples/client/petstore/powershell-experimental/docs/PSStoreApi.md index b9610344c8d..e94d5fa9557 100644 --- a/samples/client/petstore/powershell-experimental/docs/PSStoreApi.md +++ b/samples/client/petstore/powershell-experimental/docs/PSStoreApi.md @@ -1,6 +1,6 @@ # PSPetstore.PSPetstore/Api.PSStoreApi -All URIs are relative to *http://petstore.swagger.io/v2* +All URIs are relative to *http://petstore.swagger.io:80/v2* Method | HTTP request | Description ------------- | ------------- | ------------- @@ -57,7 +57,7 @@ No authorization required # **Get-PSInventory** -> Hashtable Get-PSInventory
+> System.Collections.Hashtable Get-PSInventory
Returns pet inventories by status @@ -76,7 +76,7 @@ $Configuration["ApiKey"]["api_key"] = "YOUR_API_KEY" # Returns pet inventories by status try { - Hashtable $Result = Get-PSInventory + System.Collections.Hashtable $Result = Get-PSInventory } catch { Write-Host ($_.ErrorDetails | ConvertFrom-Json) Write-Host ($_.Exception.Response.Headers | ConvertTo-Json) @@ -88,7 +88,7 @@ This endpoint does not need any parameter. ### Return type -**Hashtable** +**System.Collections.Hashtable** ### Authorization diff --git a/samples/client/petstore/powershell-experimental/docs/PSUserApi.md b/samples/client/petstore/powershell-experimental/docs/PSUserApi.md index e1a41da0b1a..31284988c7e 100644 --- a/samples/client/petstore/powershell-experimental/docs/PSUserApi.md +++ b/samples/client/petstore/powershell-experimental/docs/PSUserApi.md @@ -1,6 +1,6 @@ # PSPetstore.PSPetstore/Api.PSUserApi -All URIs are relative to *http://petstore.swagger.io/v2* +All URIs are relative to *http://petstore.swagger.io:80/v2* Method | HTTP request | Description ------------- | ------------- | ------------- diff --git a/samples/client/petstore/powershell-experimental/src/PSPetstore/API/PSStoreApi.ps1 b/samples/client/petstore/powershell-experimental/src/PSPetstore/API/PSStoreApi.ps1 index 577a91767d0..503cdaae77c 100644 --- a/samples/client/petstore/powershell-experimental/src/PSPetstore/API/PSStoreApi.ps1 +++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/API/PSStoreApi.ps1 @@ -75,7 +75,7 @@ No description available. .OUTPUTS -Hashtable +System.Collections.Hashtable #> function Get-PSInventory { [CmdletBinding()] @@ -114,7 +114,7 @@ function Get-PSInventory { -QueryParameters $LocalVarQueryParameters ` -FormParameters $LocalVarFormParameters ` -CookieParameters $LocalVarCookieParameters ` - -ReturnType "Hashtable" + -ReturnType "System.Collections.Hashtable" return $LocalVarResult["Response"] } 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 17f463156cf..86c952757d9 100644 --- a/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 +++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1 @@ -23,7 +23,7 @@ function Get-PSConfiguration { $Configuration = $Script:Configuration if ([string]::IsNullOrEmpty($Configuration["BaseUrl"])) { - $Configuration["BaseUrl"] = "http://petstore.swagger.io/v2"; + $Configuration["BaseUrl"] = "http://petstore.swagger.io:80/v2"; } if (!$Configuration.containsKey("Username")) { @@ -215,3 +215,116 @@ function Set-PSConfigurationApiKeyPrefix { $Script:Configuration["ApiKeyPrefix"][$Id] = $ApiKeyPrefix } } + +<# +.SYNOPSIS + +Get the host setting. + +.DESCRIPTION + +Get the host setting in the form of array of hashtables. + +.OUTPUTS + +System.Collections.Hashtable[] +#> +function Get-PSHostSettings { + return @( + @{ + "Url" = "http://{server}.swagger.io:{port}/v2"; + "Description" = "petstore server"; + "Variables" = @{ + "server" = @{ + "Description" = "No description provided"; + "DefaultValue" = "petstore"; + "EnumValues" = @( + "petstore", + "qa-petstore", + "dev-petstore" + ) + }; + "port" = @{ + "Description" = "No description provided"; + "DefaultValue" = "80"; + "EnumValues" = @( + "80", + "8080" + ) + } + } + }, + @{ + "Url" = "https://localhost:8080/{version}"; + "Description" = "The local server"; + "Variables" = @{ + "version" = @{ + "Description" = "No description provided"; + "DefaultValue" = "v2"; + "EnumValues" = @( + "v1", + "v2" + ) + } + } + } + ) + +} + +<# +.SYNOPSIS + +Get the URL from the host settings. + +.PARAMETER Index +Index of the host settings (array) + +.PARAMETER Variables +Names and values of the variables (hashtable) + +.DESCRIPTION + +Get the URL from the host settings. + +.OUTPUTS + +String +#> +function Get-PSUrlFromHostSettings { + + [CmdletBinding()] + Param( + [Parameter(ValueFromPipeline = $true)] + [Int]$Index, + [Hashtable]$Variables = @{} + ) + + Process { + $Hosts = Get-PSHostSettings + + # check array index out of bound + if ($Index -lt 0 -or $Index -gt $Hosts.Length) { + throw "Invalid index $index when selecting the host. Must be less than $($Hosts.Length)" + } + + $Host = $Hosts[$Index]; + $Url = $Host["Url"]; + + # go through variable and assign a value + foreach ($h in $Host["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]) + } else { + throw "The variable '$($h.Name)' in the host URL has invalid value $($Variables[$h.Name]). Must be $($h.Value["EnumValues"] -join ",")" + } + } else { + $Url = $Url.replace("{$($h.Name)}", $h.Value["DefaultValue"]) + } + } + + return $Url; + + } +} diff --git a/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 b/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 index 66471446302..65ba73dea3a 100644 --- a/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 +++ b/samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1 @@ -3,7 +3,7 @@ # # Generated by: OpenAPI Generator Team # -# Generated on: 3/19/20 +# Generated on: 3/29/20 # @{ @@ -79,7 +79,8 @@ FunctionsToExport = 'Add-PSPet', 'Remove-Pet', 'Find-PSPetsByStatus', 'Find-PSPe 'Update-PSUser', 'New-PSApiResponse', 'New-PSCategory', 'New-PSInlineObject', 'New-PSInlineObject1', 'New-PSOrder', 'New-PSPet', 'New-PSTag', 'New-PSUser', 'Get-PSConfiguration', 'Set-PSConfiguration', - 'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix' + 'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix', + 'Get-PSHostSettings', 'Get-PSUrlFromHostSettings' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 b/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 index 8076bf53917..c7325ab1cfc 100644 --- a/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 +++ b/samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1 @@ -60,4 +60,29 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' { } } + + Context 'Configuration' { + It 'Get-PSHostSettings tests' { + + $HS = Get-PSHostSettings + + $HS[0]["Url"] | Should Be "http://{server}.swagger.io:{port}/v2" + $HS[0]["Description"] | Should Be "petstore server" + $HS[0]["Variables"]["server"]["Description"] | Should Be "No description provided" + $HS[0]["Variables"]["server"]["DefaultValue"] | Should Be "petstore" + $HS[0]["Variables"]["server"]["EnumValues"] | Should Be @("petstore", + "qa-petstore", + "dev-petstore") + + } + + It "Get-PSUrlFromHostSettings tests" { + Get-PSUrlFromHostSettings -Index 0 | Should Be "http://petstore.swagger.io:80/v2" + Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2" + #Get-PSUrlFromHostSettings -Index 2 | Should -Throw -ExceptionType ([RuntimeException]) + #Get-PSUrlFromHostSettings -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2" + #Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080" + + } + } }