Skip to content

Commit 74197cc

Browse files
authored
Merge pull request #9234 from swagger-api/bugfix/go-enum-conflict
[go] suffix enum name with classname to avoid conflict
2 parents f147928 + 963b9e8 commit 74197cc

File tree

18 files changed

+225
-400
lines changed

18 files changed

+225
-400
lines changed

modules/swagger-codegen/src/main/resources/go/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313
{{#enumVars}}
1414
{{^-first}}
1515
{{/-first}}
16-
{{name}} {{{classname}}} = "{{{value}}}"
16+
{{name}}_{{{classname}}} {{{classname}}} = "{{{value}}}"
1717
{{/enumVars}}
1818
{{/allowableValues}}
1919
){{/isEnum}}{{^isEnum}}{{#description}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.0-SNAPSHOT
1+
2.3.1

samples/client/petstore-security-test/go/README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ This API client was generated by the [swagger-codegen](https://github.com/swagge
1111

1212
## Installation
1313
Put the package under your project folder and add the following in import:
14-
```
15-
"./swagger"
14+
```golang
15+
import "./swagger"
1616
```
1717

1818
## Documentation for API Endpoints
@@ -35,12 +35,12 @@ Class | Method | HTTP request | Description
3535
- **Type**: API key
3636

3737
Example
38-
```
39-
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
40-
Key: "APIKEY",
41-
Prefix: "Bearer", // Omit if not necessary.
42-
})
43-
r, err := client.Service.Operation(auth, args)
38+
```golang
39+
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
40+
Key: "APIKEY",
41+
Prefix: "Bearer", // Omit if not necessary.
42+
})
43+
r, err := client.Service.Operation(auth, args)
4444
```
4545
## petstore_auth
4646
- **Type**: OAuth
@@ -51,20 +51,20 @@ Example
5151
- **read:pets**: read your pets *_/ ' \" =end -- \\r\\n \\n \\r
5252

5353
Example
54-
```
55-
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
56-
r, err := client.Service.Operation(auth, args)
54+
```golang
55+
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
56+
r, err := client.Service.Operation(auth, args)
5757
```
5858

5959
Or via OAuth2 module to automatically refresh tokens and perform user authentication.
60-
```
61-
import "golang.org/x/oauth2"
60+
```golang
61+
import "golang.org/x/oauth2"
6262

63-
/ .. Perform OAuth2 round trip request and obtain a token .. //
63+
/* Perform OAuth2 round trip request and obtain a token */
6464

65-
tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
66-
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
67-
r, err := client.Service.Operation(auth, args)
65+
tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
66+
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
67+
r, err := client.Service.Operation(auth, args)
6868
```
6969

7070
## Author
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
swagger: "2.0"
3+
info:
4+
description: "This spec is mainly for testing Petstore server and contains fake\
5+
\ endpoints, models. Please do not use this for any other purpose. Special characters:\
6+
\ \" \\ */ ' \" =end -- \r\n \n \r"
7+
version: "1.0.0 */ ' \" =end -- \\r\\n \\n \\r"
8+
title: "Swagger Petstore */ ' \" =end -- \\r\\n \\n \\r"
9+
termsOfService: "http://swagger.io/terms/ */ ' \" =end -- \\r\\n \\n \\r"
10+
contact:
11+
email: "[email protected] */ ' \" =end -- \\r\\n \\n \\r"
12+
license:
13+
name: "Apache-2.0 */ ' \" =end -- \\r\\n \\n \\r"
14+
url: "http://www.apache.org/licenses/LICENSE-2.0.html */ ' \" =end -- \\r\\n \\\
15+
n \\r"
16+
host: "petstore.swagger.io */ ' \" =end -- \\r\\n \\n \\r"
17+
basePath: "/v2 */ ' \" =end -- \\r\\n \\n \\r"
18+
tags:
19+
- name: "fake"
20+
description: "Everything about your Pets */ ' \" =end -- \\r\\n \\n \\r"
21+
externalDocs:
22+
description: "Find out more */ ' \" =end -- \\r\\n \\n \\r"
23+
url: "http://swagger.io"
24+
paths:
25+
/fake:
26+
put:
27+
tags:
28+
- "fake"
29+
summary: "To test code injection */ ' \" =end -- \\r\\n \\n \\r"
30+
operationId: "testCodeInject */ ' \" =end -- \\r\\n \\n \\r"
31+
consumes:
32+
- "application/json"
33+
- "*/ ' \" =end -- \r\n \n \r"
34+
produces:
35+
- "application/json"
36+
- "*/ ' \" =end -- \r\n \n \r"
37+
parameters:
38+
- name: "test code inject */ ' \" =end -- \\r\\n \\n \\r"
39+
in: "formData"
40+
description: "To test code injection */ ' \" =end -- \\r\\n \\n \\r"
41+
required: false
42+
type: "string"
43+
x-exportParamName: "TestCodeInjectEndRnNR"
44+
responses:
45+
400:
46+
description: "To test code injection */ ' \" =end -- \\r\\n \\n \\r"
47+
securityDefinitions:
48+
petstore_auth:
49+
type: "oauth2"
50+
authorizationUrl: "http://petstore.swagger.io/api/oauth/dialog"
51+
flow: "implicit"
52+
scopes:
53+
write:pets: "modify pets in your account */ ' \" =end -- \\r\\n \\n \\r"
54+
read:pets: "read your pets */ ' \" =end -- \\r\\n \\n \\r"
55+
api_key:
56+
type: "apiKey"
57+
name: "api_key */ ' \" =end -- \\r\\n \\n \\r"
58+
in: "header"
59+
definitions:
60+
Return:
61+
properties:
62+
return:
63+
type: "integer"
64+
format: "int32"
65+
description: "property description */ ' \" =end -- \\r\\n \\n \\r"
66+
description: "Model for testing reserved words */ ' \" =end -- \\r\\n \\n \\r"
67+
xml:
68+
name: "Return"
69+
externalDocs:
70+
description: "Find out more about Swagger */ ' \" =end -- \\r\\n \\n \\r"
71+
url: "http://swagger.io"

samples/client/petstore-security-test/go/api_client.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
33
*
4-
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
4+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
55
*
66
* API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
77
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
@@ -14,40 +14,37 @@ import (
1414
"bytes"
1515
"encoding/json"
1616
"encoding/xml"
17-
"errors"
1817
"fmt"
18+
"errors"
1919
"io"
2020
"mime/multipart"
21+
"golang.org/x/oauth2"
22+
"golang.org/x/net/context"
2123
"net/http"
2224
"net/url"
25+
"time"
2326
"os"
2427
"path/filepath"
2528
"reflect"
2629
"regexp"
27-
"strconv"
2830
"strings"
29-
"time"
3031
"unicode/utf8"
31-
32-
"context"
33-
34-
"golang.org/x/oauth2"
32+
"strconv"
3533
)
3634

3735
var (
3836
jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
39-
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
37+
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
4038
)
4139

4240
// APIClient manages communication with the Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r API v1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
4341
// In most cases there should be only one, shared, APIClient.
4442
type APIClient struct {
45-
cfg *Configuration
46-
common service // Reuse a single struct instead of allocating one for each service on the heap.
47-
48-
// API Services
43+
cfg *Configuration
44+
common service // Reuse a single struct instead of allocating one for each service on the heap.
4945

50-
FakeApi *FakeApiService
46+
// API Services
47+
FakeApi *FakeApiService
5148
}
5249

5350
type service struct {
@@ -75,6 +72,7 @@ func atoi(in string) (int, error) {
7572
return strconv.Atoi(in)
7673
}
7774

75+
7876
// selectHeaderContentType select a content type from the available list.
7977
func selectHeaderContentType(contentTypes []string) string {
8078
if len(contentTypes) == 0 {
@@ -145,18 +143,18 @@ func parameterToString(obj interface{}, collectionFormat string) string {
145143
return fmt.Sprintf("%v", obj)
146144
}
147145

148-
// callAPI do the request.
146+
// callAPI do the request.
149147
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
150-
return c.cfg.HTTPClient.Do(request)
148+
return c.cfg.HTTPClient.Do(request)
151149
}
152150

153151
// Change base path to allow switching to mocks
154-
func (c *APIClient) ChangeBasePath(path string) {
152+
func (c *APIClient) ChangeBasePath (path string) {
155153
c.cfg.BasePath = path
156154
}
157155

158156
// prepareRequest build the request
159-
func (c *APIClient) prepareRequest(
157+
func (c *APIClient) prepareRequest (
160158
ctx context.Context,
161159
path string, method string,
162160
postBody interface{},
@@ -216,7 +214,7 @@ func (c *APIClient) prepareRequest(
216214
// Set the Boundary in the Content-Type
217215
headerParams["Content-Type"] = w.FormDataContentType()
218216
}
219-
217+
220218
// Set Content-Length
221219
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
222220
w.Close()
@@ -262,9 +260,10 @@ func (c *APIClient) prepareRequest(
262260
if c.cfg.Host != "" {
263261
localVarRequest.Host = c.cfg.Host
264262
}
265-
263+
266264
// Add the user agent to the request.
267265
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
266+
268267

269268
if ctx != nil {
270269
// add context to the request
@@ -290,17 +289,18 @@ func (c *APIClient) prepareRequest(
290289

291290
// AccessToken Authentication
292291
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
293-
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
292+
localVarRequest.Header.Add("Authorization", "Bearer " + auth)
294293
}
295294
}
296295

297296
for header, value := range c.cfg.DefaultHeader {
298297
localVarRequest.Header.Add(header, value)
299298
}
300-
299+
301300
return localVarRequest, nil
302301
}
303302

303+
304304
// Add a file to the multipart request
305305
func addFile(w *multipart.Writer, fieldName, path string) error {
306306
file, err := os.Open(path)
@@ -319,7 +319,7 @@ func addFile(w *multipart.Writer, fieldName, path string) error {
319319
}
320320

321321
// Prevent trying to import "fmt"
322-
func reportError(format string, a ...interface{}) error {
322+
func reportError(format string, a ...interface{}) (error) {
323323
return fmt.Errorf(format, a...)
324324
}
325325

@@ -356,7 +356,7 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
356356
func detectContentType(body interface{}) string {
357357
contentType := "text/plain; charset=utf-8"
358358
kind := reflect.TypeOf(body).Kind()
359-
359+
360360
switch kind {
361361
case reflect.Struct, reflect.Map, reflect.Ptr:
362362
contentType = "application/json; charset=utf-8"
@@ -373,6 +373,7 @@ func detectContentType(body interface{}) string {
373373
return contentType
374374
}
375375

376+
376377
// Ripped from https://github.com/gregjones/httpcache/blob/master/httpcache.go
377378
type cacheControl map[string]string
378379

@@ -395,15 +396,15 @@ func parseCacheControl(headers http.Header) cacheControl {
395396
}
396397

397398
// CacheExpires helper function to determine remaining time before repeating a request.
398-
func CacheExpires(r *http.Response) time.Time {
399+
func CacheExpires(r *http.Response) (time.Time) {
399400
// Figure out when the cache expires.
400401
var expires time.Time
401402
now, err := time.Parse(time.RFC1123, r.Header.Get("date"))
402403
if err != nil {
403404
return time.Now()
404405
}
405406
respCacheControl := parseCacheControl(r.Header)
406-
407+
407408
if maxAge, ok := respCacheControl["max-age"]; ok {
408409
lifetime, err := time.ParseDuration(maxAge + "s")
409410
if err != nil {
@@ -422,6 +423,7 @@ func CacheExpires(r *http.Response) time.Time {
422423
return expires
423424
}
424425

425-
func strlen(s string) int {
426+
func strlen(s string) (int) {
426427
return utf8.RuneCountInString(s)
427428
}
429+

samples/client/petstore-security-test/go/api_response.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import (
1616

1717
type APIResponse struct {
1818
*http.Response `json:"-"`
19-
Message string `json:"message,omitempty"`
19+
Message string `json:"message,omitempty"`
2020
// Operation is the name of the swagger operation.
21-
Operation string `json:"operation,omitempty"`
21+
Operation string `json:"operation,omitempty"`
2222
// RequestURL is the request URL. This value is always available, even if the
2323
// embedded *http.Response is nil.
24-
RequestURL string `json:"url,omitempty"`
24+
RequestURL string `json:"url,omitempty"`
2525
// Method is the HTTP method used for the request. This value is always
2626
// available, even if the embedded *http.Response is nil.
27-
Method string `json:"method,omitempty"`
27+
Method string `json:"method,omitempty"`
2828
// Payload holds the contents of the response body (which may be nil or empty).
2929
// This is provided here as the raw response.Body() reader will have already
3030
// been drained.

samples/client/petstore-security-test/go/docs/FakeApi.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
1515

1616
Name | Type | Description | Notes
1717
------------- | ------------- | ------------- | -------------
18-
**ctx** | **context.Context** | context for logging, tracing, authentication, etc.
19-
**optional** | **map[string]interface{}** | optional parameters | nil if no parameters
18+
**ctx** | **context.Context** | context for authentication, logging, cancellation, deadlines, tracing, etc.
19+
**optional** | ***TestCodeInjectEndRnNROpts** | optional parameters | nil if no parameters
2020

2121
### Optional Parameters
22-
Optional parameters are passed through a map[string]interface{}.
22+
Optional parameters are passed through a pointer to a TestCodeInjectEndRnNROpts struct
2323

2424
Name | Type | Description | Notes
2525
------------- | ------------- | ------------- | -------------
26-
**testCodeInjectEndRnNR** | **string**| To test code injection *_/ ' \" =end -- \\r\\n \\n \\r |
26+
**testCodeInjectEndRnNR** | **optional.**| To test code injection *_/ ' \" =end -- \\r\\n \\n \\r |
2727

2828
### Return type
2929

0 commit comments

Comments
 (0)