From 0050666ca329ed55d58c51a1bd546e7ec4bf21ec Mon Sep 17 00:00:00 2001 From: kdelay Date: Wed, 12 Aug 2026 11:02:45 +0900 Subject: [PATCH] fix(go): emit the oneOf no-match error once instead of once per member The no-match branch of the generated UnmarshalJSON was wrapped in a {{#oneOf}} section, so the error return introduced by #24349 was rendered once per oneOf member. Only the first copy is reachable; the rest is dead code that grows with the number of members. Render the block once, outside the section. err is declared unconditionally at the top of UnmarshalJSON, so the {{^oneOf}} fallback is no longer needed. Behaviour is unchanged: the first copy is what executed before. Regenerate the affected go samples. fix #24661 --- .../main/resources/go/model_oneof.mustache | 30 +++++++------------ .../go/oneof-anyof-required/model_object.go | 15 ++++------ .../petstore/go/go-petstore/model_fruit.go | 15 ++++------ .../go/go-petstore/model_fruit_req.go | 15 ++++------ .../go/go-petstore/model_incident_data.go | 15 ++++------ .../petstore/go/go-petstore/model_mammal.go | 15 ++++------ .../model_one_of_primitive_type.go | 20 ++++--------- .../model_one_of_primitive_types.go | 15 ++++------ .../model_one_of_with_complex_type.go | 15 ++++------ 9 files changed, 50 insertions(+), 105 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go/model_oneof.mustache b/modules/openapi-generator/src/main/resources/go/model_oneof.mustache index 5021dd1df0f2..daaf9360b19b 100644 --- a/modules/openapi-generator/src/main/resources/go/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_oneof.mustache @@ -80,16 +80,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - {{#oneOf}} - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf({{classname}})") - } - {{/oneOf}} - {{^oneOf}} - return fmt.Errorf("data failed to match schemas in oneOf({{classname}})") - {{/oneOf}} + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf({{classname}})") } {{/discriminator}} {{/useOneOfDiscriminatorLookup}} @@ -124,16 +119,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - {{#oneOf}} - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf({{classname}})") - } - {{/oneOf}} - {{^oneOf}} - return fmt.Errorf("data failed to match schemas in oneOf({{classname}})") - {{/oneOf}} + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf({{classname}})") } {{/useOneOfDiscriminatorLookup}} } diff --git a/samples/client/others/go/oneof-anyof-required/model_object.go b/samples/client/others/go/oneof-anyof-required/model_object.go index 946783e59f0f..5c71521f1702 100644 --- a/samples/client/others/go/oneof-anyof-required/model_object.go +++ b/samples/client/others/go/oneof-anyof-required/model_object.go @@ -84,16 +84,11 @@ func (dst *Object) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(Object): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(Object)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(Object): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(Object)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(Object): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(Object)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_fruit.go b/samples/openapi3/client/petstore/go/go-petstore/model_fruit.go index 30c3b9ffa59a..a9baa9c39a18 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_fruit.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_fruit.go @@ -84,16 +84,11 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(Fruit): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(Fruit)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(Fruit): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(Fruit)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(Fruit): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(Fruit)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_fruit_req.go b/samples/openapi3/client/petstore/go/go-petstore/model_fruit_req.go index 6b49630ff444..5cc9844f5ab5 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_fruit_req.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_fruit_req.go @@ -84,16 +84,11 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(FruitReq): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(FruitReq)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(FruitReq): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(FruitReq)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(FruitReq): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(FruitReq)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_incident_data.go b/samples/openapi3/client/petstore/go/go-petstore/model_incident_data.go index 5ab3c7c5e7d6..207222c9f018 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_incident_data.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_incident_data.go @@ -89,16 +89,11 @@ func (dst *IncidentData) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(IncidentData): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(IncidentData)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(IncidentData): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(IncidentData)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(IncidentData): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(IncidentData)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_mammal.go b/samples/openapi3/client/petstore/go/go-petstore/model_mammal.go index e8a67f3943bf..f5f588b6286b 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_mammal.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_mammal.go @@ -84,16 +84,11 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(Mammal): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(Mammal)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(Mammal): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(Mammal)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(Mammal): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(Mammal)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_type.go b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_type.go index 512b240afdc9..ed5b7b6936ce 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_type.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_type.go @@ -110,21 +110,11 @@ func (dst *OneOfPrimitiveType) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveType)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_types.go b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_types.go index 9bce2b8be1d9..ea54b0351f6c 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_types.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_types.go @@ -85,16 +85,11 @@ func (dst *OneOfPrimitiveTypes) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveTypes): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveTypes)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveTypes): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveTypes)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveTypes): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(OneOfPrimitiveTypes)") } } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go index fc4192914bda..89752db0c4db 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go @@ -84,16 +84,11 @@ func (dst *OneOfWithComplexType) UnmarshalJSON(data []byte) error { } else if match == 1 { return nil // exactly one match } else { // no match - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType)") - } - if err != nil { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType): %v", err) - } else { - return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType)") - } + if err != nil { + return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType): %v", err) + } + + return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType)") } }