Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit fb8b946

Browse files
authored
Add support for generating proper output type for the Swagger operations with x-ms-pageable extension (#342)
* Added support for generating proper output type for the Swaagger operations with x-ms-pageable extension.
1 parent f0e1c5a commit fb8b946

File tree

4 files changed

+145
-15
lines changed

4 files changed

+145
-15
lines changed

PSSwagger/Paths.psm1

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ function New-SwaggerPath
561561
$Cmdlet = ''
562562
$CmdletParameter = ''
563563
$CmdletArgs = ''
564-
$pageType = 'Array'
564+
$pageType = ''
565565
$resultBlockStr = $resultBlockNoPaging
566566
if ($x_ms_pageableObject) {
567567
if ($x_ms_pageableObject.ReturnType -ne 'NONE') {
@@ -988,7 +988,13 @@ function New-SwaggerPath
988988
$bodyObject = $pathGenerationPhaseResult.BodyObject
989989

990990
$body = $bodyObject.Body
991-
$outputTypeBlock = $bodyObject.OutputTypeBlock
991+
if($pageType){
992+
$fullPathDataType = $pageType
993+
$outputTypeBlock = $executionContext.InvokeCommand.ExpandString($outputTypeStr)
994+
}
995+
else {
996+
$outputTypeBlock = $bodyObject.OutputTypeBlock
997+
}
992998

993999
if ($UseAzureCsharpGenerator) {
9941000
$dependencyInitFunction = "Initialize-PSSwaggerDependencies -Azure"
@@ -1150,6 +1156,10 @@ function Set-ExtendedCodeMetadata {
11501156
$returnType = $returnType.GenericTypeArguments[0]
11511157
}
11521158

1159+
if (($returnType.Name -eq 'IPage`1') -and $returnType.GenericTypeArguments) {
1160+
$returnType = $returnType.GenericTypeArguments[0]
1161+
}
1162+
# Note: ReturnType is currently used for Swagger operations which supports x-ms-pageable.
11531163
$returnTypeString = Convert-GenericTypeToString -Type $returnType
11541164
$parameterSetDetail['ReturnType'] = $returnTypeString
11551165

@@ -1272,7 +1282,7 @@ function Convert-GenericTypeToString {
12721282
)
12731283

12741284
if (-not $Type.IsGenericType) {
1275-
return $Type.FullName
1285+
return $Type.FullName.Trim('[]')
12761286
}
12771287

12781288
$genericTypeStr = ''

Tests/PSSwagger.Unit.Tests.ps1

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,32 @@ Describe "PSSwagger Unit Tests" -Tag @('BVT', 'DRT', 'UnitTest', 'P0') {
269269
}
270270

271271
Context "Get-CSharpModelName Unit Tests" {
272-
It "Get-CSharpModelNamee should remove special characters" {
272+
It "Get-CSharpModelName should remove special characters" {
273273
Get-CSharpModelName -Name @"
274274
SomeTypeWithSpecialCharacters ~!@#$%^&*()_+|}{:"<>?,./;'][\=-``
275275
"@ | Should BeExactly 'SomeTypeWithSpecialCharacters'
276276
}
277-
It "Get-CSharpModelNamee should replace [] with Sequence" {
277+
It "Get-CSharpModelName should replace [] with Sequence" {
278278
Get-CSharpModelName -Name 'foo[]' | Should BeExactly 'FooSequence'
279279
}
280-
It "Get-CSharpModelNamee should append 'Model' for C# reserved words" {
280+
It "Get-CSharpModelName should append 'Model' for C# reserved words" {
281281
Get-CSharpModelName -Name 'break' | Should BeExactly 'BreakModel'
282282
}
283283
}
284+
285+
Context "Convert-GenericTypeToString Unit Tests" {
286+
It "Convert-GenericTypeToString with 'System.Int32' type" {
287+
Convert-GenericTypeToString -Type ('System.Int32' -as [type]) | Should BeExactly 'System.Int32'
288+
}
289+
It "Convert-GenericTypeToString with 'System.String[]' type" {
290+
Convert-GenericTypeToString -Type ('System.String[]' -as [type]) | Should BeExactly 'System.String'
291+
}
292+
It "Convert-GenericTypeToString with 'System.Collections.Generic.List[string]' type" {
293+
Convert-GenericTypeToString -Type ('System.Collections.Generic.List[string]' -as [type]) | Should BeExactly 'System.Collections.Generic.List[System.String]'
294+
}
295+
It "Convert-GenericTypeToString with 'System.Collections.Generic.Dictionary[string, string]' type" {
296+
Convert-GenericTypeToString -Type ('System.Collections.Generic.Dictionary[string, string]' -as [type]) | Should BeExactly 'System.Collections.Generic.Dictionary[System.String,System.String]'
297+
}
298+
}
284299
}
285300
}

Tests/PSSwaggerScenario.Tests.ps1

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,17 @@ Describe "Optional parameter tests" -Tag ScenarioTest {
245245

246246
Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
247247
BeforeAll {
248+
$ModuleName = 'Generated.ParamTypes.Module'
248249
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwaggerUtility" | `
249250
Join-Path -ChildPath "PSSwaggerUtility.psd1") -Force
250-
Initialize-Test -GeneratedModuleName "Generated.ParamTypes.Module" -GeneratedModuleVersion "0.0.2" -TestApiName "ParameterTypes" `
251+
Initialize-Test -GeneratedModuleName $ModuleName -GeneratedModuleVersion "0.0.2" -TestApiName "ParameterTypes" `
251252
-TestSpecFileName "ParameterTypesSpec.json" -TestDataFileName "ParameterTypesData.json" `
252253
-PsSwaggerPath (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger") -TestRootPath $PSScriptRoot
253254

254255
# Import generated module
255256
Write-Verbose "Importing modules"
256257
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "Generated" | `
257-
Join-Path -ChildPath "Generated.ParamTypes.Module")
258+
Join-Path -ChildPath $ModuleName)
258259

259260
$processes = Start-JsonServer -TestRootPath $PSScriptRoot -TestApiName "ParameterTypes"
260261
if ($global:PSSwaggerTest_EnableTracing -and $script:EnableTracer) {
@@ -325,7 +326,6 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
325326
}
326327

327328
It "Test dummy definition references" {
328-
$ModuleName = 'Generated.ParamTypes.Module'
329329
$ev = $null
330330
$CommandList = Get-Command -Module $ModuleName -ErrorVariable ev
331331
$ev | Should BeNullOrEmpty
@@ -345,7 +345,6 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
345345
}
346346

347347
It "Test CSharp reserved keywords as definition or type names" {
348-
$ModuleName = 'Generated.ParamTypes.Module'
349348
$ev = $null
350349
$CommandList = Get-Command -Module $ModuleName -ErrorVariable ev
351350
$ev | Should BeNullOrEmpty
@@ -375,7 +374,6 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
375374
}
376375

377376
It "Test Definition commands 'New-<NestedDefinition>Object' for nested definitions" {
378-
$ModuleName = 'Generated.ParamTypes.Module'
379377
$ev = $null
380378
$CommandList = Get-Command -Module $ModuleName -ErrorVariable ev
381379
$ev | Should BeNullOrEmpty
@@ -400,7 +398,6 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
400398
}
401399

402400
It 'Test parameter types with array of items in AdditionalProperties json schema' {
403-
$ModuleName = 'Generated.ParamTypes.Module'
404401
$ev = $null
405402
$null = Get-Command -Module $ModuleName -Syntax -ErrorVariable ev
406403
$ev | Should BeNullOrEmpty
@@ -413,8 +410,6 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
413410
}
414411

415412
It 'Test parameter types with references to enum definition type' {
416-
$ModuleName = 'Generated.ParamTypes.Module'
417-
418413
# Swagger operation command with parameter type reference to enum definition type
419414
$OperationCommandInfo = Get-Command -Name Get-PathWithEnumDefinitionType -Module $ModuleName
420415

@@ -453,7 +448,7 @@ Describe "ParameterTypes tests" -Tag @('ParameterTypes','ScenarioTest') {
453448
}
454449
}
455450

456-
Describe "AzureExtensions" {
451+
Describe "AzureExtensions" -Tag @('AzureExtension','ScenarioTest') {
457452
BeforeAll {
458453
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath ".." | Join-Path -ChildPath "PSSwagger" | Join-Path -ChildPath "PSSwaggerUtility" | `
459454
Join-Path -ChildPath "PSSwaggerUtility.psd1") -Force
@@ -1118,3 +1113,32 @@ Describe "Pre-compiled SDK Assmebly scenario tests" -Tag @('SDKAssembly','Scenar
11181113
(Remove-TestErrorId -FullyQualifiedErrorId $ev.FullyQualifiedErrorId) | Should Be 'UnableToExtractDetailsFromSdkAssembly,Update-PathFunctionDetails'
11191114
}
11201115
}
1116+
1117+
Describe "Output type scenario tests" -Tag @('OutputType','ScenarioTest') {
1118+
BeforeAll {
1119+
$ModuleName = 'Generated.AzExt.OutputType.Module'
1120+
$SwaggerSpecPath = Join-Path -Path $PSScriptRoot -ChildPath 'Data' | Join-Path -ChildPath 'AzureExtensions' | Join-Path -ChildPath 'AzureExtensionsSpec.json'
1121+
$GeneratedPath = Join-Path -Path $PSScriptRoot -ChildPath 'Generated'
1122+
$GeneratedModuleBase = Join-Path -Path $GeneratedPath -ChildPath $ModuleName
1123+
if (Test-Path -Path $GeneratedModuleBase -PathType Container) {
1124+
Remove-Item -Path $GeneratedModuleBase -Recurse -Force
1125+
}
1126+
1127+
$params = @{
1128+
SpecificationPath = $SwaggerSpecPath
1129+
Name = $ModuleName
1130+
UseAzureCsharpGenerator = $true
1131+
Path = $GeneratedPath
1132+
ConfirmBootstrap = $true
1133+
Verbose = $true
1134+
}
1135+
Invoke-NewPSSwaggerModuleCommand -NewPSSwaggerModuleParameters $params
1136+
1137+
Import-Module $GeneratedModuleBase -Force
1138+
}
1139+
1140+
It 'Test output type of swagger operation which supports x-ms-pageable' {
1141+
$CommandInfo = Get-Command -Name Get-IotHubResourceEventHubConsumerGroup -Module $ModuleName
1142+
$CommandInfo.OutputType.Type.ToString() | Should BeExactly 'System.String'
1143+
}
1144+
}

Tests/data/AzureExtensions/AzureExtensionsSpec.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,69 @@
449449
}
450450
}
451451
}
452+
},
453+
"/PathWithPagingResult": {
454+
"get": {
455+
"tags": [
456+
"GET"
457+
],
458+
"summary": "Get a list of the consumer groups in the Event Hub-compatible device-to-cloud endpoint in an IoT hub.",
459+
"description": "Get a list of the consumer groups in the Event Hub-compatible device-to-cloud endpoint in an IoT hub.",
460+
"operationId": "IotHubResource_ListEventHubConsumerGroups",
461+
"parameters": [
462+
{
463+
"$ref": "#/parameters/ApiVersionParameter"
464+
},
465+
{
466+
"$ref": "#/parameters/SubscriptionIdParameter"
467+
},
468+
{
469+
"name": "resourceGroupName",
470+
"description": "The name of the resource group that contains the IoT hub.",
471+
"in": "path",
472+
"required": true,
473+
"type": "string"
474+
},
475+
{
476+
"name": "resourceName",
477+
"in": "path",
478+
"description": "The name of the IoT hub.",
479+
"required": true,
480+
"type": "string"
481+
},
482+
{
483+
"name": "eventHubEndpointName",
484+
"in": "path",
485+
"description": "The name of the Event Hub-compatible endpoint.",
486+
"required": true,
487+
"type": "string"
488+
}
489+
],
490+
"responses": {
491+
"200": {
492+
"description": "This is a synchronous operation. The body contains a JSON-serialized list of the consumer groups in the the Event Hub-compatible endpoint in this IoT hub",
493+
"schema": {
494+
"$ref": "#/definitions/EventHubConsumerGroupsListResult"
495+
}
496+
},
497+
"default": {
498+
"description": "DefaultErrorResponse",
499+
"schema": {
500+
"$ref": "#/definitions/Error"
501+
}
502+
}
503+
},
504+
"deprecated": false,
505+
"x-ms-pageable": {
506+
"nextLinkName": "nextLink"
507+
},
508+
"produces": [
509+
"application/json"
510+
],
511+
"consumes": [
512+
"application/json"
513+
]
514+
}
452515
}
453516
},
454517
"x-ms-paths": {
@@ -672,6 +735,24 @@
672735
"description": "Message"
673736
}
674737
}
738+
},
739+
"EventHubConsumerGroupsListResult": {
740+
"description": "The JSON-serialized array of Event Hub-compatible consumer group names with a next link.",
741+
"type": "object",
742+
"properties": {
743+
"value": {
744+
"description": "The array of Event Hub-compatible consumer group names.",
745+
"type": "array",
746+
"items": {
747+
"type": "string"
748+
}
749+
},
750+
"nextLink": {
751+
"description": "The next link.",
752+
"type": "string",
753+
"readOnly": true
754+
}
755+
}
675756
}
676757
},
677758
"parameters": {

0 commit comments

Comments
 (0)