[PS][Experimental] Add multiple server support (#5741)

* code comment

* add get host setting

* add multiple server support
This commit is contained in:
William Cheng
2020-03-29 21:44:18 +08:00
committed by GitHub
parent 94152c4d35
commit 01f02f6c57
11 changed files with 280 additions and 16 deletions

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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