forked from loafle/openapi-generator-original
[Go][Experimental] Add discriminator support to anyOf (#6511)
* add discriminator support in anyof * update samples
This commit is contained in:
@@ -15,6 +15,35 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
{{/isNullable}}
|
||||
{{#discriminator}}
|
||||
{{#mappedModels}}
|
||||
{{#-first}}
|
||||
// use discriminator value to speed up the lookup
|
||||
var jsonDict map[string]interface{}
|
||||
err := json.Unmarshal(data, &jsonDict)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to unmarshal JSON into map for the discrimintor lookup.")
|
||||
}
|
||||
|
||||
{{/-first}}
|
||||
// check if the discriminator value is '{{{mappingName}}}'
|
||||
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
|
||||
// try to unmarshal JSON data into {{{modelName}}}
|
||||
err = json.Unmarshal(data, &dst.{{{modelName}}});
|
||||
if err == nil {
|
||||
json{{{modelName}}}, _ := json.Marshal(dst.{{{modelName}}})
|
||||
if string(json{{{modelName}}}) == "{}" { // empty struct
|
||||
dst.{{{modelName}}} = nil
|
||||
} else {
|
||||
return nil // data stored in dst.{{{modelName}}}, return on the first match
|
||||
}
|
||||
} else {
|
||||
dst.{{{modelName}}} = nil
|
||||
}
|
||||
}
|
||||
|
||||
{{/mappedModels}}
|
||||
{{/discriminator}}
|
||||
{{#anyOf}}
|
||||
// try to unmarshal JSON data into {{{.}}}
|
||||
err = json.Unmarshal(data, &dst.{{{.}}});
|
||||
@@ -44,4 +73,4 @@ func (src *{{classname}}) MarshalJSON() ([]byte, error) {
|
||||
return nil, nil // no data in anyOf schemas
|
||||
}
|
||||
|
||||
{{>nullable_model}}
|
||||
{{>nullable_model}}
|
||||
|
||||
@@ -101,3 +101,4 @@ func (v *NullableGmFruit) UnmarshalJSON(src []byte) error {
|
||||
return json.Unmarshal(src, &v.value)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user