[PS] automatically derive discriminator mapping for oneOf/anyOf (#6542)

* infer mapping based on oneOf/anyOf schemas

* update files
This commit is contained in:
William Cheng 2020-06-04 21:13:42 +08:00 committed by GitHub
parent 0f627e70fb
commit 176c439a6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 13 deletions

View File

@ -926,6 +926,27 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo
if ("null<String, SystemCollectionsHashtable>".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<String> 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<CodegenDiscriminator.MappedModel> mappedModels = new HashSet<>();
for (String s: schemas) {
mappedModels.add(new CodegenDiscriminator.MappedModel(s, s));
}
model.discriminator.setMappedModels(mappedModels);
}
}
return objs;

View File

@ -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

View File

@ -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

View File

@ -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