[PowerShell] discard readonly properties in Initialize cmdlets (#6754)

* ValidatePattern having double quote(") throws exception on running Build.ps1

* fix tab with space

* [powershell-experimental] : http signature auth

* fix the tab issue

* merge cnflict fix for powershell experimental

* Htpp signing : added support for ecdsa

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

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

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

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

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

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

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

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

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

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

* HttpSigningHeader accepts any header available in request to calculate the signature.

* Update modules/openapi-generator/src/main/resources/powershell/http_signature_auth.mustache

Co-authored-by: Sebastien Rosset <serosset@cisco.com>

* Incorporated the review comments

* addressed the merge conflict

* discard readonly property in initialize cmdlets

* update doc

* update powershell sample

Co-authored-by: Ghufran Zahidi <gzahidi@cisco.com>
Co-authored-by: Sebastien Rosset <serosset@cisco.com>
Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
Ghufz 2020-06-25 14:04:38 +05:30 committed by GitHub
parent 6b877efe1f
commit 13fe079901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 148 additions and 42 deletions

View File

@ -7,6 +7,7 @@ sidebar_label: powershell
| ------ | ----------- | ------ | ------- | | ------ | ----------- | ------ | ------- |
|apiNamePrefix|Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet =&gt; PSPet.| |null| |apiNamePrefix|Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet =&gt; PSPet.| |null|
|commonVerbs|PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly.| |null| |commonVerbs|PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly.| |null|
|discardReadOnly|Set discardReadonly to true to generate the Initialize cmdlet without readonly parameters| |null|
|packageGuid|GUID for PowerShell module (e.g. a27b908d-2a20-467f-bc32-af6f3a654ac5). A random GUID will be generated by default.| |null| |packageGuid|GUID for PowerShell module (e.g. a27b908d-2a20-467f-bc32-af6f3a654ac5). A random GUID will be generated by default.| |null|
|packageName|Client package name (e.g. PSTwitter).| |PSOpenAPITools| |packageName|Client package name (e.g. PSTwitter).| |PSOpenAPITools|
|packageVersion|Package version (e.g. 0.1.2).| |0.1.2| |packageVersion|Package version (e.g. 0.1.2).| |0.1.2|

View File

@ -54,6 +54,7 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
protected Map<String, String> commonVerbs; // verbs not in the official ps verb list but can be mapped to one of the verbs protected Map<String, String> commonVerbs; // verbs not in the official ps verb list but can be mapped to one of the verbs
protected HashSet methodNames; // store a list of method names to detect duplicates protected HashSet methodNames; // store a list of method names to detect duplicates
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
protected boolean discardReadOnly = false; // Discard the readonly property in initialize cmdlet
/** /**
* Constructs an instance of `PowerShellClientCodegen`. * Constructs an instance of `PowerShellClientCodegen`.
@ -499,6 +500,7 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, "Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => PSPet.")); cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, "Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => PSPet."));
cliOptions.add(new CliOption("commonVerbs", "PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly.")); cliOptions.add(new CliOption("commonVerbs", "PS common verb mappings. e.g. Delete=Remove:Patch=Update to map Delete with Remove and Patch with Update accordingly."));
cliOptions.add(new CliOption(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC)); cliOptions.add(new CliOption(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC));
cliOptions.add(new CliOption("discardReadOnly", "Set discardReadonly to true to generate the Initialize cmdlet without readonly parameters"));
} }
public CodegenType getTag() { public CodegenType getTag() {
@ -542,6 +544,14 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
public boolean getUseOneOfDiscriminatorLookup() { public boolean getUseOneOfDiscriminatorLookup() {
return this.useOneOfDiscriminatorLookup; return this.useOneOfDiscriminatorLookup;
} }
public void setDiscardReadOnly(boolean discardReadOnly) {
this.discardReadOnly = discardReadOnly;
}
public boolean getDiscardReadOnly() {
return this.discardReadOnly;
}
@Override @Override
public void processOpts() { public void processOpts() {
@ -565,6 +575,12 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, useOneOfDiscriminatorLookup); additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, useOneOfDiscriminatorLookup);
} }
if (additionalProperties.containsKey("discardReadOnly")) {
setDiscardReadOnly(convertPropertyToBooleanAndWriteBack("discardReadOnly"));
} else {
additionalProperties.put("discardReadOnly", discardReadOnly);
}
if (StringUtils.isNotBlank(powershellGalleryUrl)) { if (StringUtils.isNotBlank(powershellGalleryUrl)) {
// get the last segment of the URL // get the last segment of the URL
// e.g. https://www.powershellgallery.com/packages/PSTwitter => PSTwitter // e.g. https://www.powershellgallery.com/packages/PSTwitter => PSTwitter
@ -905,9 +921,19 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
for (Object _mo : models) { for (Object _mo : models) {
Map<String, Object> _model = (Map<String, Object>) _mo; Map<String, Object> _model = (Map<String, Object>) _mo;
CodegenModel model = (CodegenModel) _model.get("model"); CodegenModel model = (CodegenModel) _model.get("model");
CodegenProperty lastWritableProperty = null;
for (CodegenProperty cp : model.allVars) { for (CodegenProperty cp : model.allVars) {
cp.vendorExtensions.put("x-powershell-data-type", getPSDataType(cp)); cp.vendorExtensions.put("x-powershell-data-type", getPSDataType(cp));
if(this.discardReadOnly && !cp.isReadOnly) {
lastWritableProperty = cp;
}
}
// Mark the last readonly false property
if(this.discardReadOnly && lastWritableProperty != null) {
lastWritableProperty.vendorExtensions.put("x-lastWritable", true);
model.allVars.set(model.allVars.indexOf(lastWritableProperty), lastWritableProperty);
} }
// if oneOf contains "null" type // if oneOf contains "null" type

View File

@ -8,9 +8,16 @@
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}} {{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{#allVars}} {{#allVars}}
{{^discardReadOnly}}
.PARAMETER {{{name}}} .PARAMETER {{{name}}}
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}} {{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{/discardReadOnly}}
{{#discardReadOnly}}
{{^isReadOnly}}
.PARAMETER {{{name}}}
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{/isReadOnly}}
{{/discardReadOnly}}
{{/allVars}} {{/allVars}}
.OUTPUTS .OUTPUTS
@ -20,6 +27,26 @@
function Initialize-{{{apiNamePrefix}}}{{{classname}}} { function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
[CmdletBinding()] [CmdletBinding()]
Param ( Param (
{{#discardReadOnly}}
{{#allVars}}
{{^isReadOnly}}
[Parameter(ValueFromPipelineByPropertyName = $true)]
{{#pattern}}
[ValidatePattern("{{{.}}}")]
{{/pattern}}
{{#isEnum}}
{{#allowableValues}}
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})]
{{/allowableValues}}
{{/isEnum}}
[{{vendorExtensions.x-powershell-data-type}}]
{{=<% %>=}}
${<%name%>}<%^vendorExtensions.x-lastWritable%>,<%/vendorExtensions.x-lastWritable%>
<%={{ }}=%>
{{/isReadOnly}}
{{/allVars}}
{{/discardReadOnly}}
{{^discardReadOnly}}
{{#allVars}} {{#allVars}}
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)] [Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)]
{{#pattern}} {{#pattern}}
@ -35,12 +62,14 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
${<%name%>}<%^-last%>,<%/-last%> ${<%name%>}<%^-last%>,<%/-last%>
<%={{ }}=%> <%={{ }}=%>
{{/allVars}} {{/allVars}}
{{/discardReadOnly}}
) )
Process { Process {
'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug 'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
{{^discardReadOnly}}
{{#vars}} {{#vars}}
{{^isNullable}} {{^isNullable}}
{{#required}} {{#required}}
@ -88,14 +117,79 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
{{/minItems}} {{/minItems}}
{{/hasValidation}} {{/hasValidation}}
{{/vars}} {{/vars}}
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
{{=<< >>=}} {{=<< >>=}}
<<#allVars>> <<#allVars>>
"<<baseName>>" = ${<<name>>} "<<baseName>>" = ${<<name>>}
<</allVars>> <</allVars>>
<<={{ }}=>> <<={{ }}=>>
} }
{{/discardReadOnly}}
{{#discardReadOnly}}
{{#vars}}
{{^isReadOnly}}
{{^isNullable}}
{{#required}}
if (!${{{name}}}) {
throw "invalid value for '{{{name}}}', '{{{name}}}' cannot be null."
}
{{/required}}
{{/isNullable}}
{{#hasValidation}}
{{#maxLength}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxLength}}}) {
throw "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
}
{{/maxLength}}
{{#minLength}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minLength}}}) {
throw "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
}
{{/minLength}}
{{#maximum}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMaximum}}-ge{{/exclusiveMaximum}}{{^exclusiveMaximum}}-gt{{/exclusiveMaximum}} {{{maximum}}}) {
throw "invalid value for '{{{name}}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}."
}
{{/maximum}}
{{#minimum}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMinimum}}-le{{/exclusiveMinimum}}{{^exclusiveMinimum}}-lt{{/exclusiveMinimum}} {{{minimum}}}) {
throw "invalid value for '{{{name}}}', must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}."
}
{{/minimum}}
{{#maxItems}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxItems}}}) {
throw "invalid value for '{{{name}}}', number of items must be less than or equal to {{{maxItems}}}."
}
{{/maxItems}}
{{#minItems}}
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minItems}}}) {
throw "invalid value for '{{{name}}}', number of items must be greater than or equal to {{{minItems}}}."
}
{{/minItems}}
{{/hasValidation}}
{{/isReadOnly}}
{{/vars}}
$PSO = [PSCustomObject]@{
{{=<< >>=}}
<<#allVars>>
<<^isReadOnly>>
"<<baseName>>" = ${<<name>>}
<</isReadOnly>>
<</allVars>>
<<={{ }}=>>
}
{{/discardReadOnly}}
return $PSO return $PSO
} }

View File

@ -16,13 +16,10 @@ Describes the result of uploading an image resource
.PARAMETER Code .PARAMETER Code
No description available. No description available.
.PARAMETER Type .PARAMETER Type
No description available. No description available.
.PARAMETER Message .PARAMETER Message
No description available. No description available.
.OUTPUTS .OUTPUTS
ApiResponse<PSCustomObject> ApiResponse<PSCustomObject>
@ -46,11 +43,13 @@ function Initialize-PSApiResponse {
'Creating PSCustomObject: PSPetstore => PSApiResponse' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSApiResponse' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"code" = ${Code} "code" = ${Code}
"type" = ${Type} "type" = ${Type}
"message" = ${Message} "message" = ${Message}
} }
return $PSO return $PSO
} }

View File

@ -16,10 +16,8 @@ A category for a pet
.PARAMETER Id .PARAMETER Id
No description available. No description available.
.PARAMETER Name .PARAMETER Name
No description available. No description available.
.OUTPUTS .OUTPUTS
Category<PSCustomObject> Category<PSCustomObject>
@ -41,10 +39,12 @@ function Initialize-PSCategory {
'Creating PSCustomObject: PSPetstore => PSCategory' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSCategory' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"id" = ${Id} "id" = ${Id}
"name" = ${Name} "name" = ${Name}
} }
return $PSO return $PSO
} }

View File

@ -16,10 +16,8 @@ No description available.
.PARAMETER Name .PARAMETER Name
Updated name of the pet Updated name of the pet
.PARAMETER Status .PARAMETER Status
Updated status of the pet Updated status of the pet
.OUTPUTS .OUTPUTS
InlineObject<PSCustomObject> InlineObject<PSCustomObject>
@ -40,10 +38,12 @@ function Initialize-PSInlineObject {
'Creating PSCustomObject: PSPetstore => PSInlineObject' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSInlineObject' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"name" = ${Name} "name" = ${Name}
"status" = ${Status} "status" = ${Status}
} }
return $PSO return $PSO
} }

View File

@ -16,10 +16,8 @@ No description available.
.PARAMETER AdditionalMetadata .PARAMETER AdditionalMetadata
Additional data to pass to server Additional data to pass to server
.PARAMETER File .PARAMETER File
file to upload file to upload
.OUTPUTS .OUTPUTS
InlineObject1<PSCustomObject> InlineObject1<PSCustomObject>
@ -40,10 +38,12 @@ function Initialize-PSInlineObject1 {
'Creating PSCustomObject: PSPetstore => PSInlineObject1' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSInlineObject1' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"additionalMetadata" = ${AdditionalMetadata} "additionalMetadata" = ${AdditionalMetadata}
"file" = ${File} "file" = ${File}
} }
return $PSO return $PSO
} }

View File

@ -16,22 +16,16 @@ An order for a pets from the pet store
.PARAMETER Id .PARAMETER Id
No description available. No description available.
.PARAMETER PetId .PARAMETER PetId
No description available. No description available.
.PARAMETER Quantity .PARAMETER Quantity
No description available. No description available.
.PARAMETER ShipDate .PARAMETER ShipDate
No description available. No description available.
.PARAMETER Status .PARAMETER Status
Order Status Order Status
.PARAMETER Complete .PARAMETER Complete
No description available. No description available.
.OUTPUTS .OUTPUTS
Order<PSCustomObject> Order<PSCustomObject>
@ -65,7 +59,8 @@ function Initialize-PSOrder {
'Creating PSCustomObject: PSPetstore => PSOrder' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSOrder' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"id" = ${Id} "id" = ${Id}
"petId" = ${PetId} "petId" = ${PetId}
"quantity" = ${Quantity} "quantity" = ${Quantity}
@ -73,6 +68,7 @@ function Initialize-PSOrder {
"status" = ${Status} "status" = ${Status}
"complete" = ${Complete} "complete" = ${Complete}
} }
return $PSO return $PSO
} }

View File

@ -16,22 +16,16 @@ A pet for sale in the pet store
.PARAMETER Id .PARAMETER Id
No description available. No description available.
.PARAMETER Category .PARAMETER Category
No description available. No description available.
.PARAMETER Name .PARAMETER Name
No description available. No description available.
.PARAMETER PhotoUrls .PARAMETER PhotoUrls
No description available. No description available.
.PARAMETER Tags .PARAMETER Tags
No description available. No description available.
.PARAMETER Status .PARAMETER Status
pet status in the store pet status in the store
.OUTPUTS .OUTPUTS
Pet<PSCustomObject> Pet<PSCustomObject>
@ -73,7 +67,8 @@ function Initialize-PSPet {
throw "invalid value for 'PhotoUrls', 'PhotoUrls' cannot be null." throw "invalid value for 'PhotoUrls', 'PhotoUrls' cannot be null."
} }
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"id" = ${Id} "id" = ${Id}
"category" = ${Category} "category" = ${Category}
"name" = ${Name} "name" = ${Name}
@ -81,6 +76,7 @@ function Initialize-PSPet {
"tags" = ${Tags} "tags" = ${Tags}
"status" = ${Status} "status" = ${Status}
} }
return $PSO return $PSO
} }

View File

@ -16,10 +16,8 @@ A tag for a pet
.PARAMETER Id .PARAMETER Id
No description available. No description available.
.PARAMETER Name .PARAMETER Name
No description available. No description available.
.OUTPUTS .OUTPUTS
Tag<PSCustomObject> Tag<PSCustomObject>
@ -40,10 +38,12 @@ function Initialize-PSTag {
'Creating PSCustomObject: PSPetstore => PSTag' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSTag' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"id" = ${Id} "id" = ${Id}
"name" = ${Name} "name" = ${Name}
} }
return $PSO return $PSO
} }

View File

@ -16,28 +16,20 @@ A User who is purchasing from the pet store
.PARAMETER Id .PARAMETER Id
No description available. No description available.
.PARAMETER Username .PARAMETER Username
No description available. No description available.
.PARAMETER FirstName .PARAMETER FirstName
No description available. No description available.
.PARAMETER LastName .PARAMETER LastName
No description available. No description available.
.PARAMETER Email .PARAMETER Email
No description available. No description available.
.PARAMETER Password .PARAMETER Password
No description available. No description available.
.PARAMETER Phone .PARAMETER Phone
No description available. No description available.
.PARAMETER UserStatus .PARAMETER UserStatus
User Status User Status
.OUTPUTS .OUTPUTS
User<PSCustomObject> User<PSCustomObject>
@ -77,7 +69,8 @@ function Initialize-PSUser {
'Creating PSCustomObject: PSPetstore => PSUser' | Write-Debug 'Creating PSCustomObject: PSPetstore => PSUser' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug $PSBoundParameters | Out-DebugParameter | Write-Debug
$PSO = [PSCustomObject]@{
$PSO = [PSCustomObject]@{
"id" = ${Id} "id" = ${Id}
"username" = ${Username} "username" = ${Username}
"firstName" = ${FirstName} "firstName" = ${FirstName}
@ -87,6 +80,7 @@ function Initialize-PSUser {
"phone" = ${Phone} "phone" = ${Phone}
"userStatus" = ${UserStatus} "userStatus" = ${UserStatus}
} }
return $PSO return $PSO
} }