Skip to content

Commit

Permalink
Merge pull request #82 from Clever/validation-fixes
Browse files Browse the repository at this point in the history
Validation fixes
  • Loading branch information
cozmo authored Oct 31, 2016
2 parents e754abe + 46e4b72 commit 731f34b
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 182 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
}
swaggerSpec := *doc.Spec()

if err := validation.Validate(swaggerSpec); err != nil {
if err := validation.Validate(*doc); err != nil {
log.Fatalf("Swagger file not valid: %s", err)
}

Expand Down
92 changes: 46 additions & 46 deletions samples/gen-go/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,35 +184,35 @@ func (c *WagClient) GetBooks(ctx context.Context, i *models.GetBooksInput) ([]mo
}
}

// GetBookByID makes a GET request to /books/{book_id}.
// Returns a book
func (c *WagClient) GetBookByID(ctx context.Context, i *models.GetBookByIDInput) (models.GetBookByIDOutput, error) {
path := c.basePath + "/v1/books/{book_id}"
// CreateBook makes a POST request to /books.
// Creates a book
func (c *WagClient) CreateBook(ctx context.Context, i *models.Book) (*models.Book, error) {
path := c.basePath + "/v1/books"
urlVals := url.Values{}
var body []byte

path = strings.Replace(path, "{book_id}", strconv.FormatInt(i.BookID, 10), -1)
if i.AuthorID != nil {
urlVals.Add("authorID", *i.AuthorID)
}
if i.RandomBytes != nil {
urlVals.Add("randomBytes", string(*i.RandomBytes))
}
path = path + "?" + urlVals.Encode()

if i != nil {

var err error
body, err = json.Marshal(i)

if err != nil {
return nil, err
}

}

client := &http.Client{Transport: c.transport}
req, err := http.NewRequest("GET", path, bytes.NewBuffer(body))
req, err := http.NewRequest("POST", path, bytes.NewBuffer(body))

if err != nil {
return nil, err
}

if i.Authorization != nil {
req.Header.Set("authorization", *i.Authorization)
}

// Add the opname for doers like tracing
ctx = context.WithValue(ctx, opNameCtx{}, "getBookByID")
ctx = context.WithValue(ctx, opNameCtx{}, "createBook")
req = req.WithContext(ctx)
// Don't add the timeout in a "doer" because we don't want to call "defer.cancel()"
// until we've finished all the processing of the request object. Otherwise we'll cancel
Expand All @@ -231,22 +231,13 @@ func (c *WagClient) GetBookByID(ctx context.Context, i *models.GetBookByIDInput)
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var output models.GetBookByID200Output
var output models.Book

if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, models.DefaultInternalError{Msg: err.Error()}
}

return &output, nil
case 204:
var output models.GetBookByID204Output
return output, nil
case 401:
var output models.GetBookByID401Output
return nil, output
case 404:
var output models.GetBookByID404Output
return nil, output
case 400:
var output models.DefaultBadRequest

Expand All @@ -269,35 +260,35 @@ func (c *WagClient) GetBookByID(ctx context.Context, i *models.GetBookByIDInput)
}
}

// CreateBook makes a POST request to /books/{book_id}.
// Creates a book
func (c *WagClient) CreateBook(ctx context.Context, i *models.Book) (*models.Book, error) {
// GetBookByID makes a GET request to /books/{book_id}.
// Returns a book
func (c *WagClient) GetBookByID(ctx context.Context, i *models.GetBookByIDInput) (models.GetBookByIDOutput, error) {
path := c.basePath + "/v1/books/{book_id}"
urlVals := url.Values{}
var body []byte

path = path + "?" + urlVals.Encode()

if i != nil {

var err error
body, err = json.Marshal(i)

if err != nil {
return nil, err
}

path = strings.Replace(path, "{book_id}", strconv.FormatInt(i.BookID, 10), -1)
if i.AuthorID != nil {
urlVals.Add("authorID", *i.AuthorID)
}
if i.RandomBytes != nil {
urlVals.Add("randomBytes", string(*i.RandomBytes))
}
path = path + "?" + urlVals.Encode()

client := &http.Client{Transport: c.transport}
req, err := http.NewRequest("POST", path, bytes.NewBuffer(body))
req, err := http.NewRequest("GET", path, bytes.NewBuffer(body))

if err != nil {
return nil, err
}

if i.Authorization != nil {
req.Header.Set("authorization", *i.Authorization)
}

// Add the opname for doers like tracing
ctx = context.WithValue(ctx, opNameCtx{}, "createBook")
ctx = context.WithValue(ctx, opNameCtx{}, "getBookByID")
req = req.WithContext(ctx)
// Don't add the timeout in a "doer" because we don't want to call "defer.cancel()"
// until we've finished all the processing of the request object. Otherwise we'll cancel
Expand All @@ -316,13 +307,22 @@ func (c *WagClient) CreateBook(ctx context.Context, i *models.Book) (*models.Boo
defer resp.Body.Close()
switch resp.StatusCode {
case 200:
var output models.Book
var output models.GetBookByID200Output

if err := json.NewDecoder(resp.Body).Decode(&output); err != nil {
return nil, models.DefaultInternalError{Msg: err.Error()}
}

return &output, nil
case 204:
var output models.GetBookByID204Output
return output, nil
case 401:
var output models.GetBookByID401Output
return nil, output
case 404:
var output models.GetBookByID404Output
return nil, output
case 400:
var output models.DefaultBadRequest

Expand All @@ -345,10 +345,10 @@ func (c *WagClient) CreateBook(ctx context.Context, i *models.Book) (*models.Boo
}
}

// GetBookByID2 makes a GET request to /books/{id}.
// GetBookByID2 makes a GET request to /books2/{id}.
// Retrieve a book
func (c *WagClient) GetBookByID2(ctx context.Context, i *models.GetBookByID2Input) (*models.Book, error) {
path := c.basePath + "/v1/books/{id}"
path := c.basePath + "/v1/books2/{id}"
urlVals := url.Values{}
var body []byte

Expand Down
10 changes: 5 additions & 5 deletions samples/gen-go/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ type Client interface {
// Returns a list of books
GetBooks(ctx context.Context, i *models.GetBooksInput) ([]models.Book, error)

// CreateBook makes a POST request to /books.
// Creates a book
CreateBook(ctx context.Context, i *models.Book) (*models.Book, error)

// GetBookByID makes a GET request to /books/{book_id}.
// Returns a book
GetBookByID(ctx context.Context, i *models.GetBookByIDInput) (models.GetBookByIDOutput, error)

// CreateBook makes a POST request to /books/{book_id}.
// Creates a book
CreateBook(ctx context.Context, i *models.Book) (*models.Book, error)

// GetBookByID2 makes a GET request to /books/{id}.
// GetBookByID2 makes a GET request to /books2/{id}.
// Retrieve a book
GetBookByID2(ctx context.Context, i *models.GetBookByID2Input) (*models.Book, error)

Expand Down
20 changes: 10 additions & 10 deletions samples/gen-go/client/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ func (_mr *_MockClientRecorder) GetBooks(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetBooks", arg0, arg1)
}

func (_m *MockClient) GetBookByID(ctx context.Context, i *models.GetBookByIDInput) (models.GetBookByIDOutput, error) {
ret := _m.ctrl.Call(_m, "GetBookByID", ctx, i)
ret0, _ := ret[0].(models.GetBookByIDOutput)
func (_m *MockClient) CreateBook(ctx context.Context, i *models.Book) (*models.Book, error) {
ret := _m.ctrl.Call(_m, "CreateBook", ctx, i)
ret0, _ := ret[0].(*models.Book)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockClientRecorder) GetBookByID(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetBookByID", arg0, arg1)
func (_mr *_MockClientRecorder) CreateBook(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "CreateBook", arg0, arg1)
}

func (_m *MockClient) CreateBook(ctx context.Context, i *models.Book) (*models.Book, error) {
ret := _m.ctrl.Call(_m, "CreateBook", ctx, i)
ret0, _ := ret[0].(*models.Book)
func (_m *MockClient) GetBookByID(ctx context.Context, i *models.GetBookByIDInput) (models.GetBookByIDOutput, error) {
ret := _m.ctrl.Call(_m, "GetBookByID", ctx, i)
ret0, _ := ret[0].(models.GetBookByIDOutput)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockClientRecorder) CreateBook(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "CreateBook", arg0, arg1)
func (_mr *_MockClientRecorder) GetBookByID(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetBookByID", arg0, arg1)
}

func (_m *MockClient) GetBookByID2(ctx context.Context, i *models.GetBookByID2Input) (*models.Book, error) {
Expand Down
Loading

0 comments on commit 731f34b

Please sign in to comment.