Skip to content

Commit eaae895

Browse files
[Go][Client] Use configured Scheme and Host in requests
Previously we were simply setting the Host header and completely ignoring the configured scheme.
1 parent e5179b0 commit eaae895

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+798
-322
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ func (c *APIClient) prepareRequest(
244244
return nil, err
245245
}
246246

247+
// Override request host, if applicable
248+
if c.cfg.Host != "" {
249+
url.Host = c.cfg.Host
250+
}
251+
252+
// Override request scheme, if applicable
253+
if c.cfg.Scheme != "" {
254+
url.Scheme = c.cfg.Scheme
255+
}
256+
247257
// Adding Query Param
248258
query := url.Query()
249259
for k, v := range queryParams {
@@ -274,11 +284,6 @@ func (c *APIClient) prepareRequest(
274284
localVarRequest.Header = headers
275285
}
276286

277-
// Override request host, if applicable
278-
if c.cfg.Host != "" {
279-
localVarRequest.Host = c.cfg.Host
280-
}
281-
282287
// Add the user agent to the request.
283288
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
284289

samples/client/petstore/go/auth_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,23 @@ func TestHostOverride(t *testing.T) {
229229
t.Fatalf("Error while finding pets by status: %v", err)
230230
}
231231

232-
if r.Request.Host != testHost {
232+
if r.Request.URL.Host != testHost {
233233
t.Errorf("Request Host is %v, expected %v", r.Request.Host, testHost)
234234
}
235235
}
236236

237+
func TestSchemeOverride(t *testing.T) {
238+
_, r, err := client.PetApi.FindPetsByStatus(context.Background(), nil)
239+
240+
if err != nil {
241+
t.Fatalf("Error while finding pets by status: %v", err)
242+
}
243+
244+
if r.Request.URL.Scheme != testScheme {
245+
t.Errorf("Request Scheme is %v, expected %v", r.Request.URL.Scheme, testScheme)
246+
}
247+
}
248+
237249
// Add custom clients to the context.
238250
func createContext(httpClient *http.Client) context.Context {
239251
parent := oauth2.NoContext

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

Lines changed: 10 additions & 5 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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ func (c *APIClient) prepareRequest(
255255
return nil, err
256256
}
257257

258+
// Override request host, if applicable
259+
if c.cfg.Host != "" {
260+
url.Host = c.cfg.Host
261+
}
262+
263+
// Override request scheme, if applicable
264+
if c.cfg.Scheme != "" {
265+
url.Scheme = c.cfg.Scheme
266+
}
267+
258268
// Adding Query Param
259269
query := url.Query()
260270
for k, v := range queryParams {
@@ -285,11 +295,6 @@ func (c *APIClient) prepareRequest(
285295
localVarRequest.Header = headers
286296
}
287297

288-
// Override request host, if applicable
289-
if c.cfg.Host != "" {
290-
localVarRequest.Host = c.cfg.Host
291-
}
292-
293298
// Add the user agent to the request.
294299
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
295300

samples/client/petstore/go/pet_api_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import (
1414

1515
var client *sw.APIClient
1616

17-
const testHost = "testhost"
17+
const testHost = "petstore.swagger.io:80"
18+
const testScheme = "http"
1819

1920
func TestMain(m *testing.M) {
2021
cfg := sw.NewConfiguration()
2122
cfg.AddDefaultHeader("testheader", "testvalue")
2223
cfg.Host = testHost
24+
cfg.Scheme = testScheme
2325
client = sw.NewAPIClient(cfg)
2426
retCode := m.Run()
2527
os.Exit(retCode)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.0-SNAPSHOT
1+
4.0.2-SNAPSHOT

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ This API client was generated by the [OpenAPI Generator](https://openapi-generat
1212
## Installation
1313

1414
Install the following dependencies:
15-
```
15+
16+
```shell
1617
go get github.com/stretchr/testify/assert
1718
go get golang.org/x/oauth2
1819
go get golang.org/x/net/context
1920
go get github.com/antihax/optional
2021
```
2122

2223
Put the package under your project folder and add the following in import:
24+
2325
```golang
2426
import "./petstore"
2527
```
@@ -79,10 +81,12 @@ Class | Method | HTTP request | Description
7981
- [ArrayTest](docs/ArrayTest.md)
8082
- [Capitalization](docs/Capitalization.md)
8183
- [Cat](docs/Cat.md)
84+
- [CatAllOf](docs/CatAllOf.md)
8285
- [Category](docs/Category.md)
8386
- [ClassModel](docs/ClassModel.md)
8487
- [Client](docs/Client.md)
8588
- [Dog](docs/Dog.md)
89+
- [DogAllOf](docs/DogAllOf.md)
8690
- [EnumArrays](docs/EnumArrays.md)
8791
- [EnumClass](docs/EnumClass.md)
8892
- [EnumTest](docs/EnumTest.md)
@@ -104,6 +108,7 @@ Class | Method | HTTP request | Description
104108
- [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
105109
- [Model200Response](docs/Model200Response.md)
106110
- [Name](docs/Name.md)
111+
- [NullableClass](docs/NullableClass.md)
107112
- [NumberOnly](docs/NumberOnly.md)
108113
- [Order](docs/Order.md)
109114
- [OuterComposite](docs/OuterComposite.md)
@@ -121,51 +126,71 @@ Class | Method | HTTP request | Description
121126

122127
## Documentation For Authorization
123128

129+
130+
124131
## api_key
125-
- **Type**: API key
132+
133+
- **Type**: API key
126134

127135
Example
136+
128137
```golang
129138
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
130-
Key: "APIKEY",
131-
Prefix: "Bearer", // Omit if not necessary.
139+
Key: "APIKEY",
140+
Prefix: "Bearer", // Omit if not necessary.
132141
})
133142
r, err := client.Service.Operation(auth, args)
134143
```
144+
145+
135146
## api_key_query
136-
- **Type**: API key
147+
148+
- **Type**: API key
137149

138150
Example
151+
139152
```golang
140153
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
141-
Key: "APIKEY",
142-
Prefix: "Bearer", // Omit if not necessary.
154+
Key: "APIKEY",
155+
Prefix: "Bearer", // Omit if not necessary.
143156
})
144157
r, err := client.Service.Operation(auth, args)
145158
```
159+
160+
146161
## bearer_test
162+
147163
- **Type**: HTTP basic authentication
148164

149165
Example
166+
150167
```golang
151168
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
152-
UserName: "username",
153-
Password: "password",
169+
UserName: "username",
170+
Password: "password",
154171
})
155172
r, err := client.Service.Operation(auth, args)
156173
```
174+
175+
157176
## http_basic_test
177+
158178
- **Type**: HTTP basic authentication
159179

160180
Example
181+
161182
```golang
162183
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
163-
UserName: "username",
164-
Password: "password",
184+
UserName: "username",
185+
Password: "password",
165186
})
166187
r, err := client.Service.Operation(auth, args)
167188
```
189+
190+
168191
## petstore_auth
192+
193+
169194
- **Type**: OAuth
170195
- **Flow**: implicit
171196
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
@@ -174,12 +199,14 @@ r, err := client.Service.Operation(auth, args)
174199
- **read:pets**: read your pets
175200

176201
Example
202+
177203
```golang
178204
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
179205
r, err := client.Service.Operation(auth, args)
180206
```
181207

182208
Or via OAuth2 module to automatically refresh tokens and perform user authentication.
209+
183210
```golang
184211
import "golang.org/x/oauth2"
185212

@@ -190,6 +217,7 @@ auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
190217
r, err := client.Service.Operation(auth, args)
191218
```
192219

220+
193221
## Author
194222

195223

0 commit comments

Comments
 (0)