add nullable support to oneof, anyof (#6392)

This commit is contained in:
William Cheng
2020-05-22 15:03:51 +08:00
committed by GitHub
parent f10de73ed5
commit a8f9ea4873
4 changed files with 38 additions and 1108 deletions

View File

@@ -888,12 +888,25 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
// add x-data-type to store powershell type
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
Map<String, Object> _model = (Map<String, Object>) _mo;
CodegenModel model = (CodegenModel) _model.get("model");
for (CodegenProperty cp : cm.allVars) {
for (CodegenProperty cp : model.allVars) {
cp.vendorExtensions.put("x-powershell-data-type", getPSDataType(cp));
}
// if oneOf contains "null" type
if (model.oneOf != null && !model.oneOf.isEmpty() && model.oneOf.contains("ModelNull")) {
model.isNullable = true;
model.oneOf.remove("ModelNull");
}
// if anyOf contains "null" type
if (model.anyOf != null && !model.anyOf.isEmpty() && model.anyOf.contains("ModelNull")) {
model.isNullable = true;
model.anyOf.remove("ModelNull");
}
}
return objs;

View File

@@ -27,6 +27,17 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
$matchType = $null
$matchInstance = $null
{{#isNullable}}
# nullalble check
if ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") {
return [PSCustomObject]@{
"ActualType" = $null
"ActualInstance" = $null
"AnyOfSchemas" = @({{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}})
}
}
{{/isNullable}}
{{#anyOf}}
if ($match -ne 0) { # no match yet
# try to match {{{.}}} defined in the anyOf schemas

View File

@@ -27,6 +27,17 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} {
$matchType = $null
$matchInstance = $null
{{#isNullable}}
# nullalble check
if ([string]::IsNullOrEmpty($Json) -or $Json -eq "{}") {
return [PSCustomObject]@{
"ActualType" = $null
"ActualInstance" = $null
"OneOfSchemas" = @({{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}})
}
}
{{/isNullable}}
{{#oneOf}}
# try to match {{{.}}} defined in the oneOf schemas
try {