manyminds / api2go

JSONAPI.org Implementation for Go
MIT License
700 stars 94 forks source link

Server panic when query page[size]=0 #305

Open imhuytq opened 7 years ago

imhuytq commented 7 years ago

When calling PaginatedFindAll with query page[size]=0, the server returns the following error:

http: panic serving 172.24.0.1:41332: runtime error: integer divide by zero
goroutine 260 [running]:
net/http.(*conn).serve.func1(0xc4200f3ae0)
    /usr/local/go/src/net/http/server.go:1721 +0xd0
panic(0x7f4540, 0xa619c0)
    /usr/local/go/src/runtime/panic.go:489 +0x2cf
github.com/imhuytq/a/vendor/github.com/manyminds/api2go.paginationQueryParams.getLinks(0xc42015ef1b, 0x1, 0xc42015ef28, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc42000b100, 0x2, ...)
    /go/src/github.com/imhuytq/a/vendor/github.com/manyminds/api2go/api.go:110 +0xec1
github.com/imhuytq/a/vendor/github.com/manyminds/api2go.(*resource).handleIndex(0xc4203627c0, 0xa40460, 0xc42000e1d0, 0xa3e480, 0xc420462c40, 0xc42000b100, 0x85e51e, 0x1, 0xa390c0, 0xc4203515f0, ...)
    /go/src/github.com/imhuytq/a/vendor/github.com/manyminds/api2go/api.go:479 +0x567
github.com/imhuytq/a/vendor/github.com/manyminds/api2go.(*API).addResource.func3(0xa3e480, 0xc420462c40, 0xc42000b100, 0xc4207e10b0)
    /go/src/github.com/imhuytq/a/vendor/github.com/manyminds/api2go/api.go:262 +0x198
github.com/imhuytq/a/vendor/github.com/manyminds/api2go/routing.HTTPRouter.Handle.func1(0xa3e480, 0xc420462c40, 0xc42000b100, 0x0, 0x0, 0x0)
    /go/src/github.com/imhuytq/a/vendor/github.com/manyminds/api2go/routing/httprouter.go:22 +0x151
github.com/imhuytq/a/vendor/github.com/julienschmidt/httprouter.(*Router).ServeHTTP(0xc420362540, 0xa3e480, 0xc420462c40, 0xc42000b100)
    /go/src/github.com/imhuytq/a/vendor/github.com/julienschmidt/httprouter/router.go:344 +0x813
net/http.serverHandler.ServeHTTP(0xc42009b8c0, 0xa3e480, 0xc420462c40, 0xc42000b100)
    /usr/local/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc4200f3ae0, 0xa3eac0, 0xc42015ee40)
    /usr/local/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:2668 +0x2ce

I think PaginatedFindAll should return the defaultSize. When query page[size]=0 will assign size = defaultSize. Another problem is that when I want to limit the size (eg max = 10), what should I do?

sharpner commented 7 years ago

Oh that is not supposed to happen. Thanks for reporting! Instead of implicitly assuming that zero should be the defaultSize, I'd rather handle invalid cases with a bad request. If you'd like or it's urgent you you open a PR with a test, but if not, we will take a look at it :)

imhuytq commented 7 years ago

I validated the page[size] parameter in the PaginatedFindAll function and returned a BadRequest error message if it was zero. This problem is not very serious, but you should check back later. I think the way to solve this problem may be related to #306.