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

Commit 44dda19

Browse files
authored
Add complex parameter flattening support (#279)
* Add complex parameter flattening support -Enabled generation of 'flattening' property in x-ps-parameter-info for the swagger operations and global parameters. -Enabled flattening functionality for the complex typed parameters for the swagger operations. -Enabled 'New-<Type>Object' function generation for the complex types used for the global parameters -Added PS approved verb mapping for Undelete = 'Restore'.
1 parent e4ef817 commit 44dda19

12 files changed

+1414
-43
lines changed

PSSwagger/Definitions.psm1

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function Get-DefinitionParameterType
440440
return $ParameterType
441441
}
442442

443-
function New-SwaggerDefinitionCommand
443+
function Expand-SwaggerDefinition
444444
{
445445
[CmdletBinding()]
446446
param
@@ -449,10 +449,6 @@ function New-SwaggerDefinitionCommand
449449
[hashtable]
450450
$DefinitionFunctionsDetails,
451451

452-
[Parameter(Mandatory = $true)]
453-
[hashtable]
454-
$SwaggerMetaDict,
455-
456452
[Parameter(Mandatory = $true)]
457453
[string]
458454
$NameSpace,
@@ -464,11 +460,6 @@ function New-SwaggerDefinitionCommand
464460

465461
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
466462

467-
$FunctionsToExport = @()
468-
$GeneratedCommandsPath = Join-Path -Path $SwaggerMetaDict['outputDirectory'] -ChildPath $GeneratedCommandsName
469-
$SwaggerDefinitionCommandsPath = Join-Path -Path $GeneratedCommandsPath -ChildPath 'SwaggerDefinitionCommands'
470-
$FormatFilesPath = Join-Path -Path $GeneratedCommandsPath -ChildPath 'FormatFiles'
471-
472463
# Expand the definition parameters from 'AllOf' definitions and x_ms_client-flatten declarations.
473464
$ExpandedAllDefinitions = $false
474465

@@ -553,6 +544,36 @@ function New-SwaggerDefinitionCommand
553544
Set-GenerateDefinitionCmdlet -DefinitionFunctionsDetails $DefinitionFunctionsDetails -FunctionDetails $FunctionDetails -ModelsNamespaceWithDot "$Namespace.$Models."
554545
}
555546
}
547+
}
548+
549+
function New-SwaggerDefinitionCommand
550+
{
551+
[CmdletBinding()]
552+
param
553+
(
554+
[Parameter(Mandatory = $true)]
555+
[hashtable]
556+
$DefinitionFunctionsDetails,
557+
558+
[Parameter(Mandatory = $true)]
559+
[hashtable]
560+
$SwaggerMetaDict,
561+
562+
[Parameter(Mandatory = $true)]
563+
[string]
564+
$NameSpace,
565+
566+
[Parameter(Mandatory = $true)]
567+
[string]
568+
$Models
569+
)
570+
571+
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
572+
573+
$FunctionsToExport = @()
574+
$GeneratedCommandsPath = Join-Path -Path $SwaggerMetaDict['outputDirectory'] -ChildPath $GeneratedCommandsName
575+
$SwaggerDefinitionCommandsPath = Join-Path -Path $GeneratedCommandsPath -ChildPath 'SwaggerDefinitionCommands'
576+
$FormatFilesPath = Join-Path -Path $GeneratedCommandsPath -ChildPath 'FormatFiles'
556577

557578
$DefinitionFunctionsDetails.GetEnumerator() | ForEach-Object {
558579
$FunctionDetails = $_.Value

PSSwagger/PSCommandVerbMap.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ $script:PSCommandVerbMap = @{
9090
Nullify = 'Clear'
9191

9292
Recover = 'Restore'
93+
Undelete = 'Restore'
9394

9495
Synchronize = 'Sync'
9596
Synch = 'Sync'

PSSwagger/PSSwagger.Constants.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ $parameterGroupPropertyExpression = @'
156156
if (`$PSBoundParameters.ContainsKey('$parameterGroupPropertyName')) { `$$groupName.$parameterGroupPropertyName = `$$parameterGroupPropertyName }
157157
'@
158158

159+
$constructFlattenedParameter = @'
160+
161+
`$flattenedParameters = $flattenedParametersListStr
162+
`$utilityCmdParams = @{}
163+
`$flattenedParameters | ForEach-Object {
164+
if(`$PSBoundParameters.ContainsKey(`$_)) {
165+
`$utilityCmdParams[`$_] = `$PSBoundParameters[`$_]
166+
}
167+
}
168+
`$$SwaggerOperationParameterName = New-$($FlattenedParamType)Object @utilityCmdParams
169+
170+
'@
171+
159172
$functionBodyStr = @'
160173
161174
`$ErrorActionPreference = 'Stop'
@@ -166,6 +179,7 @@ $functionBodyStr = @'
166179
$GlobalParameterBlock
167180
$oDataExpressionBlock
168181
$parameterGroupsExpressionBlock
182+
$flattenedParametersBlock
169183
170184
`$skippedCount = 0
171185
`$returnedCount = 0

PSSwagger/PSSwagger.Resources.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ ConvertFrom-StringData @'
8181
PSMetaFileExists=The PowerShell metadata file '{0}' already exists for the specified Swagger document '{1}'. Use -Force switch parameter to replace the existing metadata file '{0}'.
8282
SuccessfullyGeneratedMetadataFile=Successfully generated the PowerShell metadata file '{0}' for the specified Swagger specification '{1}'.
8383
NewPSSwaggerMetadataFileOperationMessage=Create PowerShell Swagger Metadata file
84+
ServiceTypeMetadataFileNotFound=The service type metadata file '{0}' does not exist.
85+
UnknownPSMetadataProperty=Unknown '{0}' property: '{1}'.
86+
InvalidPSMetaFlattenParameter=Flatten property is specified as 'true' for an invalid parameter '{0}' with type '{1}'.
8487
###PSLOC
8588
'@

PSSwagger/PSSwagger.psm1

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ function New-PSSwaggerModule
256256
return
257257
}
258258

259+
# Get the PowerShell Metadata if .psmeta.json file is available.
260+
$PSMetaJsonObject = $null
261+
$PSMetaFilePath = [regex]::replace($SwaggerSpecPath, ".json$", ".psmeta.json")
262+
if (Test-Path -Path $PSMetaFilePath -PathType Leaf) {
263+
$PSMetaJsonObject = ConvertFrom-Json -InputObject ((Get-Content -Path $PSMetaFilePath) -join [Environment]::NewLine) -ErrorAction Stop
264+
}
265+
259266
if ($PSCmdlet.ParameterSetName -eq 'SwaggerPath')
260267
{
261268
$jsonObject = ConvertFrom-Json -InputObject ((Get-Content -Path $SwaggerSpecPath) -join [Environment]::NewLine) -ErrorAction Stop
@@ -339,6 +346,7 @@ function New-PSSwaggerModule
339346
CustomAuthCommand = ""
340347
HostOverrideCommand = ""
341348
NoAuthChallenge = $false
349+
NameSpacePrefix = ''
342350
}
343351

344352
# Parse the JSON and populate the dictionary
@@ -351,19 +359,22 @@ function New-PSSwaggerModule
351359
DefinitionFunctionsDetails = $DefinitionFunctionsDetails
352360
AzureSpec = $UseAzureCsharpGenerator
353361
PowerShellCodeGen = $PowerShellCodeGen
362+
PSMetaJsonObject = $PSMetaJsonObject
354363
}
355364
$swaggerDict = ConvertTo-SwaggerDictionary @ConvertToSwaggerDictionary_params
356365

357-
Get-PowerShellCodeGenSettings -Path $SwaggerSpecPath -CodeGenSettings $PowerShellCodeGen
358-
foreach ($additionalSwaggerSpecPath in $SwaggerSpecFilePaths) {
359-
Get-PowerShellCodeGenSettings -Path $additionalSwaggerSpecPath -CodeGenSettings $PowerShellCodeGen
366+
Get-PowerShellCodeGenSettings -Path $SwaggerSpecPath -CodeGenSettings $PowerShellCodeGen -PSMetaJsonObject $PSMetaJsonObject
367+
if(-not $PSMetaJsonObject) {
368+
foreach ($additionalSwaggerSpecPath in $SwaggerSpecFilePaths) {
369+
Get-PowerShellCodeGenSettings -Path $additionalSwaggerSpecPath -CodeGenSettings $PowerShellCodeGen
370+
}
360371
}
361372

362373
# Expand partner metadata
363374
if ($PowerShellCodeGen['ServiceType']) {
364375
$partnerFilePath = Join-Path -Path $PSScriptRoot -ChildPath "ServiceTypes" | Join-Path -ChildPath "$($PowerShellCodeGen['ServiceType'].ToLowerInvariant()).PSMeta.json"
365-
if (-not (Test-Path -Path $partnerFilePath)) {
366-
Write-Warning -Message "Service type metadata file doesn't exist: $partnerFilePath"
376+
if (-not (Test-Path -Path $partnerFilePath -PathType Leaf)) {
377+
Write-Warning -Message ($LocalizedData.ServiceTypeMetadataFileNotFound -f $partnerFilePath)
367378
} else {
368379
Get-PowerShellCodeGenSettings -Path $partnerFilePath -CodeGenSettings $PowerShellCodeGen
369380
}
@@ -388,8 +399,8 @@ function New-PSSwaggerModule
388399
}
389400
}
390401

391-
$null = New-Item -ItemType Directory $outputDirectory -Force -ErrorAction Stop
392-
$null = New-Item -ItemType Directory $SymbolPath -Force -ErrorAction Stop
402+
$null = New-Item -ItemType Directory $outputDirectory -Force -ErrorAction Stop -Confirm:$false -WhatIf:$false
403+
$null = New-Item -ItemType Directory $SymbolPath -Force -ErrorAction Stop -Confirm:$false -WhatIf:$false
393404

394405
$swaggerMetaDict = @{
395406
OutputDirectory = $outputDirectory
@@ -424,7 +435,8 @@ function New-PSSwaggerModule
424435
-SwaggerDict $swaggerDict `
425436
-SwaggerMetaDict $swaggerMetaDict `
426437
-DefinitionFunctionsDetails $DefinitionFunctionsDetails `
427-
-ParameterGroupCache $ParameterGroupCache
438+
-ParameterGroupCache $ParameterGroupCache `
439+
-PSMetaJsonObject $PSMetaJsonObject
428440
}
429441
}
430442

@@ -436,7 +448,9 @@ function New-PSSwaggerModule
436448
-SwaggerDict $swaggerDict `
437449
-SwaggerMetaDict $swaggerMetaDict `
438450
-DefinitionFunctionsDetails $DefinitionFunctionsDetails `
439-
-ParameterGroupCache $ParameterGroupCache
451+
-ParameterGroupCache $ParameterGroupCache `
452+
-PSMetaJsonObject $PSMetaJsonObject
453+
440454
}
441455
}
442456
}
@@ -454,10 +468,14 @@ function New-PSSwaggerModule
454468
$PathFunctionDetails = $codePhaseResult.PathFunctionDetails
455469
$generatedCSharpFilePath = $codePhaseResult.GeneratedCSharpPath
456470

471+
# Need to expand the definitions early as parameter flattening feature requires the parameters list of the definition/model types.
472+
Expand-SwaggerDefinition -DefinitionFunctionsDetails $DefinitionFunctionsDetails -NameSpace $NameSpace -Models $Models
473+
457474
$FunctionsToExport = @()
458475
$FunctionsToExport += New-SwaggerSpecPathCommand -PathFunctionDetails $PathFunctionDetails `
459476
-SwaggerMetaDict $swaggerMetaDict `
460-
-SwaggerDict $swaggerDict
477+
-SwaggerDict $swaggerDict `
478+
-DefinitionFunctionsDetails $DefinitionFunctionsDetails
461479

462480
$FunctionsToExport += New-SwaggerDefinitionCommand -DefinitionFunctionsDetails $DefinitionFunctionsDetails `
463481
-SwaggerMetaDict $swaggerMetaDict `
@@ -545,7 +563,7 @@ function ConvertTo-CsharpCode
545563

546564
$clrPath = Join-Path -Path $outputDirectory -ChildPath 'ref' | Join-Path -ChildPath 'fullclr'
547565
if (-not (Test-Path -Path $clrPath)) {
548-
$null = New-Item -Path $clrPath -ItemType Directory
566+
$null = New-Item -Path $clrPath -ItemType Directory -Force -Confirm:$false -WhatIf:$false
549567
}
550568

551569
$outAssembly = "$NameSpace.dll"
@@ -705,7 +723,7 @@ function ConvertTo-CsharpCode
705723
}
706724

707725
if (-not (Test-Path -Path $clrPath)) {
708-
$null = New-Item $clrPath -ItemType Directory
726+
$null = New-Item $clrPath -ItemType Directory -Force -Confirm:$false -WhatIf:$false
709727
}
710728
$dependencies = Get-PSSwaggerExternalDependencies -Azure:$codeCreatedByAzureGenerator -Framework 'netstandard1'
711729
$microsoftRestClientRuntimeAzureRequiredVersion = if ($dependencies.ContainsKey('Microsoft.Rest.ClientRuntime.Azure')) { $dependencies['Microsoft.Rest.ClientRuntime.Azure'].RequiredVersion } else { '' }

PSSwagger/PSSwaggerMetadata.psm1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function New-PSSwaggerMetadataFile {
7575
)
7676

7777
# Validate swagger path
78-
if (-not (Test-path -Path $SwaggerSpecPath)) {
78+
if (-not (Test-path -Path $SwaggerSpecPath -PathType Leaf)) {
7979
throw $LocalizedData.SwaggerSpecPathNotExist -f ($SwaggerSpecPath)
8080
return
8181
}
@@ -424,6 +424,7 @@ function Get-PathsPSMetadata {
424424
$x_ps_parameter_info = [ordered]@{
425425
name = $parameterName
426426
description = $paramDetails.Description
427+
flatten = $false
427428
}
428429
$parametersPSMetadata[$paramDetails.OriginalParameterName] = [ordered]@{
429430
'x-ps-parameter-info' = $x_ps_parameter_info
@@ -446,6 +447,7 @@ function Get-PathsPSMetadata {
446447
$x_ps_parameter_info = [ordered]@{
447448
name = $parameterName
448449
description = $paramDetails.Description
450+
flatten = $false
449451
}
450452
$pathItemFieldPSMetadata[$paramDetails.OriginalParameterName] = [ordered]@{
451453
'x-ps-parameter-info' = $x_ps_parameter_info
@@ -482,6 +484,7 @@ function Get-GlobalParametersPSMetadata {
482484
$x_ps_parameter_info = [ordered]@{
483485
name = $parameterDetails.Name
484486
description = $parameterDetails.Description
487+
flatten = $false
485488
}
486489
$parameterPSMetadata = [ordered]@{
487490
'x-ps-parameter-info' = $x_ps_parameter_info

0 commit comments

Comments
 (0)