Skip to content

Commit

Permalink
[PS] automatically derive discriminator mapping for oneOf/anyOf (#6542)
Browse files Browse the repository at this point in the history
* infer mapping based on oneOf/anyOf schemas

* update files
  • Loading branch information
wing328 authored Jun 4, 2020
1 parent 0f627e7 commit 176c439
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,27 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions samples/client/petstore/powershell/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 176c439

Please sign in to comment.