Skip to content

Commit 86b0a52

Browse files
committed
[go] support io.Reader and []byte response types in client decode
The Go generator maps binary types to *os.File by default. Callers can override that with --type-mappings to get io.Reader or []byte, and setBody already handles both for request bodies. The shared decode helper, however, only asserts *string, *os.File, and **os.File. When Execute declares localVarReturnValue io.Reader or []byte and calls decode(&localVarReturnValue, ...), no branch matches and the call returns 'undefined response type'. Add two branches: - *io.Reader wraps the already-buffered bytes in a bytes.Reader. - *[]byte assigns the bytes directly. This branch must stay before the JSON branch, since json.Unmarshal accepts *[]byte and base64-decodes into it, which is not what we want for raw binary responses. Both shapes are useful for different scenarios: []byte is more honest (the response is fully buffered in memory anyway), while io.Reader is forward-compatible (the return type stays valid if the response path is ever refactored to skip the eager buffering and stream the body). bytes is already imported in every generated client.go. The same additive patch is applied to the eleven Go client samples.
1 parent 2114edc commit 86b0a52

12 files changed

Lines changed: 108 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,15 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
539539
*s = string(b)
540540
return nil
541541
}
542+
if r, ok := v.(*io.Reader); ok {
543+
*r = bytes.NewReader(b)
544+
return nil
545+
}
546+
// Must stay before the JSON branch: json.Unmarshal would base64-decode into *[]byte.
547+
if p, ok := v.(*[]byte); ok {
548+
*p = b
549+
return nil
550+
}
542551
if f, ok := v.(*os.File); ok {
543552
f, err = os.CreateTemp("", "HttpClientFile")
544553
if err != nil {

samples/client/echo_api/go-external-refs/client.go

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

samples/client/echo_api/go/client.go

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

samples/client/others/go/allof_multiple_ref_and_discriminator/client.go

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

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

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

samples/client/others/go/oneof-discriminator-lookup/client.go

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

samples/client/petstore/go/go-petstore/client.go

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

samples/openapi3/client/extensions/x-auth-id-alias/go-experimental/client.go

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

samples/openapi3/client/petstore/go-petstore-generateMarshalJSON-false/client.go

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

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

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

0 commit comments

Comments
 (0)