[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|
|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|
|packageName|Client package name (e.g. PSTwitter).| |PSOpenAPITools|
|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 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 discardReadOnly = false; // Discard the readonly property in initialize cmdlet
/**
* 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("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("discardReadOnly", "Set discardReadonly to true to generate the Initialize cmdlet without readonly parameters"));
}
public CodegenType getTag() {
@ -542,6 +544,14 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
public boolean getUseOneOfDiscriminatorLookup() {
return this.useOneOfDiscriminatorLookup;
}
public void setDiscardReadOnly(boolean discardReadOnly) {
this.discardReadOnly = discardReadOnly;
}
public boolean getDiscardReadOnly() {
return this.discardReadOnly;
}
@Override
public void processOpts() {
@ -565,6 +575,12 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, useOneOfDiscriminatorLookup);
}
if (additionalProperties.containsKey("discardReadOnly")) {
setDiscardReadOnly(convertPropertyToBooleanAndWriteBack("discardReadOnly"));
} else {
additionalProperties.put("discardReadOnly", discardReadOnly);
}
if (StringUtils.isNotBlank(powershellGalleryUrl)) {
// get the last segment of the URL
// e.g. https://www.powershellgallery.com/packages/PSTwitter => PSTwitter
@ -905,9 +921,19 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
for (Object _mo : models) {
Map<String, Object> _model = (Map<String, Object>) _mo;
CodegenModel model = (CodegenModel) _model.get("model");
CodegenProperty lastWritableProperty = null;
for (CodegenProperty cp : model.allVars) {
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

View File

@ -8,9 +8,16 @@
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{#allVars}}
{{^discardReadOnly}}
.PARAMETER {{{name}}}
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{/discardReadOnly}}
{{#discardReadOnly}}
{{^isReadOnly}}
.PARAMETER {{{name}}}
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
{{/isReadOnly}}
{{/discardReadOnly}}
{{/allVars}}
.OUTPUTS
@ -20,6 +27,26 @@
function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
[CmdletBinding()]
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}}
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)]
{{#pattern}}
@ -35,12 +62,14 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
${<%name%>}<%^-last%>,<%/-last%>
<%={{ }}=%>
{{/allVars}}
{{/discardReadOnly}}
)
Process {
'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug
$PSBoundParameters | Out-DebugParameter | Write-Debug
{{^discardReadOnly}}
{{#vars}}
{{^isNullable}}
{{#required}}
@ -88,14 +117,79 @@ function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
{{/minItems}}
{{/hasValidation}}
{{/vars}}
$PSO = [PSCustomObject]@{
{{/vars}}
$PSO = [PSCustomObject]@{
{{=<< >>=}}
<<#allVars>>
"<<baseName>>" = ${<<name>>}
<</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
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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