Skip to content

Commit f52f6c6

Browse files
committed
Merge pull request #2542 from wing328/restore_petstore_yaml
[Petstore] restore petstore.yaml from http://petstore.swagger.io/v2/swagger.json
2 parents 5bc4570 + 6f7e3c7 commit f52f6c6

File tree

10 files changed

+513
-363
lines changed

10 files changed

+513
-363
lines changed

modules/swagger-codegen/src/test/resources/2_0/petstore.yaml

Lines changed: 322 additions & 203 deletions
Large diffs are not rendered by default.

samples/client/petstore/go/swagger/Category.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ import (
55

66
type Category struct {
77
Id int64 `json:"id,omitempty"`
8-
Name string `json:"name,omitempty"`
9-
8+
Name string `json:"name,omitempty"`
109
}

samples/client/petstore/go/swagger/Order.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66

77
type Order struct {
88
Id int64 `json:"id,omitempty"`
9-
PetId int64 `json:"petId,omitempty"`
10-
Quantity int32 `json:"quantity,omitempty"`
11-
ShipDate time.Time `json:"shipDate,omitempty"`
12-
Status string `json:"status,omitempty"`
13-
Complete bool `json:"complete,omitempty"`
14-
9+
PetId int64 `json:"petId,omitempty"`
10+
Quantity int32 `json:"quantity,omitempty"`
11+
ShipDate time.Time `json:"shipDate,omitempty"`
12+
Status string `json:"status,omitempty"`
13+
Complete bool `json:"complete,omitempty"`
1514
}

samples/client/petstore/go/swagger/Pet.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55

66
type Pet struct {
77
Id int64 `json:"id,omitempty"`
8-
Category Category `json:"category,omitempty"`
9-
Name string `json:"name,omitempty"`
10-
PhotoUrls []string `json:"photoUrls,omitempty"`
11-
Tags []Tag `json:"tags,omitempty"`
12-
Status string `json:"status,omitempty"`
13-
8+
Category Category `json:"category,omitempty"`
9+
Name string `json:"name,omitempty"`
10+
PhotoUrls []string `json:"photoUrls,omitempty"`
11+
Tags []Tag `json:"tags,omitempty"`
12+
Status string `json:"status,omitempty"`
1413
}

samples/client/petstore/go/swagger/PetApi.go

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package swagger
22

3-
43
import (
54
"strings"
65
"fmt"
76
"encoding/json"
87
"errors"
98
"github.com/dghubble/sling"
10-
9+
"os"
1110
)
1211

1312
type PetApi struct {
@@ -30,7 +29,6 @@ func NewPetApiWithBasePath(basePath string) *PetApi{
3029
}
3130
}
3231

33-
3432
/**
3533
* Add a new pet to the store
3634
*
@@ -43,20 +41,17 @@ func (a PetApi) AddPet (body Pet) (error) {
4341
_sling := sling.New().Post(a.Configuration.BasePath)
4442

4543
// create path and map variables
46-
path := "/v2/pets"
47-
44+
path := "/v2/pet"
4845

4946
_sling = _sling.Path(path)
5047

51-
5248
// accept header
53-
accepts := []string { "application/json", "application/xml" }
49+
accepts := []string { "application/xml", "application/json" }
5450
for key := range accepts {
5551
_sling = _sling.Set("Accept", accepts[key])
5652
break // only use the first Accept
5753
}
5854

59-
6055
// body params
6156
_sling = _sling.BodyJSON(body)
6257

@@ -94,29 +89,26 @@ func (a PetApi) AddPet (body Pet) (error) {
9489

9590
return err
9691
}
97-
9892
/**
9993
* Deletes a pet
10094
*
101-
* @param apiKey
10295
* @param petId Pet id to delete
96+
* @param apiKey
10397
* @return void
10498
*/
105-
//func (a PetApi) DeletePet (apiKey string, petId int64) (error) {
106-
func (a PetApi) DeletePet (apiKey string, petId int64) (error) {
99+
//func (a PetApi) DeletePet (petId int64, apiKey string) (error) {
100+
func (a PetApi) DeletePet (petId int64, apiKey string) (error) {
107101

108102
_sling := sling.New().Delete(a.Configuration.BasePath)
109103

110104
// create path and map variables
111-
path := "/v2/pets/{petId}"
105+
path := "/v2/pet/{petId}"
112106
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
113107

114-
115108
_sling = _sling.Path(path)
116109

117-
118110
// accept header
119-
accepts := []string { "application/json", "application/xml" }
111+
accepts := []string { "application/xml", "application/json" }
120112
for key := range accepts {
121113
_sling = _sling.Set("Accept", accepts[key])
122114
break // only use the first Accept
@@ -127,7 +119,6 @@ func (a PetApi) DeletePet (apiKey string, petId int64) (error) {
127119

128120

129121

130-
131122
// We use this map (below) so that any arbitrary error JSON can be handled.
132123
// FIXME: This is in the absence of this Go generator honoring the non-2xx
133124
// response (error) models, which needs to be implemented at some point.
@@ -160,10 +151,9 @@ func (a PetApi) DeletePet (apiKey string, petId int64) (error) {
160151

161152
return err
162153
}
163-
164154
/**
165155
* Finds Pets by status
166-
* Multiple status values can be provided with comma seperated strings
156+
* Multiple status values can be provided with comma separated strings
167157
* @param status Status values that need to be considered for filter
168158
* @return []Pet
169159
*/
@@ -173,26 +163,22 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) {
173163
_sling := sling.New().Get(a.Configuration.BasePath)
174164

175165
// create path and map variables
176-
path := "/v2/pets/findByStatus"
177-
166+
path := "/v2/pet/findByStatus"
178167

179168
_sling = _sling.Path(path)
180169

181170
type QueryParams struct {
182171
status []string `url:"status,omitempty"`
183-
184172
}
185173
_sling = _sling.QueryStruct(&QueryParams{ status: status })
186-
187174
// accept header
188-
accepts := []string { "application/json", "application/xml" }
175+
accepts := []string { "application/xml", "application/json" }
189176
for key := range accepts {
190177
_sling = _sling.Set("Accept", accepts[key])
191178
break // only use the first Accept
192179
}
193180

194181

195-
196182
var successPayload = new([]Pet)
197183

198184
// We use this map (below) so that any arbitrary error JSON can be handled.
@@ -227,10 +213,9 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) {
227213

228214
return *successPayload, err
229215
}
230-
231216
/**
232217
* Finds Pets by tags
233-
* Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
218+
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
234219
* @param tags Tags to filter by
235220
* @return []Pet
236221
*/
@@ -240,26 +225,22 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) {
240225
_sling := sling.New().Get(a.Configuration.BasePath)
241226

242227
// create path and map variables
243-
path := "/v2/pets/findByTags"
244-
228+
path := "/v2/pet/findByTags"
245229

246230
_sling = _sling.Path(path)
247231

248232
type QueryParams struct {
249233
tags []string `url:"tags,omitempty"`
250-
251234
}
252235
_sling = _sling.QueryStruct(&QueryParams{ tags: tags })
253-
254236
// accept header
255-
accepts := []string { "application/json", "application/xml" }
237+
accepts := []string { "application/xml", "application/json" }
256238
for key := range accepts {
257239
_sling = _sling.Set("Accept", accepts[key])
258240
break // only use the first Accept
259241
}
260242

261243

262-
263244
var successPayload = new([]Pet)
264245

265246
// We use this map (below) so that any arbitrary error JSON can be handled.
@@ -294,11 +275,10 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) {
294275

295276
return *successPayload, err
296277
}
297-
298278
/**
299279
* Find pet by ID
300-
* Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
301-
* @param petId ID of pet that needs to be fetched
280+
* Returns a single pet
281+
* @param petId ID of pet to return
302282
* @return Pet
303283
*/
304284
//func (a PetApi) GetPetById (petId int64) (Pet, error) {
@@ -307,22 +287,19 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) {
307287
_sling := sling.New().Get(a.Configuration.BasePath)
308288

309289
// create path and map variables
310-
path := "/v2/pets/{petId}"
290+
path := "/v2/pet/{petId}"
311291
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
312292

313-
314293
_sling = _sling.Path(path)
315294

316-
317295
// accept header
318-
accepts := []string { "application/json", "application/xml" }
296+
accepts := []string { "application/xml", "application/json" }
319297
for key := range accepts {
320298
_sling = _sling.Set("Accept", accepts[key])
321299
break // only use the first Accept
322300
}
323301

324302

325-
326303
var successPayload = new(Pet)
327304

328305
// We use this map (below) so that any arbitrary error JSON can be handled.
@@ -357,7 +334,6 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) {
357334

358335
return *successPayload, err
359336
}
360-
361337
/**
362338
* Update an existing pet
363339
*
@@ -370,20 +346,17 @@ func (a PetApi) UpdatePet (body Pet) (error) {
370346
_sling := sling.New().Put(a.Configuration.BasePath)
371347

372348
// create path and map variables
373-
path := "/v2/pets"
374-
349+
path := "/v2/pet"
375350

376351
_sling = _sling.Path(path)
377352

378-
379353
// accept header
380-
accepts := []string { "application/json", "application/xml" }
354+
accepts := []string { "application/xml", "application/json" }
381355
for key := range accepts {
382356
_sling = _sling.Set("Accept", accepts[key])
383357
break // only use the first Accept
384358
}
385359

386-
387360
// body params
388361
_sling = _sling.BodyJSON(body)
389362

@@ -421,7 +394,6 @@ func (a PetApi) UpdatePet (body Pet) (error) {
421394

422395
return err
423396
}
424-
425397
/**
426398
* Updates a pet in the store with form data
427399
*
@@ -430,21 +402,19 @@ func (a PetApi) UpdatePet (body Pet) (error) {
430402
* @param status Updated status of the pet
431403
* @return void
432404
*/
433-
//func (a PetApi) UpdatePetWithForm (petId string, name string, status string) (error) {
434-
func (a PetApi) UpdatePetWithForm (petId string, name string, status string) (error) {
405+
//func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (error) {
406+
func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (error) {
435407

436408
_sling := sling.New().Post(a.Configuration.BasePath)
437409

438410
// create path and map variables
439-
path := "/v2/pets/{petId}"
411+
path := "/v2/pet/{petId}"
440412
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
441413

442-
443414
_sling = _sling.Path(path)
444415

445-
446416
// accept header
447-
accepts := []string { "application/json", "application/xml" }
417+
accepts := []string { "application/xml", "application/json" }
448418
for key := range accepts {
449419
_sling = _sling.Set("Accept", accepts[key])
450420
break // only use the first Accept
@@ -453,13 +423,11 @@ func (a PetApi) UpdatePetWithForm (petId string, name string, status string) (er
453423
type FormParams struct {
454424
name string `url:"name,omitempty"`
455425
status string `url:"status,omitempty"`
456-
457426
}
458427
_sling = _sling.BodyForm(&FormParams{ name: name,status: status })
459428

460429

461430

462-
463431
// We use this map (below) so that any arbitrary error JSON can be handled.
464432
// FIXME: This is in the absence of this Go generator honoring the non-2xx
465433
// response (error) models, which needs to be implemented at some point.
@@ -492,5 +460,69 @@ func (a PetApi) UpdatePetWithForm (petId string, name string, status string) (er
492460

493461
return err
494462
}
463+
/**
464+
* uploads an image
465+
*
466+
* @param petId ID of pet to update
467+
* @param additionalMetadata Additional data to pass to server
468+
* @param file file to upload
469+
* @return ApiResponse
470+
*/
471+
//func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.File) (ApiResponse, error) {
472+
func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.File) (ApiResponse, error) {
473+
474+
_sling := sling.New().Post(a.Configuration.BasePath)
475+
476+
// create path and map variables
477+
path := "/v2/pet/{petId}/uploadImage"
478+
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
479+
480+
_sling = _sling.Path(path)
481+
482+
// accept header
483+
accepts := []string { "application/json" }
484+
for key := range accepts {
485+
_sling = _sling.Set("Accept", accepts[key])
486+
break // only use the first Accept
487+
}
488+
489+
type FormParams struct {
490+
additionalMetadata string `url:"additionalMetadata,omitempty"`
491+
file *os.File `url:"file,omitempty"`
492+
}
493+
_sling = _sling.BodyForm(&FormParams{ additionalMetadata: additionalMetadata,file: file })
494+
495+
var successPayload = new(ApiResponse)
495496

497+
// We use this map (below) so that any arbitrary error JSON can be handled.
498+
// FIXME: This is in the absence of this Go generator honoring the non-2xx
499+
// response (error) models, which needs to be implemented at some point.
500+
var failurePayload map[string]interface{}
501+
502+
httpResponse, err := _sling.Receive(successPayload, &failurePayload)
503+
504+
if err == nil {
505+
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
506+
if failurePayload != nil {
507+
// If the failurePayload is present, there likely was some kind of non-2xx status
508+
// returned (and a JSON payload error present)
509+
var str []byte
510+
str, err = json.Marshal(failurePayload)
511+
if err == nil { // For safety, check for an error marshalling... probably superfluous
512+
// This will return the JSON error body as a string
513+
err = errors.New(string(str))
514+
}
515+
} else {
516+
// So, there was no network-type error, and nothing in the failure payload,
517+
// but we should still check the status code
518+
if httpResponse == nil {
519+
// This should never happen...
520+
err = errors.New("No HTTP Response received.")
521+
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
522+
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
523+
}
524+
}
525+
}
496526

527+
return *successPayload, err
528+
}

0 commit comments

Comments
 (0)