[Go][Experimental] Fix discriminator lookup (#6521)

* bug fix, better code format

* update oas3 sample
This commit is contained in:
William Cheng 2020-06-03 01:11:41 +08:00 committed by GitHub
parent 8fc7ec8458
commit 4d68953ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 36 deletions

View File

@ -30,7 +30,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
{{#-first}} {{#-first}}
// use discriminator value to speed up the lookup // use discriminator value to speed up the lookup
var jsonDict map[string]interface{} var jsonDict map[string]interface{}
err := json.Unmarshal(data, &jsonDict) err = json.Unmarshal(data, &jsonDict)
if err != nil { if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discrimintor lookup.") return fmt.Errorf("Failed to unmarshal JSON into map for the discrimintor lookup.")
} }
@ -39,7 +39,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
// check if the discriminator value is '{{{mappingName}}}' // check if the discriminator value is '{{{mappingName}}}'
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" { if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
// try to unmarshal JSON data into {{{modelName}}} // try to unmarshal JSON data into {{{modelName}}}
err = json.Unmarshal(data, &dst.{{{modelName}}}); err = json.Unmarshal(data, &dst.{{{modelName}}})
if err == nil { if err == nil {
json{{{modelName}}}, _ := json.Marshal(dst.{{{modelName}}}) json{{{modelName}}}, _ := json.Marshal(dst.{{{modelName}}})
if string(json{{{modelName}}}) == "{}" { // empty struct if string(json{{{modelName}}}) == "{}" { // empty struct
@ -57,7 +57,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
{{/useOneOfDiscriminatorLookup}} {{/useOneOfDiscriminatorLookup}}
{{#oneOf}} {{#oneOf}}
// try to unmarshal data into {{{.}}} // try to unmarshal data into {{{.}}}
err = json.Unmarshal(data, &dst.{{{.}}}); err = json.Unmarshal(data, &dst.{{{.}}})
if err == nil { if err == nil {
json{{{.}}}, _ := json.Marshal(dst.{{{.}}}) json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
if string(json{{{.}}}) == "{}" { // empty struct if string(json{{{.}}}) == "{}" { // empty struct

View File

@ -36,7 +36,7 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
var err error var err error
match := 0 match := 0
// try to unmarshal data into Apple // try to unmarshal data into Apple
err = json.Unmarshal(data, &dst.Apple); err = json.Unmarshal(data, &dst.Apple)
if err == nil { if err == nil {
jsonApple, _ := json.Marshal(dst.Apple) jsonApple, _ := json.Marshal(dst.Apple)
if string(jsonApple) == "{}" { // empty struct if string(jsonApple) == "{}" { // empty struct
@ -49,7 +49,7 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
} }
// try to unmarshal data into Banana // try to unmarshal data into Banana
err = json.Unmarshal(data, &dst.Banana); err = json.Unmarshal(data, &dst.Banana)
if err == nil { if err == nil {
jsonBanana, _ := json.Marshal(dst.Banana) jsonBanana, _ := json.Marshal(dst.Banana)
if string(jsonBanana) == "{}" { // empty struct if string(jsonBanana) == "{}" { // empty struct

View File

@ -36,7 +36,7 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
var err error var err error
match := 0 match := 0
// try to unmarshal data into AppleReq // try to unmarshal data into AppleReq
err = json.Unmarshal(data, &dst.AppleReq); err = json.Unmarshal(data, &dst.AppleReq)
if err == nil { if err == nil {
jsonAppleReq, _ := json.Marshal(dst.AppleReq) jsonAppleReq, _ := json.Marshal(dst.AppleReq)
if string(jsonAppleReq) == "{}" { // empty struct if string(jsonAppleReq) == "{}" { // empty struct
@ -49,7 +49,7 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
} }
// try to unmarshal data into BananaReq // try to unmarshal data into BananaReq
err = json.Unmarshal(data, &dst.BananaReq); err = json.Unmarshal(data, &dst.BananaReq)
if err == nil { if err == nil {
jsonBananaReq, _ := json.Marshal(dst.BananaReq) jsonBananaReq, _ := json.Marshal(dst.BananaReq)
if string(jsonBananaReq) == "{}" { // empty struct if string(jsonBananaReq) == "{}" { // empty struct

View File

@ -36,7 +36,7 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
var err error var err error
match := 0 match := 0
// try to unmarshal data into Whale // try to unmarshal data into Whale
err = json.Unmarshal(data, &dst.Whale); err = json.Unmarshal(data, &dst.Whale)
if err == nil { if err == nil {
jsonWhale, _ := json.Marshal(dst.Whale) jsonWhale, _ := json.Marshal(dst.Whale)
if string(jsonWhale) == "{}" { // empty struct if string(jsonWhale) == "{}" { // empty struct
@ -49,7 +49,7 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
} }
// try to unmarshal data into Zebra // try to unmarshal data into Zebra
err = json.Unmarshal(data, &dst.Zebra); err = json.Unmarshal(data, &dst.Zebra)
if err == nil { if err == nil {
jsonZebra, _ := json.Marshal(dst.Zebra) jsonZebra, _ := json.Marshal(dst.Zebra)
if string(jsonZebra) == "{}" { // empty struct if string(jsonZebra) == "{}" { // empty struct