Skip to content

Commit

Permalink
Update go client, fix double body read (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
antihax authored and wing328 committed Apr 24, 2018
1 parent 949ef44 commit 5d8362d
Show file tree
Hide file tree
Showing 94 changed files with 6,796 additions and 1,702 deletions.
Empty file modified bin/go-petstore-withxml.sh
100644 → 100755
Empty file.
5 changes: 0 additions & 5 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
}
{{#responses}}{{#dataType}}
if localVarHttpResponse.StatusCode == {{{code}}} {
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err
}

var v {{{dataType}}}
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0-SNAPSHOT
3.0.0-SNAPSHOT
38 changes: 19 additions & 19 deletions samples/client/petstore-security-test/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ This API client was generated by the [swagger-codegen](https://github.com/swagge

- API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
- Package version: 1.0.0
- Build package: io.swagger.codegen.languages.GoClientCodegen
- Build package: org.openapitools.codegen.languages.GoClientCodegen

## Installation
Put the package under your project folder and add the following in import:
```
"./swagger"
```golang
import "./swagger"
```

## Documentation for API Endpoints

All URIs are relative to *https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*
All URIs are relative to *petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
Expand All @@ -35,12 +35,12 @@ Class | Method | HTTP request | Description
- **Type**: API key

Example
```
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
```golang
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
Key: "APIKEY",
Prefix: "Bearer", // Omit if not necessary.
})
r, err := client.Service.Operation(auth, args)
```
## petstore_auth
- **Type**: OAuth
Expand All @@ -51,20 +51,20 @@ Example
- **read:pets**: read your pets *_/ ' \" =end -- \\r\\n \\n \\r

Example
```
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
r, err := client.Service.Operation(auth, args)
```golang
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
r, err := client.Service.Operation(auth, args)
```

Or via OAuth2 module to automatically refresh tokens and perform user authentication.
```
import "golang.org/x/oauth2"
```golang
import "golang.org/x/oauth2"

/ .. Perform OAuth2 round trip request and obtain a token .. //
/* Perform OAuth2 round trip request and obtain a token */

tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
r, err := client.Service.Operation(auth, args)
tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
r, err := client.Service.Operation(auth, args)
```

## Author
Expand Down
74 changes: 74 additions & 0 deletions samples/client/petstore-security-test/go/api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
openapi: 3.0.1
info:
title: Swagger Petstore */ ' " =end -- \r\n \n \r
description: "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 -- \r\n \n \r"
termsOfService: http://swagger.io/terms/ */ ' " =end -- \r\n \n \r
contact:
email: [email protected] */ ' " =end -- \r\n \n \r
license:
name: Apache-2.0 */ ' " =end -- \r\n \n \r
url: http://www.apache.org/licenses/LICENSE-2.0.html */ ' " =end -- \r\n \n \r
version: 1.0.0 */ ' " =end -- \r\n \n \r
externalDocs:
description: Find out more about Swagger */ ' " =end -- \r\n \n \r
url: http://swagger.io
servers:
- url: petstore.swagger.io */ ' " =end -- \r\n \n \r/v2 */ ' " =end -- \r\n \n \r
tags:
- name: fake
description: Everything about your Pets */ ' " =end -- \r\n \n \r
externalDocs:
description: Find out more */ ' " =end -- \r\n \n \r
url: http://swagger.io
paths:
/fake:
put:
tags:
- fake
summary: To test code injection */ ' " =end -- \r\n \n \r
operationId: testCodeInject */ ' " =end -- \r\n \n \r
requestBody:
content:
application/json:
schema:
properties:
test code inject */ ' " =end -- \r\n \n \r:
type: string
description: To test code injection */ ' " =end -- \r\n \n \r
? "*/ ' \" =end -- \r\n \n \r"
: schema:
properties:
test code inject */ ' " =end -- \r\n \n \r:
type: string
description: To test code injection */ ' " =end -- \r\n \n \r
responses:
400:
description: To test code injection */ ' " =end -- \r\n \n \r
content: {}
components:
schemas:
Return:
type: object
properties:
return:
type: integer
description: property description */ ' " =end -- \r\n \n \r
format: int32
description: Model for testing reserved words */ ' " =end -- \r\n \n \r
xml:
name: Return
securitySchemes:
petstore_auth:
type: oauth2
flows:
implicit:
authorizationUrl: http://petstore.swagger.io/api/oauth/dialog
scopes:
write:pets: modify pets in your account */ ' " =end -- \r\n \n \r
read:pets: read your pets */ ' " =end -- \r\n \n \r
api_key:
type: apiKey
name: api_key */ ' " =end -- \r\n \n \r
in: header
110 changes: 110 additions & 0 deletions samples/client/petstore-security-test/go/api_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
*
* 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 --
*
* API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package swagger

import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"context"
)

// Linger please
var (
_ context.Context
)

type FakeApiService service

/*
FakeApiService To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
* @param optional nil or *TestCodeInjectEndRnNROpts - Optional Parameters:
* @param "" (optional.Interface of ErrorUnknown) -
*/

type TestCodeInjectEndRnNROpts struct {
optional.Interface
}

func (a *FakeApiService) TestCodeInjectEndRnNR(ctx context.Context, localVarOptionals *TestCodeInjectEndRnNROpts) (*http.Response, error) {
var (
localVarHttpMethod = strings.ToUpper("Put")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte

)

// create path and map variables
localVarPath := a.client.cfg.BasePath + "/fake"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}

// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json", "*_/ ' =end -- "}

// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
if localVarHttpContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHttpContentType
}

// to determine the Accept header
localVarHttpHeaderAccepts := []string{}

// set Accept header
localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
if localVarHttpHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
}
// body params
if localVarOptionals != nil && localVarOptionals..IsSet() {

localVarOptional, localVarOptionalok := localVarOptionals..Value().(ErrorUnknown)
if !localVarOptionalok {
return nil, reportError("uNKNOWNBASETYPE should be ErrorUnknown")
}
localVarPostBody = &localVarOptional
}
r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return nil, err
}

localVarHttpResponse, err := a.client.callAPI(r)
if err != nil || localVarHttpResponse == nil {
return localVarHttpResponse, err
}

localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
if err != nil {
return localVarHttpResponse, err
}


if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}

return localVarHttpResponse, newErr
}

return localVarHttpResponse, nil
}
Loading

0 comments on commit 5d8362d

Please sign in to comment.