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 f58773df049..a3310c354b3 100644 --- a/modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/model_anyof.mustache @@ -38,6 +38,35 @@ function ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{classname}}} { } {{/isNullable}} + {{#discriminator}} + {{#mappedModels}} + {{#-first}} + $JsonData = ConvertFrom-Json -InputObject $Json + {{/-first}} + # check if the discriminator value is '{{{mappingName}}}' + if ($JsonData.PSobject.Properties["{{{propertyBaseName}}}"].value == "{{{mappingName}}}") { + # try to match {{{modelName}}} defined in the anyOf schemas + try { + $matchInstance = ConvertFrom-{{{apiNamePrefix}}}JsonTo{{{modelName}}} $Json + + foreach($property in $matchInstance.PsObject.Properties) { + if ($null -ne $property.Value) { + $matchType = "{{{modelName}}}" + return [PSCustomObject]@{ + "ActualType" = ${matchType} + "ActualInstance" = ${matchInstance} + "anyOfSchemas" = @({{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}}) + } + } + } + } catch { + # fail to match the schema defined in anyOf with the discriminator lookup, proceed to the next one + Write-Debug "Failed to match '{{{modelName}}}' defined in anyOf ({{{apiNamePrefix}}}{{{classname}}}) using the discriminator lookup ({{{mappingName}}}). Proceeding with the typical anyOf type matching." + } + } + + {{/mappedModels}} + {{/discriminator}} {{#anyOf}} if ($match -ne 0) { # no match yet # try to match {{{.}}} defined in the anyOf schemas