diff --git a/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache b/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache index bbd9aeecccee..c86af51efb9c 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache @@ -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 diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go index 416ae81c7188..d9c7a0b3b41f 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go @@ -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 diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go index dce03284f0f0..d20957648c65 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go @@ -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 diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go index e9731f1d550d..49d5dfdfe2a4 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go @@ -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