Skip to content

Commit c0e11ec

Browse files
dnaeonwing328
authored andcommitted
Add debug flag support for go-experimental generator (#4649)
* Add debug flag for go-experimental generator * Check for errors before dumping the response in Go generator * samples/openapi3: update samples for Go generators
1 parent 030a7a8 commit c0e11ec

File tree

11 files changed

+91
-4
lines changed

11 files changed

+91
-4
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"log"
1213
"mime/multipart"
1314
"net/http"
15+
"net/http/httputil"
1416
"net/url"
1517
"os"
1618
"path/filepath"
@@ -161,7 +163,27 @@ func parameterToJson(obj interface{}) (string, error) {
161163

162164
// callAPI do the request.
163165
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
164-
return c.cfg.HTTPClient.Do(request)
166+
if c.cfg.Debug {
167+
dump, err := httputil.DumpRequestOut(request, true)
168+
if err != nil {
169+
return nil, err
170+
}
171+
log.Printf("\n%s\n", string(dump))
172+
}
173+
174+
resp, err := c.cfg.HTTPClient.Do(request)
175+
if err != nil {
176+
return resp, err
177+
}
178+
179+
if c.cfg.Debug {
180+
dump, err := httputil.DumpResponse(resp, true)
181+
if err != nil {
182+
return resp, err
183+
}
184+
log.Printf("\n%s\n", string(dump))
185+
}
186+
return resp, err
165187
}
166188

167189
// ChangeBasePath changes base path to allow switching to mocks

modules/openapi-generator/src/main/resources/go-experimental/configuration.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Configuration struct {
4848
Scheme string `json:"scheme,omitempty"`
4949
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
5050
UserAgent string `json:"userAgent,omitempty"`
51+
Debug bool `json:"debug,omitempty"`
5152
HTTPClient *http.Client
5253
}
5354

@@ -57,6 +58,7 @@ func NewConfiguration() *Configuration {
5758
BasePath: "{{{basePath}}}",
5859
DefaultHeader: make(map[string]string),
5960
UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}",
61+
Debug: false,
6062
}
6163
return cfg
6264
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
172172
}
173173

174174
resp, err := c.cfg.HTTPClient.Do(request)
175+
if err != nil {
176+
return resp, err
177+
}
175178

176179
if c.cfg.Debug {
177180
dump, err := httputil.DumpResponse(resp, true)

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"errors"
1818
"fmt"
1919
"io"
20+
"log"
2021
"mime/multipart"
2122
"net/http"
23+
"net/http/httputil"
2224
"net/url"
2325
"os"
2426
"path/filepath"
@@ -172,7 +174,27 @@ func parameterToJson(obj interface{}) (string, error) {
172174

173175
// callAPI do the request.
174176
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
175-
return c.cfg.HTTPClient.Do(request)
177+
if c.cfg.Debug {
178+
dump, err := httputil.DumpRequestOut(request, true)
179+
if err != nil {
180+
return nil, err
181+
}
182+
log.Printf("\n%s\n", string(dump))
183+
}
184+
185+
resp, err := c.cfg.HTTPClient.Do(request)
186+
if err != nil {
187+
return resp, err
188+
}
189+
190+
if c.cfg.Debug {
191+
dump, err := httputil.DumpResponse(resp, true)
192+
if err != nil {
193+
return resp, err
194+
}
195+
log.Printf("\n%s\n", string(dump))
196+
}
197+
return resp, err
176198
}
177199

178200
// ChangeBasePath changes base path to allow switching to mocks

samples/client/petstore/go-experimental/go-petstore/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Configuration struct {
5656
Scheme string `json:"scheme,omitempty"`
5757
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
5858
UserAgent string `json:"userAgent,omitempty"`
59+
Debug bool `json:"debug,omitempty"`
5960
HTTPClient *http.Client
6061
}
6162

@@ -65,6 +66,7 @@ func NewConfiguration() *Configuration {
6566
BasePath: "http://petstore.swagger.io:80/v2",
6667
DefaultHeader: make(map[string]string),
6768
UserAgent: "OpenAPI-Generator/1.0.0/go",
69+
Debug: false,
6870
}
6971
return cfg
7072
}

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

Lines changed: 3 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
183183
}
184184

185185
resp, err := c.cfg.HTTPClient.Do(request)
186+
if err != nil {
187+
return resp, err
188+
}
186189

187190
if c.cfg.Debug {
188191
dump, err := httputil.DumpResponse(resp, true)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.2.0-SNAPSHOT
1+
4.2.2-SNAPSHOT

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import (
1717
"errors"
1818
"fmt"
1919
"io"
20+
"log"
2021
"mime/multipart"
2122
"net/http"
23+
"net/http/httputil"
2224
"net/url"
2325
"os"
2426
"path/filepath"
@@ -175,7 +177,27 @@ func parameterToJson(obj interface{}) (string, error) {
175177

176178
// callAPI do the request.
177179
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
178-
return c.cfg.HTTPClient.Do(request)
180+
if c.cfg.Debug {
181+
dump, err := httputil.DumpRequestOut(request, true)
182+
if err != nil {
183+
return nil, err
184+
}
185+
log.Printf("\n%s\n", string(dump))
186+
}
187+
188+
resp, err := c.cfg.HTTPClient.Do(request)
189+
if err != nil {
190+
return resp, err
191+
}
192+
193+
if c.cfg.Debug {
194+
dump, err := httputil.DumpResponse(resp, true)
195+
if err != nil {
196+
return resp, err
197+
}
198+
log.Printf("\n%s\n", string(dump))
199+
}
200+
return resp, err
179201
}
180202

181203
// ChangeBasePath changes base path to allow switching to mocks
@@ -353,6 +375,9 @@ func (c *APIClient) prepareRequest(
353375
}
354376

355377
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
378+
if len(b) == 0 {
379+
return nil
380+
}
356381
if s, ok := v.(*string); ok {
357382
*s = string(b)
358383
return nil

samples/openapi3/client/petstore/go-experimental/go-petstore/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Configuration struct {
5656
Scheme string `json:"scheme,omitempty"`
5757
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
5858
UserAgent string `json:"userAgent,omitempty"`
59+
Debug bool `json:"debug,omitempty"`
5960
HTTPClient *http.Client
6061
}
6162

@@ -65,6 +66,7 @@ func NewConfiguration() *Configuration {
6566
BasePath: "http://petstore.swagger.io:80/v2",
6667
DefaultHeader: make(map[string]string),
6768
UserAgent: "OpenAPI-Generator/1.0.0/go",
69+
Debug: false,
6870
}
6971
return cfg
7072
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
186186
}
187187

188188
resp, err := c.cfg.HTTPClient.Do(request)
189+
if err != nil {
190+
return resp, err
191+
}
189192

190193
if c.cfg.Debug {
191194
dump, err := httputil.DumpResponse(resp, true)

0 commit comments

Comments
 (0)