Skip to content

Commit 53264a2

Browse files
authored
fix(go): emit the oneOf no-match error once instead of once per member (#24683)
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
1 parent d098bc3 commit 53264a2

9 files changed

Lines changed: 50 additions & 105 deletions

File tree

modules/openapi-generator/src/main/resources/go/model_oneof.mustache

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
8080
} else if match == 1 {
8181
return nil // exactly one match
8282
} else { // no match
83-
{{#oneOf}}
84-
if err != nil {
85-
return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
86-
} else {
87-
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
88-
}
89-
{{/oneOf}}
90-
{{^oneOf}}
91-
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
92-
{{/oneOf}}
83+
if err != nil {
84+
return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
85+
}
86+
87+
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
9388
}
9489
{{/discriminator}}
9590
{{/useOneOfDiscriminatorLookup}}
@@ -124,16 +119,11 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
124119
} else if match == 1 {
125120
return nil // exactly one match
126121
} else { // no match
127-
{{#oneOf}}
128-
if err != nil {
129-
return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
130-
} else {
131-
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
132-
}
133-
{{/oneOf}}
134-
{{^oneOf}}
135-
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
136-
{{/oneOf}}
122+
if err != nil {
123+
return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
124+
}
125+
126+
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
137127
}
138128
{{/useOneOfDiscriminatorLookup}}
139129
}

samples/client/others/go/oneof-anyof-required/model_object.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_fruit.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_fruit_req.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_incident_data.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_mammal.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_type.go

Lines changed: 5 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_one_of_primitive_types.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)