diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java index 9c46377c097..d3e519a9a7f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java @@ -926,6 +926,27 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo if ("null".equals(model.parent)) { model.vendorExtensions.put("x-additional-properties", true); } + + // automatically create discriminator mapping for oneOf/anyOf if not present + if (((model.oneOf != null && !model.oneOf.isEmpty()) || (model.anyOf != null && !model.anyOf.isEmpty())) && + model.discriminator != null && model.discriminator.getMapping() == null) { + // create mappedModels + Set schemas = new HashSet<>(); + if (model.oneOf != null && !model.oneOf.isEmpty()) { + schemas = model.oneOf; + } else if (model.anyOf != null && !model.anyOf.isEmpty()) { + schemas = model.anyOf; + } + + HashSet mappedModels = new HashSet<>(); + + for (String s: schemas) { + mappedModels.add(new CodegenDiscriminator.MappedModel(s, s)); + } + + model.discriminator.setMappedModels(mappedModels); + + } } return objs; diff --git a/modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache b/modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache index a3310c354b3..62991addbe2 100644 --- a/modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache @@ -44,7 +44,7 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} { $JsonData = ConvertFrom-Json -InputObject $Json {{/-first}} # check if the discriminator value is '{{{mappingName}}}' - if ($JsonData.PSobject.Properties["{{{propertyBaseName}}}"].value == "{{{mappingName}}}") { + if ($JsonData.PSobject.Properties["{{{propertyBaseName}}}"].value -eq "{{{mappingName}}}") { # try to match {{{modelName}}} defined in the anyOf schemas try { $matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{modelName}}} $Json diff --git a/modules/openapi-generator/src/main/resources/powershell/model_oneof.mustache b/modules/openapi-generator/src/main/resources/powershell/model_oneof.mustache index 4bda9700eab..e71d77814d2 100644 --- a/modules/openapi-generator/src/main/resources/powershell/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/model_oneof.mustache @@ -45,7 +45,7 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} { $JsonData = ConvertFrom-Json -InputObject $Json {{/-first}} # check if the discriminator value is '{{{mappingName}}}' - if ($JsonData.PSobject.Properties["{{{propertyBaseName}}}"].value == "{{{mappingName}}}") { + if ($JsonData.PSobject.Properties["{{{propertyBaseName}}}"].value -eq "{{{mappingName}}}") { # try to match {{{modelName}}} defined in the oneOf schemas try { $matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{modelName}}} $Json diff --git a/samples/client/petstore/powershell/.openapi-generator/FILES b/samples/client/petstore/powershell/.openapi-generator/FILES index 2849a3267b9..4708e0914a5 100644 --- a/samples/client/petstore/powershell/.openapi-generator/FILES +++ b/samples/client/petstore/powershell/.openapi-generator/FILES @@ -31,14 +31,3 @@ src/PSPetstore/Private/PSApiClient.ps1 src/PSPetstore/Private/PSHttpSignatureAuth.ps1 src/PSPetstore/Private/PSRSAEncryptionProvider.cs src/PSPetstore/en-US/about_PSPetstore.help.txt -tests/Api/PSPetApi.Tests.ps1 -tests/Api/PSStoreApi.Tests.ps1 -tests/Api/PSUserApi.Tests.ps1 -tests/Model/ApiResponse.Tests.ps1 -tests/Model/Category.Tests.ps1 -tests/Model/InlineObject.Tests.ps1 -tests/Model/InlineObject1.Tests.ps1 -tests/Model/Order.Tests.ps1 -tests/Model/Pet.Tests.ps1 -tests/Model/Tag.Tests.ps1 -tests/Model/User.Tests.ps1