Skip to content

Commit 5fe34fb

Browse files
authored
Add oneof/anyof support to PowerShell client generator (#6361)
* add oneof/anyof support to powershell client gen * fix tests
1 parent 096b8f8 commit 5fe34fb

File tree

18 files changed

+873
-129
lines changed

18 files changed

+873
-129
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public void processOpts() {
575575
}
576576

577577
if (additionalProperties.containsKey("commonVerbs")) {
578-
String[] entries = ((String)additionalProperties.get("commonVerbs")).split(":");
578+
String[] entries = ((String) additionalProperties.get("commonVerbs")).split(":");
579579
for (String entry : entries) {
580580
String[] pair = entry.split("=");
581581
if (pair.length == 2) {
@@ -617,7 +617,7 @@ public void processOpts() {
617617
supportingFiles.add(new SupportingFile("Out-DebugParameter.mustache", infrastructureFolder + File.separator + "Private" + File.separator, "Out-DebugParameter.ps1"));
618618
supportingFiles.add(new SupportingFile("http_signature_auth.mustache", infrastructureFolder + "Private", apiNamePrefix + "HttpSignatureAuth.ps1"));
619619
supportingFiles.add(new SupportingFile("rsa_provider.mustache", infrastructureFolder + "Private", apiNamePrefix + "RSAEncryptionProvider.cs"));
620-
620+
621621

622622
// en-US
623623
supportingFiles.add(new SupportingFile("about_Org.OpenAPITools.help.txt.mustache", infrastructureFolder + File.separator + "en-US" + File.separator + "about_" + packageName + ".help.txt"));
@@ -629,7 +629,7 @@ public void processOpts() {
629629
@SuppressWarnings("static-method")
630630
@Override
631631
public String escapeText(String input) {
632-
632+
633633
if (input == null) {
634634
return input;
635635
}
@@ -646,9 +646,9 @@ public String escapeText(String input) {
646646
.replaceAll("[\\t\\n\\r]", " ")
647647
.replace("\\", "\\\\")
648648
.replace("\"", "\"\""));
649-
649+
650650
}
651-
651+
652652
@Override
653653
public String escapeUnsafeCharacters(String input) {
654654
return input.replace("#>", "#_>").replace("<#", "<_#");
@@ -857,6 +857,26 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
857857
}
858858
}
859859

860+
// check if return type is oneOf/anyeOf model
861+
for (CodegenOperation op : operationList) {
862+
if (op.returnType != null) {
863+
// look up the model to see if it's anyOf/oneOf
864+
if (modelMaps.containsKey(op.returnType) && modelMaps.get(op.returnType) != null) {
865+
CodegenModel cm = modelMaps.get(op.returnType);
866+
867+
if (cm.oneOf != null && !cm.oneOf.isEmpty()) {
868+
op.vendorExtensions.put("x-ps-return-type-one-of", true);
869+
}
870+
871+
if (cm.anyOf != null && !cm.anyOf.isEmpty()) {
872+
op.vendorExtensions.put("x-ps-return-type-any-of", true);
873+
}
874+
} else {
875+
//LOGGER.error("cannot lookup model " + op.returnType);
876+
}
877+
}
878+
}
879+
860880
return objs;
861881
}
862882

modules/openapi-generator/src/main/resources/powershell/api.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ function {{{vendorExtensions.x-powershell-method-name}}} {
223223
-CookieParameters $LocalVarCookieParameters `
224224
-ReturnType "{{#returnType}}{{{.}}}{{/returnType}}"
225225

226+
{{#vendorExtensions.x-ps-return-type-one-of}}
227+
# process oneOf response
228+
$LocalVarResult["Response"] = ConvertFrom-{{apiNamePrefix}}JsonTo{{returnType}} (ConvertTo-Json $LocalVarResult["Response"])
229+
230+
{{/vendorExtensions.x-ps-return-type-one-of}}
231+
{{#vendorExtensions.x-ps-return-type-any-of}}
232+
# process anyOf response
233+
$LocalVarResult["Response"] = ConvertFrom-{{apiNamePrefix}}JsonTo{{returnType}} (ConvertTo-Json $LocalVarResult["Response"])
234+
235+
{{/vendorExtensions.x-ps-return-type-any-of}}
226236
if ($WithHttpInfo.IsPresent) {
227237
return $LocalVarResult
228238
} else {

modules/openapi-generator/src/main/resources/powershell/api_client.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
8989
$RequestBody = $Body
9090
}
9191

92-
# http signature authentication
92+
{{#hasHttpSignatureMethods}}
93+
# http signature authentication
9394
if ($null -ne $Configuration['ApiKey'] -and $Configuration['ApiKey'].Count -gt 0) {
9495
$httpSignHeaderArgument = @{
9596
Method = $Method
@@ -104,6 +105,7 @@ function Invoke-{{{apiNamePrefix}}}ApiClient {
104105
}
105106
}
106107

108+
{{/hasHttpSignatureMethods}}
107109
if ($SkipCertificateCheck -eq $true) {
108110
$Response = Invoke-WebRequest -Uri $UriBuilder.Uri `
109111
-Method $Method `
Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,6 @@
11
{{> partial_header}}
22
{{#models}}
33
{{#model}}
4-
<#
5-
.SYNOPSIS
6-
7-
{{#summary}}{{{.}}}{{/summary}}{{^summary}}No summary available.{{/summary}}
8-
9-
.DESCRIPTION
10-
11-
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
12-
13-
{{#allVars}}
14-
.PARAMETER {{{name}}}
15-
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
16-
17-
{{/allVars}}
18-
.OUTPUTS
19-
20-
{{{classname}}}<PSCustomObject>
21-
#>
22-
23-
function Initialize-{{{apiNamePrefix}}}{{{classname}}} {
24-
[CmdletBinding()]
25-
Param (
26-
{{#allVars}}
27-
[Parameter(Position = {{vendorExtensions.x-index}}, ValueFromPipelineByPropertyName = $true)]
28-
{{#pattern}}
29-
[ValidatePattern("{{{.}}}")]
30-
{{/pattern}}
31-
{{#isEnum}}
32-
{{#allowableValues}}
33-
[ValidateSet({{#values}}"{{{.}}}"{{^-last}}, {{/-last}}{{/values}})]
34-
{{/allowableValues}}
35-
{{/isEnum}}
36-
[{{vendorExtensions.x-powershell-data-type}}]
37-
{{=<% %>=}}
38-
${<%name%>}<%^-last%>,<%/-last%>
39-
<%={{ }}=%>
40-
{{/allVars}}
41-
)
42-
43-
Process {
44-
'Creating PSCustomObject: {{{packageName}}} => {{{apiNamePrefix}}}{{{classname}}}' | Write-Debug
45-
$PSBoundParameters | Out-DebugParameter | Write-Debug
46-
47-
{{#vars}}
48-
{{^isNullable}}
49-
{{#required}}
50-
if (!${{{name}}}) {
51-
throw "invalid value for '{{{name}}}', '{{{name}}}' cannot be null."
52-
}
53-
54-
{{/required}}
55-
{{/isNullable}}
56-
{{#hasValidation}}
57-
{{#maxLength}}
58-
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxLength}}}) {
59-
throw "invalid value for '{{{name}}}', the character length must be smaller than or equal to {{{maxLength}}}."
60-
}
61-
62-
{{/maxLength}}
63-
{{#minLength}}
64-
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minLength}}}) {
65-
throw "invalid value for '{{{name}}}', the character length must be great than or equal to {{{minLength}}}."
66-
}
67-
68-
{{/minLength}}
69-
{{#maximum}}
70-
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMaximum}}-ge{{/exclusiveMaximum}}{{^exclusiveMaximum}}-gt{{/exclusiveMaximum}} {{{maximum}}}) {
71-
throw "invalid value for '{{{name}}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{{maximum}}}."
72-
}
73-
74-
{{/maximum}}
75-
{{#minimum}}
76-
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}} {{#exclusiveMinimum}}-le{{/exclusiveMinimum}}{{^exclusiveMinimum}}-lt{{/exclusiveMinimum}} {{{minimum}}}) {
77-
throw "invalid value for '{{{name}}}', must be greater than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{{minimum}}}."
78-
}
79-
80-
{{/minimum}}
81-
{{#maxItems}}
82-
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -gt {{{maxItems}}}) {
83-
throw "invalid value for '{{{name}}}', number of items must be less than or equal to {{{maxItems}}}."
84-
}
85-
86-
{{/maxItems}}
87-
{{#minItems}}
88-
if ({{^required}}!${{{name}}} -and {{/required}}${{{name}}}.length -lt {{{minItems}}}) {
89-
throw "invalid value for '{{{name}}}', number of items must be greater than or equal to {{{minItems}}}."
90-
}
91-
92-
{{/minItems}}
93-
{{/hasValidation}}
94-
{{/vars}}
95-
$PSO = [PSCustomObject]@{
96-
{{=<< >>=}}
97-
<<#allVars>>
98-
"<<baseName>>" = ${<<name>>}
99-
<</allVars>>
100-
<<={{ }}=>>
101-
}
102-
103-
return $PSO
104-
}
105-
}
4+
{{#oneOf}}{{#-first}}{{>model_oneof}}{{/-first}}{{/oneOf}}{{^oneOf}}{{#anyOf}}{{#-first}}{{>model_anyof}}{{/-first}}{{/anyOf}}{{^anyOf}}{{>model_simple}}{{/anyOf}}{{/oneOf}}
1065
{{/model}}
1076
{{/models}}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<#
2+
.SYNOPSIS
3+
4+
{{#summary}}{{{.}}}{{/summary}}{{^summary}}No summary available.{{/summary}}
5+
6+
.DESCRIPTION
7+
8+
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
9+
10+
.PARAMETER Json
11+
12+
JSON object
13+
14+
.OUTPUTS
15+
16+
{{{classname}}}<PSCustomObject>
17+
#>
18+
function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
19+
[CmdletBinding()]
20+
Param (
21+
[AllowEmptyString()]
22+
[string]$Json
23+
)
24+
25+
Process {
26+
$match = 0
27+
$matchType = $null
28+
$matchInstance = $null
29+
30+
{{#anyOf}}
31+
if ($match -ne 0) { # no match yet
32+
# try to match {{{.}}} defined in the anyOf schemas
33+
try {
34+
$matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{.}}} $Json
35+
36+
foreach($property in $matchInstance.PsObject.Properties) {
37+
if ($null -ne $property.Value) {
38+
$matchType = "Dog"
39+
$match++
40+
break
41+
}
42+
}
43+
} catch {
44+
# fail to match the schema defined in anyOf, proceed to the next one
45+
}
46+
}
47+
48+
{{/anyOf}}
49+
if ($match -eq 1) {
50+
return [PSCustomObject]@{
51+
"ActualType" = ${matchType}
52+
"ActualInstance" = ${matchInstance}
53+
"anyOfSchemas" = @({{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}})
54+
}
55+
} else {
56+
throw "Error! The JSON payload doesn't matches any type defined in anyOf schemas ({{{anyOf}}}). JSON Payload: $($Json)"
57+
}
58+
}
59+
}
60+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<#
2+
.SYNOPSIS
3+
4+
{{#summary}}{{{.}}}{{/summary}}{{^summary}}No summary available.{{/summary}}
5+
6+
.DESCRIPTION
7+
8+
{{#description}}{{{description}}}{{/description}}{{^description}}No description available.{{/description}}
9+
10+
.PARAMETER Json
11+
12+
JSON object
13+
14+
.OUTPUTS
15+
16+
{{{classname}}}<PSCustomObject>
17+
#>
18+
function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
19+
[CmdletBinding()]
20+
Param (
21+
[AllowEmptyString()]
22+
[string]$Json
23+
)
24+
25+
Process {
26+
$match = 0
27+
$matchType = $null
28+
$matchInstance = $null
29+
30+
{{#oneOf}}
31+
# try to match {{{.}}} defined in the oneOf schemas
32+
try {
33+
$matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{.}}} $Json
34+
35+
foreach($property in $matchInstance.PsObject.Properties) {
36+
if ($null -ne $property.Value) {
37+
$matchType = "Dog"
38+
$match++
39+
break
40+
}
41+
}
42+
} catch {
43+
# fail to match the schema defined in oneOf, proceed to the next one
44+
}
45+
46+
{{/oneOf}}
47+
if ($match -gt 1) {
48+
throw "Error! The JSON payload matches more than one type defined in oneOf schemas ({{{oneOf}}}). JSON Payload: $($Json)"
49+
} elseif ($match -eq 1) {
50+
return [PSCustomObject]@{
51+
"ActualType" = ${matchType}
52+
"ActualInstance" = ${matchInstance}
53+
"OneOfSchemas" = @({{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}})
54+
}
55+
} else {
56+
throw "Error! The JSON payload doesn't matches any type defined in oneOf schemas ({{{oneOf}}}). JSON Payload: $($Json)"
57+
}
58+
}
59+
}
60+

0 commit comments

Comments
 (0)