Skip to content

Commit 42e87c8

Browse files
authored
rename hostsetting, validate base url (#5821)
1 parent 242c2e8 commit 42e87c8

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

modules/openapi-generator/src/main/resources/powershell-experimental/configuration.mustache

+9-3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ function Set-{{{apiNamePrefix}}}Configuration {
119119
Process {
120120
121121
If ($BaseUrl) {
122+
# validate URL
123+
$URL = $BaseUrl -as [System.URI]
124+
if (!($URL.AbsoluteURI -ne $null -and $URL.Scheme -match '[http|https]')) {
125+
throw "Invalid URL '$($BaseUrl)' cannot be used in the base URL."
126+
}
127+
122128
$Script:Configuration["BaseUrl"] = $BaseUrl
123129
}
124130

@@ -278,7 +284,7 @@ Get the host setting in the form of array of hashtables.
278284

279285
System.Collections.Hashtable[]
280286
#>
281-
function Get-{{apiNamePrefix}}HostSettings {
287+
function Get-{{apiNamePrefix}}HostSetting {
282288
return @(
283289
{{#servers}}
284290
@{
@@ -330,7 +336,7 @@ Get the URL from the host settings.
330336

331337
String
332338
#>
333-
function Get-{{apiNamePrefix}}UrlFromHostSettings {
339+
function Get-{{apiNamePrefix}}UrlFromHostSetting {
334340
335341
[CmdletBinding()]
336342
Param(
@@ -340,7 +346,7 @@ function Get-{{apiNamePrefix}}UrlFromHostSettings {
340346
)
341347

342348
Process {
343-
$Hosts = Get-{{apiNamePrefix}}HostSettings
349+
$Hosts = Get-{{apiNamePrefix}}HostSetting
344350

345351
# check array index out of bound
346352
if ($Index -lt 0 -or $Index -gt $Hosts.Length) {

samples/client/petstore/powershell-experimental/src/PSPetstore/Client/PSConfiguration.ps1

+9-3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ function Set-PSConfiguration {
125125
Process {
126126

127127
If ($BaseUrl) {
128+
# validate URL
129+
$URL = $BaseUrl -as [System.URI]
130+
if (!($URL.AbsoluteURI -ne $null -and $URL.Scheme -match '[http|https]')) {
131+
throw "Invalid URL '$($BaseUrl)' cannot be used in the base URL."
132+
}
133+
128134
$Script:Configuration["BaseUrl"] = $BaseUrl
129135
}
130136

@@ -284,7 +290,7 @@ Get the host setting in the form of array of hashtables.
284290
285291
System.Collections.Hashtable[]
286292
#>
287-
function Get-PSHostSettings {
293+
function Get-PSHostSetting {
288294
return @(
289295
@{
290296
"Url" = "http://{server}.swagger.io:{port}/v2";
@@ -346,7 +352,7 @@ Get the URL from the host settings.
346352
347353
String
348354
#>
349-
function Get-PSUrlFromHostSettings {
355+
function Get-PSUrlFromHostSetting {
350356

351357
[CmdletBinding()]
352358
Param(
@@ -356,7 +362,7 @@ function Get-PSUrlFromHostSettings {
356362
)
357363

358364
Process {
359-
$Hosts = Get-PSHostSettings
365+
$Hosts = Get-PSHostSetting
360366

361367
# check array index out of bound
362368
if ($Index -lt 0 -or $Index -gt $Hosts.Length) {

samples/client/petstore/powershell-experimental/src/PSPetstore/PSPetstore.psd1

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: OpenAPI Generator Team
55
#
6-
# Generated on: 4/3/20
6+
# Generated on: 4/4/20
77
#
88

99
@{
@@ -81,8 +81,8 @@ FunctionsToExport = 'Add-PSPet', 'Remove-Pet', 'Find-PSPetsByStatus', 'Find-PSPe
8181
'Initialize-PSPet', 'Initialize-PSTag', 'Initialize-PSUser',
8282
'Get-PSConfiguration', 'Set-PSConfiguration',
8383
'Set-PSConfigurationApiKey', 'Set-PSConfigurationApiKeyPrefix',
84-
'Set-PSConfigurationDefaultHeader', 'Get-PSHostSettings',
85-
'Get-PSUrlFromHostSettings'
84+
'Set-PSConfigurationDefaultHeader', 'Get-PSHostSetting',
85+
'Get-PSUrlFromHostSetting'
8686

8787
# 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.
8888
CmdletsToExport = @()

samples/client/petstore/powershell-experimental/tests/Petstore.Tests.ps1

+14-8
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
115115
}
116116

117117
Context 'Configuration' {
118-
It 'Get-PSHostSettings tests' {
118+
It 'Get-PSHostSetting tests' {
119119

120-
$HS = Get-PSHostSettings
120+
$HS = Get-PSHostSetting
121121

122122
$HS[0]["Url"] | Should Be "http://{server}.swagger.io:{port}/v2"
123123
$HS[0]["Description"] | Should Be "petstore server"
@@ -129,12 +129,12 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
129129

130130
}
131131

132-
It "Get-PSUrlFromHostSettings tests" {
133-
Get-PSUrlFromHostSettings -Index 0 | Should Be "http://petstore.swagger.io:80/v2"
134-
Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2"
135-
#Get-PSUrlFromHostSettings -Index 2 | Should -Throw -ExceptionType ([RuntimeException])
136-
#Get-PSUrlFromHostSettings -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2"
137-
#Get-PSUrlFromHostSettings -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080"
132+
It "Get-PSUrlFromHostSetting tests" {
133+
Get-PSUrlFromHostSetting -Index 0 | Should Be "http://petstore.swagger.io:80/v2"
134+
Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "8080" } | Should Be "http://petstore.swagger.io:8080/v2"
135+
#Get-PSUrlFromHostSetting -Index 2 | Should -Throw -ExceptionType ([RuntimeException])
136+
#Get-PSUrlFromHostSetting -Index 2 | Should -Throw # "Invalid index 2 when selecting the host. Must be less than 2"
137+
#Get-PSUrlFromHostSetting -Index 0 -Variables @{ "port" = "1234" } | Should Throw "The variable 'port' in the host URL has invalid value 1234. Must be 80,8080"
138138

139139
}
140140

@@ -153,6 +153,12 @@ Describe -tag 'PSOpenAPITools' -name 'Integration Tests' {
153153
$Conf["SkipCertificateCheck"] | Should Be $false
154154
$Conf = Set-PSConfiguration -PassThru -SkipCertificateCheck
155155
$Conf["SkipCertificateCheck"] | Should Be $true
156+
$Conf = Set-PSConfiguration -PassThru # reset SkipCertificateCheck
157+
}
158+
159+
It "Base URL tests" {
160+
$Conf = Set-PSConfiguration -BaseURL "http://localhost"
161+
$Conf = Set-PSConfiguration -BaseURL "https://localhost:8080/api"
156162
}
157163
}
158164
}

0 commit comments

Comments
 (0)