forked from loafle/openapi-generator-original
[Go][Experimental] Fix discriminator lookup (#6521)
* bug fix, better code format * update oas3 sample
This commit is contained in:
parent
8fc7ec8458
commit
4d68953ded
@ -24,40 +24,40 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
{{/isNullable}}
|
||||
{{#useOneOfDiscriminatorLookup}}
|
||||
{{#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.")
|
||||
}
|
||||
{{#useOneOfDiscriminatorLookup}}
|
||||
{{#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
|
||||
}
|
||||
}
|
||||
{{/-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}}
|
||||
{{/useOneOfDiscriminatorLookup}}
|
||||
{{/mappedModels}}
|
||||
{{/discriminator}}
|
||||
{{/useOneOfDiscriminatorLookup}}
|
||||
{{#oneOf}}
|
||||
// try to unmarshal data into {{{.}}}
|
||||
err = json.Unmarshal(data, &dst.{{{.}}});
|
||||
err = json.Unmarshal(data, &dst.{{{.}}})
|
||||
if err == nil {
|
||||
json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
|
||||
if string(json{{{.}}}) == "{}" { // empty struct
|
||||
|
@ -36,7 +36,7 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
match := 0
|
||||
// try to unmarshal data into Apple
|
||||
err = json.Unmarshal(data, &dst.Apple);
|
||||
err = json.Unmarshal(data, &dst.Apple)
|
||||
if err == nil {
|
||||
jsonApple, _ := json.Marshal(dst.Apple)
|
||||
if string(jsonApple) == "{}" { // empty struct
|
||||
@ -49,7 +49,7 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
// try to unmarshal data into Banana
|
||||
err = json.Unmarshal(data, &dst.Banana);
|
||||
err = json.Unmarshal(data, &dst.Banana)
|
||||
if err == nil {
|
||||
jsonBanana, _ := json.Marshal(dst.Banana)
|
||||
if string(jsonBanana) == "{}" { // empty struct
|
||||
|
@ -36,7 +36,7 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
match := 0
|
||||
// try to unmarshal data into AppleReq
|
||||
err = json.Unmarshal(data, &dst.AppleReq);
|
||||
err = json.Unmarshal(data, &dst.AppleReq)
|
||||
if err == nil {
|
||||
jsonAppleReq, _ := json.Marshal(dst.AppleReq)
|
||||
if string(jsonAppleReq) == "{}" { // empty struct
|
||||
@ -49,7 +49,7 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
// try to unmarshal data into BananaReq
|
||||
err = json.Unmarshal(data, &dst.BananaReq);
|
||||
err = json.Unmarshal(data, &dst.BananaReq)
|
||||
if err == nil {
|
||||
jsonBananaReq, _ := json.Marshal(dst.BananaReq)
|
||||
if string(jsonBananaReq) == "{}" { // empty struct
|
||||
|
@ -36,7 +36,7 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
match := 0
|
||||
// try to unmarshal data into Whale
|
||||
err = json.Unmarshal(data, &dst.Whale);
|
||||
err = json.Unmarshal(data, &dst.Whale)
|
||||
if err == nil {
|
||||
jsonWhale, _ := json.Marshal(dst.Whale)
|
||||
if string(jsonWhale) == "{}" { // empty struct
|
||||
@ -49,7 +49,7 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
|
||||
// try to unmarshal data into Zebra
|
||||
err = json.Unmarshal(data, &dst.Zebra);
|
||||
err = json.Unmarshal(data, &dst.Zebra)
|
||||
if err == nil {
|
||||
jsonZebra, _ := json.Marshal(dst.Zebra)
|
||||
if string(jsonZebra) == "{}" { // empty struct
|
||||
|
Loading…
x
Reference in New Issue
Block a user