forked from loafle/openapi-generator-original
[Powershell] add name mapping features (#16509)
* add name mapping features to the powershell generator * update samples
This commit is contained in:
parent
f3eb07408d
commit
29f0d22713
@ -14,3 +14,7 @@ additionalProperties:
|
|||||||
projectUri: https://github.com/OpenAPITools/openapi-generator
|
projectUri: https://github.com/OpenAPITools/openapi-generator
|
||||||
releaseNotes: 'This is a sample project'
|
releaseNotes: 'This is a sample project'
|
||||||
tags: 'PetStore,powershell,sdk'
|
tags: 'PetStore,powershell,sdk'
|
||||||
|
nameMappings:
|
||||||
|
name_mapping: SomethingElse
|
||||||
|
modelNameMappings:
|
||||||
|
model-mapping: NewModel
|
||||||
|
@ -910,6 +910,11 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toModelName(String name) {
|
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
|
// check if schema-mapping has a different model for this class, so we can use it
|
||||||
// instead of the auto-generated one.
|
// instead of the auto-generated one.
|
||||||
if (schemaMapping.containsKey(name)) {
|
if (schemaMapping.containsKey(name)) {
|
||||||
@ -1016,6 +1021,11 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toParamName(String name) {
|
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
|
// sanitize and camelize parameter name
|
||||||
// pet_id => PetId
|
// pet_id => PetId
|
||||||
name = camelize(sanitizeName(name));
|
name = camelize(sanitizeName(name));
|
||||||
@ -1148,6 +1158,11 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toVarName(String name) {
|
public String toVarName(String name) {
|
||||||
|
// obtain the name from nameMapping directly if provided
|
||||||
|
if (nameMapping.containsKey(name)) {
|
||||||
|
return nameMapping.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
// sanitize name
|
// sanitize name
|
||||||
name = sanitizeName(name);
|
name = sanitizeName(name);
|
||||||
|
|
||||||
|
@ -2129,6 +2129,11 @@ components:
|
|||||||
properties:
|
properties:
|
||||||
name:
|
name:
|
||||||
type: string
|
type: string
|
||||||
|
model-mapping:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
ObjectWithDeprecatedFields:
|
ObjectWithDeprecatedFields:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
@ -2144,6 +2149,8 @@ components:
|
|||||||
deprecated: true
|
deprecated: true
|
||||||
items:
|
items:
|
||||||
$ref: '#/components/schemas/Bar'
|
$ref: '#/components/schemas/Bar'
|
||||||
|
name_mapping:
|
||||||
|
type: string
|
||||||
PetWithRequiredTags:
|
PetWithRequiredTags:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
|
@ -42,6 +42,7 @@ docs/List.md
|
|||||||
docs/Mammal.md
|
docs/Mammal.md
|
||||||
docs/MapTest.md
|
docs/MapTest.md
|
||||||
docs/MixedPropertiesAndAdditionalPropertiesClass.md
|
docs/MixedPropertiesAndAdditionalPropertiesClass.md
|
||||||
|
docs/ModelMapping.md
|
||||||
docs/Name.md
|
docs/Name.md
|
||||||
docs/NullableClass.md
|
docs/NullableClass.md
|
||||||
docs/NullableShape.md
|
docs/NullableShape.md
|
||||||
@ -131,6 +132,7 @@ src/PSPetstore/Model/MixedPropertiesAndAdditionalPropertiesClass.ps1
|
|||||||
src/PSPetstore/Model/Model200Response.ps1
|
src/PSPetstore/Model/Model200Response.ps1
|
||||||
src/PSPetstore/Model/ModelReturn.ps1
|
src/PSPetstore/Model/ModelReturn.ps1
|
||||||
src/PSPetstore/Model/Name.ps1
|
src/PSPetstore/Model/Name.ps1
|
||||||
|
src/PSPetstore/Model/NewModel.ps1
|
||||||
src/PSPetstore/Model/NullableClass.ps1
|
src/PSPetstore/Model/NullableClass.ps1
|
||||||
src/PSPetstore/Model/NullableShape.ps1
|
src/PSPetstore/Model/NullableShape.ps1
|
||||||
src/PSPetstore/Model/NumberOnly.ps1
|
src/PSPetstore/Model/NumberOnly.ps1
|
||||||
|
@ -141,6 +141,7 @@ Class | Method | HTTP request | Description
|
|||||||
- [PSPetstore\Model.Model200Response](docs/Model200Response.md)
|
- [PSPetstore\Model.Model200Response](docs/Model200Response.md)
|
||||||
- [PSPetstore\Model.ModelReturn](docs/ModelReturn.md)
|
- [PSPetstore\Model.ModelReturn](docs/ModelReturn.md)
|
||||||
- [PSPetstore\Model.Name](docs/Name.md)
|
- [PSPetstore\Model.Name](docs/Name.md)
|
||||||
|
- [PSPetstore\Model.NewModel](docs/NewModel.md)
|
||||||
- [PSPetstore\Model.NullableClass](docs/NullableClass.md)
|
- [PSPetstore\Model.NullableClass](docs/NullableClass.md)
|
||||||
- [PSPetstore\Model.NullableShape](docs/NullableShape.md)
|
- [PSPetstore\Model.NullableShape](docs/NullableShape.md)
|
||||||
- [PSPetstore\Model.NumberOnly](docs/NumberOnly.md)
|
- [PSPetstore\Model.NumberOnly](docs/NumberOnly.md)
|
||||||
|
21
samples/client/petstore/powershell/docs/ModelMapping.md
Normal file
21
samples/client/petstore/powershell/docs/ModelMapping.md
Normal 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)
|
||||||
|
|
@ -7,6 +7,7 @@ Name | Type | Description | Notes
|
|||||||
**Id** | **Decimal** | | [optional]
|
**Id** | **Decimal** | | [optional]
|
||||||
**DeprecatedRef** | [**DeprecatedObject**](DeprecatedObject.md) | | [optional]
|
**DeprecatedRef** | [**DeprecatedObject**](DeprecatedObject.md) | | [optional]
|
||||||
**Bars** | **String[]** | | [optional]
|
**Bars** | **String[]** | | [optional]
|
||||||
|
**SomethingElse** | **String** | | [optional]
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@ -15,7 +16,8 @@ Name | Type | Description | Notes
|
|||||||
$ObjectWithDeprecatedFields = Initialize-PSPetstoreObjectWithDeprecatedFields -Uuid null `
|
$ObjectWithDeprecatedFields = Initialize-PSPetstoreObjectWithDeprecatedFields -Uuid null `
|
||||||
-Id null `
|
-Id null `
|
||||||
-DeprecatedRef null `
|
-DeprecatedRef null `
|
||||||
-Bars null
|
-Bars null `
|
||||||
|
-SomethingElse null
|
||||||
```
|
```
|
||||||
|
|
||||||
- Convert the resource to JSON
|
- Convert the resource to JSON
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,8 @@ No description available.
|
|||||||
No description available.
|
No description available.
|
||||||
.PARAMETER Bars
|
.PARAMETER Bars
|
||||||
No description available.
|
No description available.
|
||||||
|
.PARAMETER SomethingElse
|
||||||
|
No description available.
|
||||||
.OUTPUTS
|
.OUTPUTS
|
||||||
|
|
||||||
ObjectWithDeprecatedFields<PSCustomObject>
|
ObjectWithDeprecatedFields<PSCustomObject>
|
||||||
@ -41,7 +43,10 @@ function Initialize-PSObjectWithDeprecatedFields {
|
|||||||
${DeprecatedRef},
|
${DeprecatedRef},
|
||||||
[Parameter(Position = 3, ValueFromPipelineByPropertyName = $true)]
|
[Parameter(Position = 3, ValueFromPipelineByPropertyName = $true)]
|
||||||
[String[]]
|
[String[]]
|
||||||
${Bars}
|
${Bars},
|
||||||
|
[Parameter(Position = 4, ValueFromPipelineByPropertyName = $true)]
|
||||||
|
[String]
|
||||||
|
${SomethingElse}
|
||||||
)
|
)
|
||||||
|
|
||||||
Process {
|
Process {
|
||||||
@ -54,6 +59,7 @@ function Initialize-PSObjectWithDeprecatedFields {
|
|||||||
"id" = ${Id}
|
"id" = ${Id}
|
||||||
"deprecatedRef" = ${DeprecatedRef}
|
"deprecatedRef" = ${DeprecatedRef}
|
||||||
"bars" = ${Bars}
|
"bars" = ${Bars}
|
||||||
|
"name_mapping" = ${SomethingElse}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -91,7 +97,7 @@ function ConvertFrom-PSJsonToObjectWithDeprecatedFields {
|
|||||||
$JsonParameters = ConvertFrom-Json -InputObject $Json
|
$JsonParameters = ConvertFrom-Json -InputObject $Json
|
||||||
|
|
||||||
# check if Json contains properties not defined in PSObjectWithDeprecatedFields
|
# 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) {
|
foreach ($name in $JsonParameters.PsObject.Properties.Name) {
|
||||||
if (!($AllProperties.Contains($name))) {
|
if (!($AllProperties.Contains($name))) {
|
||||||
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
|
throw "Error! JSON key '$name' not found in the properties: $($AllProperties)"
|
||||||
@ -122,11 +128,18 @@ function ConvertFrom-PSJsonToObjectWithDeprecatedFields {
|
|||||||
$Bars = $JsonParameters.PSobject.Properties["bars"].value
|
$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]@{
|
$PSO = [PSCustomObject]@{
|
||||||
"uuid" = ${Uuid}
|
"uuid" = ${Uuid}
|
||||||
"id" = ${Id}
|
"id" = ${Id}
|
||||||
"deprecatedRef" = ${DeprecatedRef}
|
"deprecatedRef" = ${DeprecatedRef}
|
||||||
"bars" = ${Bars}
|
"bars" = ${Bars}
|
||||||
|
"name_mapping" = ${SomethingElse}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $PSO
|
return $PSO
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user