[Powershell] add name mapping features (#16509)

* add name mapping features to the powershell generator

* update samples
This commit is contained in:
William Cheng 2023-09-05 15:45:03 +08:00 committed by GitHub
parent f3eb07408d
commit 29f0d22713
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 182 additions and 3 deletions

View File

@ -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

View File

@ -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);

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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<PSCustomObject>
#>
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<PSCustomObject>
.DESCRIPTION
Convert from JSON to NewModel<PSCustomObject>
.PARAMETER Json
Json object
.OUTPUTS
NewModel<PSCustomObject>
#>
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
}
}

View File

@ -22,6 +22,8 @@ No description available.
No description available.
.PARAMETER Bars
No description available.
.PARAMETER SomethingElse
No description available.
.OUTPUTS
ObjectWithDeprecatedFields<PSCustomObject>
@ -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

View File

@ -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
}
}
}