diff --git a/docs/generators/powershell.md b/docs/generators/powershell.md index 2b16a319698..df9d639de55 100644 --- a/docs/generators/powershell.md +++ b/docs/generators/powershell.md @@ -7,6 +7,7 @@ sidebar_label: powershell | ------ | ----------- | ------ | ------- | |apiNamePrefix|Prefix that will be appended to all PS objects. Default: empty string. e.g. Pet => 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| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java index 74a439c3d4c..7adc76f36ed 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java @@ -54,6 +54,7 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo protected Map 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 _model = (Map) _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 diff --git a/modules/openapi-generator/src/main/resources/powershell/model_simple.mustache b/modules/openapi-generator/src/main/resources/powershell/model_simple.mustache index aab5d1fd291..d3c3467c766 100644 --- a/modules/openapi-generator/src/main/resources/powershell/model_simple.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/model_simple.mustache @@ -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>> "<>" = ${<>} <> <<={{ }}=>> } + + {{/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>> + "<>" = ${<>} + <> + <> + <<={{ }}=>> + } + {{/discardReadOnly}} return $PSO } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/ApiResponse.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/ApiResponse.ps1 index 1cc58e9d5dc..92fe8175d23 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/ApiResponse.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/ApiResponse.ps1 @@ -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 @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/Category.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/Category.ps1 index 5d5e93da8e5..7c67f87d13d 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/Category.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/Category.ps1 @@ -16,10 +16,8 @@ A category for a pet .PARAMETER Id No description available. - .PARAMETER Name No description available. - .OUTPUTS Category @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject.ps1 index fbfde854a24..24d12c8d34f 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject.ps1 @@ -16,10 +16,8 @@ No description available. .PARAMETER Name Updated name of the pet - .PARAMETER Status Updated status of the pet - .OUTPUTS InlineObject @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject1.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject1.ps1 index cfd49e06365..189cbbd4618 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject1.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/InlineObject1.ps1 @@ -16,10 +16,8 @@ No description available. .PARAMETER AdditionalMetadata Additional data to pass to server - .PARAMETER File file to upload - .OUTPUTS InlineObject1 @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/Order.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/Order.ps1 index d74819356ad..5739f48e461 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/Order.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/Order.ps1 @@ -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 @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/Pet.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/Pet.ps1 index 4af7e7ecc82..e8eca455f6d 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/Pet.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/Pet.ps1 @@ -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 @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/Tag.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/Tag.ps1 index 8a7caf8d562..e906877f406 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/Tag.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/Tag.ps1 @@ -16,10 +16,8 @@ A tag for a pet .PARAMETER Id No description available. - .PARAMETER Name No description available. - .OUTPUTS Tag @@ -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 } diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/User.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/User.ps1 index 493322216a2..591f212b20e 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/User.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/User.ps1 @@ -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 @@ -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 }