Skip to content

Commit 8d9eb5b

Browse files
mcristina422wing328
authored andcommitted
refactor(golang): Use http provided constants for http methods (#3028)
* refactor(golang): Use http constants for methods * regenerate samples * fix: Only import strings when needed * regenerate samples * Only import fmt and strings when needed * regenerate samples
1 parent 6b7c5ed commit 8d9eb5b

File tree

82 files changed

+885
-333
lines changed

Some content is hidden

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

82 files changed

+885
-333
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,11 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
361361
iterator.remove();
362362
}
363363

364-
// this will only import "fmt" if there are items in pathParams
364+
// this will only import "fmt" and "strings" if there are items in pathParams
365365
for (CodegenOperation operation : operations) {
366366
if (operation.pathParams != null && operation.pathParams.size() > 0) {
367367
imports.add(createMapping("import", "fmt"));
368+
imports.add(createMapping("import", "strings"));
368369
break; //just need to import once
369370
}
370371
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io/ioutil"
88
"net/http"
99
"net/url"
10-
"strings"
1110
{{#imports}} "{{import}}"
1211
{{/imports}}
1312
)
@@ -66,7 +65,7 @@ type {{{nickname}}}Opts struct {
6665
{{/hasOptionalParams}}
6766
func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}}, {{/hasParams}}{{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}localVarOptionals *{{{nickname}}}Opts{{/hasOptionalParams}}) ({{#returnType}}{{{returnType}}}, {{/returnType}}*http.Response, error) {
6867
var (
69-
localVarHttpMethod = strings.ToUpper("{{httpMethod}}")
68+
localVarHttpMethod = http.Method{{httpMethod}}
7069
localVarPostBody interface{}
7170
localVarFormFileName string
7271
localVarFileName string
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.0-SNAPSHOT
1+
4.0.1-SNAPSHOT

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

Lines changed: 39 additions & 9 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
```
@@ -70,18 +72,27 @@ Class | Method | HTTP request | Description
7072

7173
## Documentation For Models
7274

75+
- [AdditionalPropertiesAnyType](docs/AdditionalPropertiesAnyType.md)
76+
- [AdditionalPropertiesArray](docs/AdditionalPropertiesArray.md)
77+
- [AdditionalPropertiesBoolean](docs/AdditionalPropertiesBoolean.md)
7378
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
79+
- [AdditionalPropertiesInteger](docs/AdditionalPropertiesInteger.md)
80+
- [AdditionalPropertiesNumber](docs/AdditionalPropertiesNumber.md)
81+
- [AdditionalPropertiesObject](docs/AdditionalPropertiesObject.md)
82+
- [AdditionalPropertiesString](docs/AdditionalPropertiesString.md)
7483
- [Animal](docs/Animal.md)
7584
- [ApiResponse](docs/ApiResponse.md)
7685
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
7786
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
7887
- [ArrayTest](docs/ArrayTest.md)
7988
- [Capitalization](docs/Capitalization.md)
8089
- [Cat](docs/Cat.md)
90+
- [CatAllOf](docs/CatAllOf.md)
8191
- [Category](docs/Category.md)
8292
- [ClassModel](docs/ClassModel.md)
8393
- [Client](docs/Client.md)
8494
- [Dog](docs/Dog.md)
95+
- [DogAllOf](docs/DogAllOf.md)
8596
- [EnumArrays](docs/EnumArrays.md)
8697
- [EnumClass](docs/EnumClass.md)
8798
- [EnumTest](docs/EnumTest.md)
@@ -111,40 +122,56 @@ Class | Method | HTTP request | Description
111122

112123
## Documentation For Authorization
113124

125+
126+
114127
## api_key
115-
- **Type**: API key
128+
129+
- **Type**: API key
116130

117131
Example
132+
118133
```golang
119134
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
120-
Key: "APIKEY",
121-
Prefix: "Bearer", // Omit if not necessary.
135+
Key: "APIKEY",
136+
Prefix: "Bearer", // Omit if not necessary.
122137
})
123138
r, err := client.Service.Operation(auth, args)
124139
```
140+
141+
125142
## api_key_query
126-
- **Type**: API key
143+
144+
- **Type**: API key
127145

128146
Example
147+
129148
```golang
130149
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
131-
Key: "APIKEY",
132-
Prefix: "Bearer", // Omit if not necessary.
150+
Key: "APIKEY",
151+
Prefix: "Bearer", // Omit if not necessary.
133152
})
134153
r, err := client.Service.Operation(auth, args)
135154
```
155+
156+
136157
## http_basic_test
158+
137159
- **Type**: HTTP basic authentication
138160

139161
Example
162+
140163
```golang
141164
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
142-
UserName: "username",
143-
Password: "password",
165+
UserName: "username",
166+
Password: "password",
144167
})
145168
r, err := client.Service.Operation(auth, args)
146169
```
170+
171+
147172
## petstore_auth
173+
174+
148175
- **Type**: OAuth
149176
- **Flow**: implicit
150177
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
@@ -153,12 +180,14 @@ r, err := client.Service.Operation(auth, args)
153180
- **read:pets**: read your pets
154181

155182
Example
183+
156184
```golang
157185
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
158186
r, err := client.Service.Operation(auth, args)
159187
```
160188

161189
Or via OAuth2 module to automatically refresh tokens and perform user authentication.
190+
162191
```golang
163192
import "golang.org/x/oauth2"
164193

@@ -169,6 +198,7 @@ auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
169198
r, err := client.Service.Operation(auth, args)
170199
```
171200

201+
172202
## Author
173203

174204

samples/client/petstore/go/go-petstore-withXml/api/openapi.yaml

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ paths:
3232
description: Pet object that needs to be added to the store
3333
required: true
3434
responses:
35+
200:
36+
content: {}
37+
description: successful operation
3538
405:
3639
content: {}
3740
description: Invalid input
@@ -56,6 +59,9 @@ paths:
5659
description: Pet object that needs to be added to the store
5760
required: true
5861
responses:
62+
200:
63+
content: {}
64+
description: successful operation
5965
400:
6066
content: {}
6167
description: Invalid ID supplied
@@ -120,7 +126,8 @@ paths:
120126
/pet/findByTags:
121127
get:
122128
deprecated: true
123-
description: Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
129+
description: Multiple tags can be provided with comma separated strings. Use
130+
tag1, tag2, tag3 for testing.
124131
operationId: findPetsByTags
125132
parameters:
126133
- description: Tags to filter by
@@ -173,6 +180,9 @@ paths:
173180
format: int64
174181
type: integer
175182
responses:
183+
200:
184+
content: {}
185+
description: successful operation
176186
400:
177187
content: {}
178188
description: Invalid pet value
@@ -332,7 +342,8 @@ paths:
332342
x-codegen-request-body-name: body
333343
/store/order/{order_id}:
334344
delete:
335-
description: For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
345+
description: For valid response try integer IDs with value < 1000. Anything
346+
above 1000 or nonintegers will generate API errors
336347
operationId: deleteOrder
337348
parameters:
338349
- description: ID of the order that needs to be deleted
@@ -352,7 +363,8 @@ paths:
352363
tags:
353364
- store
354365
get:
355-
description: For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
366+
description: For valid response try integer IDs with value <= 5 or > 10. Other
367+
values will generated exceptions
356368
operationId: getOrderById
357369
parameters:
358370
- description: ID of pet that needs to be fetched
@@ -1072,7 +1084,8 @@ paths:
10721084
x-codegen-request-body-name: body
10731085
/fake/body-with-file-schema:
10741086
put:
1075-
description: For this test, the body for this request much reference a schema named `File`.
1087+
description: For this test, the body for this request much reference a schema
1088+
named `File`.
10761089
operationId: testBodyWithFileSchema
10771090
requestBody:
10781091
content:
@@ -1350,17 +1363,11 @@ components:
13501363
Dog:
13511364
allOf:
13521365
- $ref: '#/components/schemas/Animal'
1353-
- properties:
1354-
breed:
1355-
type: string
1356-
type: object
1366+
- $ref: '#/components/schemas/Dog_allOf'
13571367
Cat:
13581368
allOf:
13591369
- $ref: '#/components/schemas/Animal'
1360-
- properties:
1361-
declawed:
1362-
type: boolean
1363-
type: object
1370+
- $ref: '#/components/schemas/Cat_allOf'
13641371
Animal:
13651372
discriminator:
13661373
propertyName: className
@@ -2009,6 +2016,14 @@ components:
20092016
xml:
20102017
namespace: http://a.com/schema
20112018
prefix: pre
2019+
Dog_allOf:
2020+
properties:
2021+
breed:
2022+
type: string
2023+
Cat_allOf:
2024+
properties:
2025+
declawed:
2026+
type: boolean
20122027
securitySchemes:
20132028
petstore_auth:
20142029
flows:
@@ -2028,4 +2043,4 @@ components:
20282043
type: apiKey
20292044
http_basic_test:
20302045
scheme: basic
2031-
type: http
2046+
type: http

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

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)