Skip to content

Commit 8fa54a9

Browse files
antihaxwing328
authored andcommitted
Fix read on closed body. (#8057)
1 parent f9ae84d commit 8fa54a9

27 files changed

+855
-629
lines changed

bin/go-petstore-withxml.sh

100644100755
File mode changed.

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,6 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
244244
}
245245
{{#responses}}{{#dataType}}
246246
if localVarHttpResponse.StatusCode == {{{code}}} {
247-
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
248-
if err != nil {
249-
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHttpResponse, err
250-
}
251-
252247
var v {{{dataType}}}
253248
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
254249
if err != nil {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
2222
Class | Method | HTTP request | Description
2323
------------ | ------------- | ------------- | -------------
2424
*AnotherFakeApi* | [**TestSpecialTags**](docs/AnotherFakeApi.md#testspecialtags) | **Patch** /another-fake/dummy | To test special tags
25-
*DefaultApi* | [**TestBodyWithQueryParams**](docs/DefaultApi.md#testbodywithqueryparams) | **Put** /fake/body-with-query-params |
2625
*FakeApi* | [**FakeOuterBooleanSerialize**](docs/FakeApi.md#fakeouterbooleanserialize) | **Post** /fake/outer/boolean |
2726
*FakeApi* | [**FakeOuterCompositeSerialize**](docs/FakeApi.md#fakeoutercompositeserialize) | **Post** /fake/outer/composite |
2827
*FakeApi* | [**FakeOuterNumberSerialize**](docs/FakeApi.md#fakeouternumberserialize) | **Post** /fake/outer/number |
2928
*FakeApi* | [**FakeOuterStringSerialize**](docs/FakeApi.md#fakeouterstringserialize) | **Post** /fake/outer/string |
29+
*FakeApi* | [**TestBodyWithQueryParams**](docs/FakeApi.md#testbodywithqueryparams) | **Put** /fake/body-with-query-params |
3030
*FakeApi* | [**TestClientModel**](docs/FakeApi.md#testclientmodel) | **Patch** /fake | To test \"client\" model
3131
*FakeApi* | [**TestEndpointParameters**](docs/FakeApi.md#testendpointparameters) | **Post** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
3232
*FakeApi* | [**TestEnumParameters**](docs/FakeApi.md#testenumparameters) | **Get** /fake | To test enum parameters

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,14 @@ paths:
216216
required: false
217217
type: "string"
218218
x-exportParamName: "Name"
219+
x-optionalDataType: "String"
219220
- name: "status"
220221
in: "formData"
221222
description: "Updated status of the pet"
222223
required: false
223224
type: "string"
224225
x-exportParamName: "Status"
226+
x-optionalDataType: "String"
225227
responses:
226228
405:
227229
description: "Invalid input"
@@ -244,6 +246,7 @@ paths:
244246
required: false
245247
type: "string"
246248
x-exportParamName: "ApiKey"
249+
x-optionalDataType: "String"
247250
- name: "petId"
248251
in: "path"
249252
description: "Pet id to delete"
@@ -283,6 +286,7 @@ paths:
283286
required: false
284287
type: "string"
285288
x-exportParamName: "AdditionalMetadata"
289+
x-optionalDataType: "String"
286290
- name: "file"
287291
in: "formData"
288292
description: "file to upload"
@@ -651,6 +655,7 @@ paths:
651655
- "-efg"
652656
- "(xyz)"
653657
x-exportParamName: "EnumFormString"
658+
x-optionalDataType: "String"
654659
- name: "enum_header_string_array"
655660
in: "header"
656661
description: "Header parameter enum test (string array)"
@@ -674,6 +679,7 @@ paths:
674679
- "-efg"
675680
- "(xyz)"
676681
x-exportParamName: "EnumHeaderString"
682+
x-optionalDataType: "String"
677683
- name: "enum_query_string_array"
678684
in: "query"
679685
description: "Query parameter enum test (string array)"
@@ -697,6 +703,7 @@ paths:
697703
- "-efg"
698704
- "(xyz)"
699705
x-exportParamName: "EnumQueryString"
706+
x-optionalDataType: "String"
700707
- name: "enum_query_integer"
701708
in: "query"
702709
description: "Query parameter enum test (double)"
@@ -707,6 +714,7 @@ paths:
707714
- 1
708715
- -2
709716
x-exportParamName: "EnumQueryInteger"
717+
x-optionalDataType: "Int32"
710718
- name: "enum_query_double"
711719
in: "formData"
712720
description: "Query parameter enum test (double)"
@@ -717,6 +725,7 @@ paths:
717725
- 1.1
718726
- -1.2
719727
x-exportParamName: "EnumQueryDouble"
728+
x-optionalDataType: "Float64"
720729
responses:
721730
400:
722731
description: "Invalid request"
@@ -745,6 +754,7 @@ paths:
745754
maximum: 100
746755
minimum: 10
747756
x-exportParamName: "Integer"
757+
x-optionalDataType: "Int32"
748758
- name: "int32"
749759
in: "formData"
750760
description: "None"
@@ -754,13 +764,15 @@ paths:
754764
minimum: 20
755765
format: "int32"
756766
x-exportParamName: "Int32_"
767+
x-optionalDataType: "Int32"
757768
- name: "int64"
758769
in: "formData"
759770
description: "None"
760771
required: false
761772
type: "integer"
762773
format: "int64"
763774
x-exportParamName: "Int64_"
775+
x-optionalDataType: "Int64"
764776
- name: "number"
765777
in: "formData"
766778
description: "None"
@@ -777,6 +789,7 @@ paths:
777789
maximum: 987.6
778790
format: "float"
779791
x-exportParamName: "Float"
792+
x-optionalDataType: "Float32"
780793
- name: "double"
781794
in: "formData"
782795
description: "None"
@@ -793,6 +806,7 @@ paths:
793806
type: "string"
794807
pattern: "/[a-z]/i"
795808
x-exportParamName: "String_"
809+
x-optionalDataType: "String"
796810
- name: "pattern_without_delimiter"
797811
in: "formData"
798812
description: "None"
@@ -814,20 +828,23 @@ paths:
814828
type: "string"
815829
format: "binary"
816830
x-exportParamName: "Binary"
831+
x-optionalDataType: "String"
817832
- name: "date"
818833
in: "formData"
819834
description: "None"
820835
required: false
821836
type: "string"
822837
format: "date"
823838
x-exportParamName: "Date"
839+
x-optionalDataType: "String"
824840
- name: "dateTime"
825841
in: "formData"
826842
description: "None"
827843
required: false
828844
type: "string"
829845
format: "date-time"
830846
x-exportParamName: "DateTime"
847+
x-optionalDataType: "Time"
831848
- name: "password"
832849
in: "formData"
833850
description: "None"
@@ -837,12 +854,14 @@ paths:
837854
minLength: 10
838855
format: "password"
839856
x-exportParamName: "Password"
857+
x-optionalDataType: "String"
840858
- name: "callback"
841859
in: "formData"
842860
description: "None"
843861
required: false
844862
type: "string"
845863
x-exportParamName: "Callback"
864+
x-optionalDataType: "String"
846865
responses:
847866
400:
848867
description: "Invalid username supplied"
@@ -998,6 +1017,8 @@ paths:
9981017
description: "successful operation"
9991018
/fake/body-with-query-params:
10001019
put:
1020+
tags:
1021+
- "fake"
10011022
operationId: "testBodyWithQueryParams"
10021023
consumes:
10031024
- "application/json"

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"net/http"
1616
"net/url"
1717
"strings"
18-
"golang.org/x/net/context"
18+
"context"
1919
)
2020

2121
// Linger please
@@ -25,11 +25,14 @@ var (
2525

2626
type AnotherFakeApiService service
2727

28-
/* AnotherFakeApiService To test special tags
28+
/*
29+
AnotherFakeApiService To test special tags
2930
To test special tags
30-
* @param ctx context.Context for authentication, logging, tracing, etc.
31-
@param body client model
32-
@return Client*/
31+
* @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
32+
* @param body client model
33+
34+
@return Client
35+
*/
3336
func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, body Client) (Client, *http.Response, error) {
3437
var (
3538
localVarHttpMethod = strings.ToUpper("Patch")
@@ -76,6 +79,7 @@ func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, body Client
7679
}
7780

7881
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
82+
localVarHttpResponse.Body.Close()
7983
if err != nil {
8084
return localVarReturnValue, localVarHttpResponse, err
8185
}
@@ -95,11 +99,6 @@ func (a *AnotherFakeApiService) TestSpecialTags(ctx context.Context, body Client
9599
}
96100

97101
if localVarHttpResponse.StatusCode == 200 {
98-
localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
99-
if err != nil {
100-
return localVarReturnValue, localVarHttpResponse, err
101-
}
102-
103102
var v Client
104103
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"));
105104
if err != nil {

0 commit comments

Comments
 (0)