-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add oneof/anyof support to PowerShell client generator (#6361)
* add oneof/anyof support to powershell client gen * fix tests
- Loading branch information
Showing
18 changed files
with
873 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 1 addition & 102 deletions
103
modules/openapi-generator/src/main/resources/powershell/model.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,6 @@ | ||
{{> partial_header}} | ||
{{#models}} | ||
{{#model}} | ||
<# | ||
.SYNOPSIS | ||
|
||
{{#summary}}{{{.}}}{{/summary}}{{^summary}}No summary available.{{/summary}} | ||
|
||
.DESCRIPTION | ||
|
||
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}} | ||
|
||
{{#allVars}} | ||
.PARAMETER {{{name}}} | ||
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}} | ||
|
||
{{/allVars}} | ||
.OUTPUTS | ||
|
||
{{{classname}}}<PSCustomObject> | ||
#> | ||
|
||
function Initialize-{{{apiNamePrefix}}}{{{classname}}} { | ||
[CmdletBinding()] | ||
Param ( | ||
{{#allVars}} | ||
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)] | ||
{{#pattern}} | ||
[ValidatePattern("{{{.}}}")] | ||
{{/pattern}} | ||
{{#isEnum}} | ||
{{#allowableValues}} | ||
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})] | ||
{{/allowableValues}} | ||
{{/isEnum}} | ||
[{{vendorExtensions.x-powershell-data-type}}] | ||
{{=<% %>=}} | ||
${<%name%>}<%^-last%>,<%/-last%> | ||
<%={{ }}=%> | ||
{{/allVars}} | ||
) | ||
|
||
Process { | ||
'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug | ||
$PSBoundParameters | Out-DebugParameter | Write-Debug | ||
{{#vars}} | ||
{{^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}} | ||
{{/vars}} | ||
$PSO = [PSCustomObject]@{ | ||
{{=<< >>=}} | ||
<<#allVars>> | ||
"<<baseName>>" = ${<<name>>} | ||
<</allVars>> | ||
<<={{ }}=>> | ||
} | ||
|
||
return $PSO | ||
} | ||
} | ||
{{#oneOf}}{{#-first}}{{>model_oneof}}{{/-first}}{{/oneOf}}{{^oneOf}}{{#anyOf}}{{#-first}}{{>model_anyof}}{{/-first}}{{/anyOf}}{{^anyOf}}{{>model_simple}}{{/anyOf}}{{/oneOf}} | ||
{{/model}} | ||
{{/models}} |
60 changes: 60 additions & 0 deletions
60
modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<# | ||
.SYNOPSIS | ||
|
||
{{#summary}}{{{.}}}{{/summary}}{{^summary}}No summary available.{{/summary}} | ||
|
||
.DESCRIPTION | ||
|
||
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}} | ||
|
||
.PARAMETER Json | ||
|
||
JSON object | ||
|
||
.OUTPUTS | ||
|
||
{{{classname}}}<PSCustomObject> | ||
#> | ||
function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} { | ||
[CmdletBinding()] | ||
Param ( | ||
[AllowEmptyString()] | ||
[string]$Json | ||
) | ||
Process { | ||
$match = 0 | ||
$matchType = $null | ||
$matchInstance = $null | ||
{{#anyOf}} | ||
if ($match -ne 0) { # no match yet | ||
# try to match {{{.}}} defined in the anyOf schemas | ||
try { | ||
$matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{.}}} $Json | ||
|
||
foreach($property in $matchInstance.PsObject.Properties) { | ||
if ($null -ne $property.Value) { | ||
$matchType = "Dog" | ||
$match++ | ||
break | ||
} | ||
} | ||
} catch { | ||
# fail to match the schema defined in anyOf, proceed to the next one | ||
} | ||
} | ||
|
||
{{/anyOf}} | ||
if ($match -eq 1) { | ||
return [PSCustomObject]@{ | ||
"ActualType" = ${matchType} | ||
"ActualInstance" = ${matchInstance} | ||
"anyOfSchemas" = @({{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}}) | ||
} | ||
} else { | ||
throw "Error! The JSON payload doesn't matches any type defined in anyOf schemas ({{{anyOf}}}). JSON Payload: $($Json)" | ||
} | ||
} | ||
} | ||
|
60 changes: 60 additions & 0 deletions
60
modules/openapi-generator/src/main/resources/powershell/model_oneof.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<# | ||
.SYNOPSIS | ||
|
||
{{#summary}}{{{.}}}{{/summary}}{{^summary}}No summary available.{{/summary}} | ||
|
||
.DESCRIPTION | ||
|
||
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}} | ||
|
||
.PARAMETER Json | ||
|
||
JSON object | ||
|
||
.OUTPUTS | ||
|
||
{{{classname}}}<PSCustomObject> | ||
#> | ||
function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} { | ||
[CmdletBinding()] | ||
Param ( | ||
[AllowEmptyString()] | ||
[string]$Json | ||
) | ||
Process { | ||
$match = 0 | ||
$matchType = $null | ||
$matchInstance = $null | ||
{{#oneOf}} | ||
# try to match {{{.}}} defined in the oneOf schemas | ||
try { | ||
$matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{.}}} $Json | ||
|
||
foreach($property in $matchInstance.PsObject.Properties) { | ||
if ($null -ne $property.Value) { | ||
$matchType = "Dog" | ||
$match++ | ||
break | ||
} | ||
} | ||
} catch { | ||
# fail to match the schema defined in oneOf, proceed to the next one | ||
} | ||
|
||
{{/oneOf}} | ||
if ($match -gt 1) { | ||
throw "Error! The JSON payload matches more than one type defined in oneOf schemas ({{{oneOf}}}). JSON Payload: $($Json)" | ||
} elseif ($match -eq 1) { | ||
return [PSCustomObject]@{ | ||
"ActualType" = ${matchType} | ||
"ActualInstance" = ${matchInstance} | ||
"OneOfSchemas" = @({{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}}) | ||
} | ||
} else { | ||
throw "Error! The JSON payload doesn't matches any type defined in oneOf schemas ({{{oneOf}}}). JSON Payload: $($Json)" | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.