Skip to content

Commit

Permalink
if discriminator is not defined with useOneOfDiscriminatorLookup set …
Browse files Browse the repository at this point in the history
…to true, we generated as useOneOfDiscriminatorLookup false (OpenAPITools#11178)
  • Loading branch information
kandaaaaa authored and cachescrubber committed Jan 20, 2022
1 parent 4076b1d commit d4912b4
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,38 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
}

{{/mappedModels}}
{{/discriminator}}
return nil
{{/discriminator}}
{{^discriminator}}
match := 0
{{#oneOf}}
// try to unmarshal data into {{{.}}}
err = json.Unmarshal(data, &dst.{{{.}}})
if err == nil {
json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
if string(json{{{.}}}) == "{}" { // empty struct
dst.{{{.}}} = nil
} else {
match++
}
} else {
dst.{{{.}}} = nil
}

{{/oneOf}}
if match > 1 { // more than 1 match
// reset to nil
{{#oneOf}}
dst.{{{.}}} = nil
{{/oneOf}}

return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})")
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf({{classname}})")
}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
{{^useOneOfDiscriminatorLookup}}
match := 0
Expand Down

0 comments on commit d4912b4

Please sign in to comment.