Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions PSSwagger/PSSwagger.Constants.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ if($hostOverrideCommand){
$hostOverrideCommand
`'@"
}
if($GlobalParameters -or $GlobalParametersStatic) {
"
`$GlobalParameterHashtable = @{}
`$NewServiceClient_params['GlobalParameterHashtable'] = `$GlobalParameterHashtable
"
}
if($GlobalParameters) {
'
$GlobalParameterHashtable = @{} '

foreach($parameter in $GlobalParameters) {
"
`$GlobalParameterHashtable['$parameter'] = `$null
Expand All @@ -203,8 +206,13 @@ if($GlobalParameters) {
}
"
}
"
`$NewServiceClient_params['GlobalParameterHashtable'] = `$GlobalParameterHashtable "
}
if ($GlobalParametersStatic) {
foreach ($entry in $GlobalParametersStatic.GetEnumerator()) {
"
`$GlobalParameterHashtable['$($entry.Name)'] = $($entry.Value)
"
}
}
)
$clientName = New-ServiceClient @NewServiceClient_params
Expand Down Expand Up @@ -309,13 +317,13 @@ if($PageTypePagingObjectStr) {
}

`$PSCommonParameters = Get-PSCommonParameter -CallerPSBoundParameters `$PSBoundParameters

`$TaskHelperFilePath = Join-Path -Path `$ExecutionContext.SessionState.Module.ModuleBase -ChildPath 'Get-TaskResult.ps1'
if(`$AsJob)
{
`$ScriptBlockParameters = New-Object -TypeName 'System.Collections.Generic.Dictionary[string,object]'
`$ScriptBlockParameters['TaskResult'] = `$TaskResult
`$ScriptBlockParameters['AsJob'] = `$AsJob
`$ScriptBlockParameters['TaskHelperFilePath'] = Join-Path -Path `$ExecutionContext.SessionState.Module.ModuleBase -ChildPath 'Get-TaskResult.ps1'
`$ScriptBlockParameters['TaskHelperFilePath'] = `$TaskHelperFilePath
`$PSCommonParameters.GetEnumerator() | ForEach-Object { `$ScriptBlockParameters[`$_.Name] = `$_.Value }

Start-PSSwaggerJobHelper -ScriptBlock `$PSSwaggerJobScriptBlock ``
Expand All @@ -326,7 +334,7 @@ if($PageTypePagingObjectStr) {
else
{
Invoke-Command -ScriptBlock `$PSSwaggerJobScriptBlock ``
-ArgumentList `$TaskResult ``
-ArgumentList `$TaskResult,`$TaskHelperFilePath ``
@PSCommonParameters
}
'@
Expand Down
109 changes: 90 additions & 19 deletions PSSwagger/Paths.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -135,31 +135,56 @@ function Get-SwaggerSpecPathInfo
(Get-Member -InputObject $PSMetaPathJsonObject -Name $operationType)) {
$PSMetaOperationJsonObject = $PSMetaPathJsonObject.$operationType
}

if($PSMetaOperationJsonObject -and
(Get-Member -InputObject $PSMetaOperationJsonObject -Name 'x-ps-cmdlet-infos')) {
$PSMetaOperationJsonObject.'x-ps-cmdlet-infos' | ForEach-Object {
$cmdletInfoOverrides += @{
Name = $_.name
}
}
}
elseif ((Get-Member -InputObject $_.Value -Name 'x-ps-cmdlet-infos') -and $_.Value.'x-ps-cmdlet-infos') {
foreach ($cmdletMetadata in $_.Value.'x-ps-cmdlet-infos') {
$cmdletInfoOverride = @{}
if ((Get-Member -InputObject $cmdletMetadata -Name 'name') -and $cmdletMetadata.name) {
$cmdletInfoOverride['name'] = $cmdletMetadata.name
}

$cmdletInfoOverrides += $cmdletInfoOverride
}
}

if(Get-Member -InputObject $_.Value -Name 'OperationId')
{
$operationId = $_.Value.operationId
Write-Verbose -Message ($LocalizedData.GettingSwaggerSpecPathInfo -f $operationId)

$defaultCommandNames = Get-PathCommandName -OperationId $operationId
if($PSMetaOperationJsonObject -and
(Get-Member -InputObject $PSMetaOperationJsonObject -Name 'x-ps-cmdlet-infos')) {
$PSMetaOperationJsonObject.'x-ps-cmdlet-infos' | ForEach-Object {
$cmdletInfoOverride = @{
Name = $_.name
Metadata = $_
}
# If no name override is specified, apply all these overrides to each default command name
if (-not $_.name) {
foreach ($defaultCommandName in $defaultCommandNames) {
$cmdletInfoOverrides += @{
Name = $defaultCommandName.name
Metadata = $cmdletInfoOverride.Metadata
}
}
} else {
$cmdletInfoOverrides += $cmdletInfoOverride
}
}
}
elseif ((Get-Member -InputObject $_.Value -Name 'x-ps-cmdlet-infos') -and $_.Value.'x-ps-cmdlet-infos') {
foreach ($cmdletMetadata in $_.Value.'x-ps-cmdlet-infos') {
$cmdletInfoOverride = @{
Metadata = $cmdletMetadata
}
if ((Get-Member -InputObject $cmdletMetadata -Name 'name') -and $cmdletMetadata.name) {
$cmdletInfoOverride['name'] = $cmdletMetadata.name
}

# If no name override is specified, apply all these overrides to each default command name
if (-not (Get-Member -InputObject $cmdletMetadata -Name 'name')) {
foreach ($defaultCommandName in $defaultCommandNames) {
$cmdletInfoOverrides += @{
Name = $defaultCommandName.name
Metadata = $cmdletInfoOverride.Metadata
}
}
} else {
$cmdletInfoOverrides += $cmdletInfoOverride
}
}
}

$FunctionDescription = ""
if((Get-Member -InputObject $_.value -Name 'description') -and $_.value.description) {
$FunctionDescription = $_.value.description
Expand Down Expand Up @@ -347,6 +372,10 @@ function Get-SwaggerSpecPathInfo
$FunctionDetails['x-ms-long-running-operation'] = $longRunningOperation
}

if ($_.ContainsKey('Metadata') -and (-not $FunctionDetails.ContainsKey("Metadata"))) {
$FunctionDetails['Metadata'] = $_.Metadata
}

if ($operationSecurityObject) {
$FunctionDetails['Security'] = $operationSecurityObject
}
Expand Down Expand Up @@ -558,6 +587,21 @@ function New-SwaggerPath
$parameterHitCount = @{}
$globalParameters = @()
$x_ms_pageableObject = $null
$globalParametersStatic = @{}
# Process global metadata for commands
if ($SwaggerDict.ContainsKey('CommandDefaults')) {
foreach ($entry in $SwaggerDict['CommandDefaults'].GetEnumerator()) {
$globalParametersStatic[$entry.Name] = Get-ValueText($entry.Value)
}
}
# Process metadata for the overall command
if ($FunctionDetails.ContainsKey('Metadata')) {
if (Get-Member -InputObject $FunctionDetails['Metadata'] -Name 'ClientParameters') {
foreach ($property in (Get-Member -InputObject $FunctionDetails['Metadata'].ClientParameters -MemberType NoteProperty)) {
$globalParametersStatic[$property.Name] = Get-ValueText($FunctionDetails['Metadata'].ClientParameters.$($property.Name))
}
}
}
foreach ($parameterSetDetail in $parameterSetDetails) {
if ($parameterSetDetail.ContainsKey('x-ms-pageable') -and $parameterSetDetail.'x-ms-pageable' -and (-not $isNextPageOperation)) {
if ($x_ms_pageableObject -and
Expand Down Expand Up @@ -1144,6 +1188,9 @@ function New-SwaggerPath
if($globalParameters) {
$functionBodyParams['GlobalParameters'] = $globalParameters
}
if ($globalParametersStatic) {
$functionBodyParams['GlobalParametersStatic'] = $globalParametersStatic
}

$pathGenerationPhaseResult = Get-PathFunctionBody @functionBodyParams
$bodyObject = $pathGenerationPhaseResult.BodyObject
Expand Down Expand Up @@ -1542,4 +1589,28 @@ function Get-TemporaryCliXmlFilePath {
$random = [Guid]::NewGuid().Guid
$filePath = Join-Path -Path (Get-XDGDirectory -DirectoryType Cache) -ChildPath "$FullClientTypeName.$random.xml"
return $filePath
}
<#
.SYNOPSIS
Convert an object into a string to represents the value in PowerShell

.EXAMPLE
[string]this is a string => 'this is a string'
[bool]true => $true
[int]5 => 5
#>
function Get-ValueText {
param(
[Parameter(Mandatory=$true)]
[object]
$obj
)

if ($obj -is [string]) {
return "'$obj'"
} elseif ($obj -is [bool]) {
return "`$$obj"
} else {
return $obj
}
}
21 changes: 20 additions & 1 deletion PSSwagger/SwaggerUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ function ConvertTo-SwaggerDictionary {
$swaggerDict['Info']['Header'] = $Header
}

if ((Get-Member -InputObject $swaggerDocObject.info -Name 'x-ps-module-info') -and
(Get-Member -InputObject $swaggerDocObject.info.'x-ps-module-info' -Name 'commandDefaults')) {
$swaggerDict['CommandDefaults'] = @{}
foreach ($property in (Get-Member -InputObject $swaggerDocObject.info.'x-ps-module-info'.commandDefaults -MemberType NoteProperty)) {
$swaggerDict['CommandDefaults'][$property.Name] = $swaggerDocObject.info.'x-ps-module-info'.commandDefaults.$($property.Name)
}
} elseif ($PSMetaJsonObject -and (Get-Member -InputObject $PSMetaJsonObject -Name 'info') -and
(Get-Member -InputObject $PSMetaJsonObject.info -Name 'x-ps-module-info') -and
(Get-Member -InputObject $PSMetaJsonObject.info.'x-ps-module-info' -Name 'commandDefaults')) {
$swaggerDict['CommandDefaults'] = @{}
foreach ($property in (Get-Member -InputObject $PSMetaJsonObject.info.'x-ps-module-info'.commandDefaults -MemberType NoteProperty)) {
$swaggerDict['CommandDefaults'][$property.Name] = $PSMetaJsonObject.info.'x-ps-module-info'.commandDefaults.$($property.Name)
}
}

$SwaggerParameters = @{}
$SwaggerDefinitions = @{}
$SwaggerPaths = @{}
Expand Down Expand Up @@ -1491,7 +1506,11 @@ function Get-PathFunctionBody

[Parameter(Mandatory=$true)]
[PSCustomObject]
$ParameterAliasMapping
$ParameterAliasMapping,

[Parameter(Mandatory=$false)]
[PSCustomObject]
$GlobalParametersStatic
)

Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
Expand Down
24 changes: 23 additions & 1 deletion docs/extensions/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ These can also be used in the Swagger/OpenApi document.
* [x-ps-cmdlet-infos](#x-ps-cmdlet-infos) - enables customization of generated commands for an operation.
* [x-ps-parameter-info](#x-ps-parameter-info) - enables customization for generated command parameters.
* [x-ps-output-format-info](#x-ps-output-format-info) - enables customization of output format views.

* [x-ps-client-parameters](#x-ps-client-parameters) - enables customization of AutoRest ServiceClient properties.


## x-ps-code-generation-settings
Expand Down Expand Up @@ -65,6 +65,7 @@ moduleVersion | `string` | **Required**. Module version.
description | `string` | **Required**. Module description.
author | `string` | **Required**. Module author.
guid | `string` | **Required**. Module guid. MUST be in the form of valid guid.
commandDefaults | `x-ps-client-parameters` | Default client parameters to apply to all commands.
.*| `string` or `bool` | Field name should be a valid parameters of either New-ModuleManifest or Update-ModuleManifest cmdlets. Value should be a valid string value, boolean for switch parameters, must be proper URL as string value for URI types - e.g., licenseUri, or array of strings if a parameter supports array of strings - e.g., Tags.

**Example**:
Expand Down Expand Up @@ -111,6 +112,7 @@ description | `string` | Description of PowerShell Cmdlet. If not specified, des
generateCommand | `bool` | Boolean to indicate whether a cmdlet is required or not. Default is true.
defaultParameterSet | `string` | String value to indicate whether the specified OperationId or Definition name as the default parameter set name when multiple Operations or definitions are merged into the same cmdlet, e.g., Get and List operationIds can be combined into single cmdlet.
generateOutputFormat | `bool` | Applicable to definitions only. Boolean to indicate whether output format file is required for this model type or not. Default value true.
clientParameters | `x-ps-client-parameters` | Client parameters for this command. Overrides global parameters.

**Examples**:
1. x-ps-cmdlet-infos on operation
Expand Down Expand Up @@ -329,3 +331,23 @@ width | `int` | **Required**. Expected width of this property in output format.
}
}
```

## x-ps-client-parameters
Defines property overrides for AutoRest ServiceClient classes.

**Parent element**: `x-ps-module-info Object` or `x-ps-cmdlet-info Object`

**Schema**:

Field Name | Type | Description
---|:---:|---
AcceptLangugage| `string` | Preferred language for response.
LongRunningOperationRetryTimeout | `int` | Retry timeout in seconds for Long Running Operations.

**Examples**:
```json5
{
"AcceptLanguage": "EN-GB",
"LongRunningOperationRetryTimeout": 9001
}
```