From 29f0d22713c10a70b19c61b225c0d0d1557d6210 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 5 Sep 2023 15:45:03 +0800 Subject: [PATCH] [Powershell] add name mapping features (#16509) * add name mapping features to the powershell generator * update samples --- bin/configs/powershell.yaml | 4 + .../languages/PowerShellClientCodegen.java | 15 +++ ...odels-for-testing-with-http-signature.yaml | 7 ++ .../powershell/.openapi-generator/FILES | 2 + samples/client/petstore/powershell/README.md | 1 + .../petstore/powershell/docs/ModelMapping.md | 21 ++++ .../docs/ObjectWithDeprecatedFields.md | 4 +- .../src/PSPetstore/Model/NewModel.ps1 | 97 +++++++++++++++++++ .../Model/ObjectWithDeprecatedFields.ps1 | 17 +++- .../powershell/tests/Model/NewModel.Tests.ps1 | 17 ++++ 10 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 samples/client/petstore/powershell/docs/ModelMapping.md create mode 100644 samples/client/petstore/powershell/src/PSPetstore/Model/NewModel.ps1 create mode 100644 samples/client/petstore/powershell/tests/Model/NewModel.Tests.ps1 diff --git a/bin/configs/powershell.yaml b/bin/configs/powershell.yaml index 9a408c2ad17..3dccae4c8b7 100644 --- a/bin/configs/powershell.yaml +++ b/bin/configs/powershell.yaml @@ -14,3 +14,7 @@ additionalProperties: projectUri: https://github.com/OpenAPITools/openapi-generator releaseNotes: 'This is a sample project' tags: 'PetStore,powershell,sdk' +nameMappings: + name_mapping: SomethingElse +modelNameMappings: + model-mapping: NewModel 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 bf3a745b04f..fea2bc0a7ec 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 @@ -910,6 +910,11 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo */ @Override public String toModelName(String name) { + // obtain the name from modelNameMapping directly if provided + if (modelNameMapping.containsKey(name)) { + return modelNameMapping.get(name); + } + // check if schema-mapping has a different model for this class, so we can use it // instead of the auto-generated one. if (schemaMapping.containsKey(name)) { @@ -1016,6 +1021,11 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo @Override public String toParamName(String name) { + // obtain the name from parameterNameMapping directly if provided + if (parameterNameMapping.containsKey(name)) { + return parameterNameMapping.get(name); + } + // sanitize and camelize parameter name // pet_id => PetId name = camelize(sanitizeName(name)); @@ -1148,6 +1158,11 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo @Override public String toVarName(String name) { + // obtain the name from nameMapping directly if provided + if (nameMapping.containsKey(name)) { + return nameMapping.get(name); + } + // sanitize name name = sanitizeName(name); diff --git a/modules/openapi-generator/src/test/resources/3_0/powershell/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml b/modules/openapi-generator/src/test/resources/3_0/powershell/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml index 50056d0d050..6a77f1bd7e2 100644 --- a/modules/openapi-generator/src/test/resources/3_0/powershell/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/powershell/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml @@ -2129,6 +2129,11 @@ components: properties: name: type: string + model-mapping: + type: object + properties: + name: + type: string ObjectWithDeprecatedFields: type: object properties: @@ -2144,6 +2149,8 @@ components: deprecated: true items: $ref: '#/components/schemas/Bar' + name_mapping: + type: string PetWithRequiredTags: type: object required: diff --git a/samples/client/petstore/powershell/.openapi-generator/FILES b/samples/client/petstore/powershell/.openapi-generator/FILES index 13c81a7d5a1..7e8383cf879 100644 --- a/samples/client/petstore/powershell/.openapi-generator/FILES +++ b/samples/client/petstore/powershell/.openapi-generator/FILES @@ -42,6 +42,7 @@ docs/List.md docs/Mammal.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md +docs/ModelMapping.md docs/Name.md docs/NullableClass.md docs/NullableShape.md @@ -131,6 +132,7 @@ src/PSPetstore/Model/MixedPropertiesAndAdditionalPropertiesClass.ps1 src/PSPetstore/Model/Model200Response.ps1 src/PSPetstore/Model/ModelReturn.ps1 src/PSPetstore/Model/Name.ps1 +src/PSPetstore/Model/NewModel.ps1 src/PSPetstore/Model/NullableClass.ps1 src/PSPetstore/Model/NullableShape.ps1 src/PSPetstore/Model/NumberOnly.ps1 diff --git a/samples/client/petstore/powershell/README.md b/samples/client/petstore/powershell/README.md index 813c6dc2b72..30b0f1454ee 100644 --- a/samples/client/petstore/powershell/README.md +++ b/samples/client/petstore/powershell/README.md @@ -141,6 +141,7 @@ Class | Method | HTTP request | Description - [PSPetstore\Model.Model200Response](docs/Model200Response.md) - [PSPetstore\Model.ModelReturn](docs/ModelReturn.md) - [PSPetstore\Model.Name](docs/Name.md) + - [PSPetstore\Model.NewModel](docs/NewModel.md) - [PSPetstore\Model.NullableClass](docs/NullableClass.md) - [PSPetstore\Model.NullableShape](docs/NullableShape.md) - [PSPetstore\Model.NumberOnly](docs/NumberOnly.md) diff --git a/samples/client/petstore/powershell/docs/ModelMapping.md b/samples/client/petstore/powershell/docs/ModelMapping.md new file mode 100644 index 00000000000..399b227c2ae --- /dev/null +++ b/samples/client/petstore/powershell/docs/ModelMapping.md @@ -0,0 +1,21 @@ +# NewModel +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**Name** | **String** | | [optional] + +## Examples + +- Prepare the resource +```powershell +$NewModel = Initialize-PSPetstoreNewModel -Name null +``` + +- Convert the resource to JSON +```powershell +$NewModel | ConvertTo-JSON +``` + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + diff --git a/samples/client/petstore/powershell/docs/ObjectWithDeprecatedFields.md b/samples/client/petstore/powershell/docs/ObjectWithDeprecatedFields.md index e3911c6ecf8..d9a9570fa6f 100644 --- a/samples/client/petstore/powershell/docs/ObjectWithDeprecatedFields.md +++ b/samples/client/petstore/powershell/docs/ObjectWithDeprecatedFields.md @@ -7,6 +7,7 @@ Name | Type | Description | Notes **Id** | **Decimal** | | [optional] **DeprecatedRef** | [**DeprecatedObject**](DeprecatedObject.md) | | [optional] **Bars** | **String[]** | | [optional] +**SomethingElse** | **String** | | [optional] ## Examples @@ -15,7 +16,8 @@ Name | Type | Description | Notes $ObjectWithDeprecatedFields = Initialize-PSPetstoreObjectWithDeprecatedFields -Uuid null ` -Id null ` -DeprecatedRef null ` - -Bars null + -Bars null ` + -SomethingElse null ``` - Convert the resource to JSON diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/NewModel.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/NewModel.ps1 new file mode 100644 index 00000000000..71d54a76067 --- /dev/null +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/NewModel.ps1 @@ -0,0 +1,97 @@ +# +# OpenAPI Petstore +# This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: "" \ +# Version: 1.0.0 +# Generated by OpenAPI Generator: https://openapi-generator.tech +# + +<# +.SYNOPSIS + +No summary available. + +.DESCRIPTION + +No description available. + +.PARAMETER Name +No description available. +.OUTPUTS + +NewModel +#> + +function Initialize-PSNewModel { + [CmdletBinding()] + Param ( + [Parameter(Position = 0, ValueFromPipelineByPropertyName = $true)] + [String] + ${Name} + ) + + Process { + 'Creating PSCustomObject: PSPetstore => PSNewModel' | Write-Debug + $PSBoundParameters | Out-DebugParameter | Write-Debug + + + $PSO = [PSCustomObject]@{ + "name" = ${Name} + } + + + return $PSO + } +} + +<# +.SYNOPSIS + +Convert from JSON to NewModel + +.DESCRIPTION + +Convert from JSON to NewModel + +.PARAMETER Json + +Json object + +.OUTPUTS + +NewModel +#> +function ConvertFrom-PSJsonToNewModel { + Param( + [AllowEmptyString()] + [string]$Json + ) + + Process { + 'Converting JSON to PSCustomObject: PSPetstore => PSNewModel' | Write-Debug + $PSBoundParameters | Out-DebugParameter | Write-Debug + + $JsonParameters = ConvertFrom-Json -InputObject $Json + + # check if Json contains properties not defined in PSNewModel + $AllProperties = ("name") + foreach ($name in $JsonParameters.PsObject.Properties.Name) { + if (!($AllProperties.Contains($name))) { + throw "Error! JSON key '$name' not found in the properties: $($AllProperties)" + } + } + + if (!([bool]($JsonParameters.PSobject.Properties.name -match "name"))) { #optional property not found + $Name = $null + } else { + $Name = $JsonParameters.PSobject.Properties["name"].value + } + + $PSO = [PSCustomObject]@{ + "name" = ${Name} + } + + return $PSO + } + +} + diff --git a/samples/client/petstore/powershell/src/PSPetstore/Model/ObjectWithDeprecatedFields.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Model/ObjectWithDeprecatedFields.ps1 index 22d222edeb0..0651b4e55da 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Model/ObjectWithDeprecatedFields.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Model/ObjectWithDeprecatedFields.ps1 @@ -22,6 +22,8 @@ No description available. No description available. .PARAMETER Bars No description available. +.PARAMETER SomethingElse +No description available. .OUTPUTS ObjectWithDeprecatedFields @@ -41,7 +43,10 @@ function Initialize-PSObjectWithDeprecatedFields { ${DeprecatedRef}, [Parameter(Position = 3, ValueFromPipelineByPropertyName = $true)] [String[]] - ${Bars} + ${Bars}, + [Parameter(Position = 4, ValueFromPipelineByPropertyName = $true)] + [String] + ${SomethingElse} ) Process { @@ -54,6 +59,7 @@ function Initialize-PSObjectWithDeprecatedFields { "id" = ${Id} "deprecatedRef" = ${DeprecatedRef} "bars" = ${Bars} + "name_mapping" = ${SomethingElse} } @@ -91,7 +97,7 @@ function ConvertFrom-PSJsonToObjectWithDeprecatedFields { $JsonParameters = ConvertFrom-Json -InputObject $Json # check if Json contains properties not defined in PSObjectWithDeprecatedFields - $AllProperties = ("uuid", "id", "deprecatedRef", "bars") + $AllProperties = ("uuid", "id", "deprecatedRef", "bars", "name_mapping") foreach ($name in $JsonParameters.PsObject.Properties.Name) { if (!($AllProperties.Contains($name))) { throw "Error! JSON key '$name' not found in the properties: $($AllProperties)" @@ -122,11 +128,18 @@ function ConvertFrom-PSJsonToObjectWithDeprecatedFields { $Bars = $JsonParameters.PSobject.Properties["bars"].value } + if (!([bool]($JsonParameters.PSobject.Properties.name -match "name_mapping"))) { #optional property not found + $SomethingElse = $null + } else { + $SomethingElse = $JsonParameters.PSobject.Properties["name_mapping"].value + } + $PSO = [PSCustomObject]@{ "uuid" = ${Uuid} "id" = ${Id} "deprecatedRef" = ${DeprecatedRef} "bars" = ${Bars} + "name_mapping" = ${SomethingElse} } return $PSO diff --git a/samples/client/petstore/powershell/tests/Model/NewModel.Tests.ps1 b/samples/client/petstore/powershell/tests/Model/NewModel.Tests.ps1 new file mode 100644 index 00000000000..7151ceba71d --- /dev/null +++ b/samples/client/petstore/powershell/tests/Model/NewModel.Tests.ps1 @@ -0,0 +1,17 @@ +# +# OpenAPI Petstore +# This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: "" \ +# Version: 1.0.0 +# Generated by OpenAPI Generator: https://openapi-generator.tech +# + +Describe -tag 'PSPetstore' -name 'PSNewModel' { + Context 'PSNewModel' { + It 'Initialize-PSNewModel' { + # a simple test to create an object + #$NewObject = Initialize-PSNewModel -Name "TEST_VALUE" + #$NewObject | Should -BeOfType NewModel + #$NewObject.property | Should -Be 0 + } + } +}