gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 577 forks source link

render.Options#DefaultContentType is not working #1335

Closed cippaciong closed 6 years ago

cippaciong commented 6 years ago

Steps to Reproduce the Problem

I created a new api-only buffalo project using version v.0.13.0-beta.2. (the same problem occurred migrating an existing project from 0.12.6)

buffalo new json_bug --api --with-dep
cd json_bug
buffalo g resource --skip-templates accounts username password
buffalo pop create
buffalo pop migrate
buffalo dev

Once the project was created I wanted to test the new render.Options#DefaultContentType which is enabled by default if using the --api flag since v0.13.0. (I added it manually in the project migrated from 0.12.6) In both cases I made the requests using both httpie and curl, the result was the same.

http localhost:3000/accounts
curl localhost:3000/account

Expected Behavior

A correct response in json (in this case an empty list since I didn't create any account)

Actual Behavior

I get the same error we used to have prior to render.Options#DefaultContentType was in place

HTTP/1.1 500 Internal Server Error
Content-Type: application/json
Date: Fri, 28 Sep 2018 09:56:19 GMT
Transfer-Encoding: chunked
Vary: Origin

{
    "code": 500,
    "error": "stat accounts/index.html: no such file or directory\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/pop.(*Connection).Transaction.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/pop/connection.go:131\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/pop.(*postgresql).Lock\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/pop/dialect_postgresql.go:165\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/pop.(*Connection).Transaction\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/pop/connection.go:118\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo-pop/pop/popmw.Transaction.func2.1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo-pop/pop/popmw/tx.go:37\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/mw-contenttype.Set.func1.1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/mw-contenttype/content_type.go:11\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/mw-paramlogger.parameterLogger.Middleware.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/mw-paramlogger/param_logger.go:63\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/mw-forcessl.Middleware.func1.1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/mw-forcessl/forcessl.go:26\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.sessionSaver.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/session.go:70\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.RequestLoggerFunc.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/request_logger.go:54\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*App).PanicHandler.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/errors.go:81\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*App).defaultErrorMiddleware.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/errors.go:87\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.RouteInfo.ServeHTTP\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/route_info.go:108\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*RouteInfo).ServeHTTP\n\t<autogenerated>:1\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gorilla/mux/mux.go:162\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/markbates/refresh/refresh/web.ErrorChecker.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/markbates/refresh/refresh/web/web.go:23\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/lib/go/src/net/http/server.go:1964\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*App).ServeHTTP\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/server.go:127\nnet/http.serverHandler.ServeHTTP\n\t/usr/lib/go/src/net/http/server.go:2741\nnet/http.(*conn).serve\n\t/usr/lib/go/src/net/http/server.go:1847\nruntime.goexit\n\t/usr/lib/go/src/runtime/asm_amd64.s:1333\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.sessionSaver.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/session.go:72\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.RequestLoggerFunc.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/request_logger.go:54\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*App).PanicHandler.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/errors.go:81\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*App).defaultErrorMiddleware.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/errors.go:87\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.RouteInfo.ServeHTTP\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/route_info.go:108\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*RouteInfo).ServeHTTP\n\t<autogenerated>:1\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gorilla/mux/mux.go:162\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/markbates/refresh/refresh/web.ErrorChecker.func1\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/markbates/refresh/refresh/web/web.go:23\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/lib/go/src/net/http/server.go:1964\ngithub.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo.(*App).ServeHTTP\n\t/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/vendor/github.com/gobuffalo/buffalo/server.go:127\nnet/http.serverHandler.ServeHTTP\n\t/usr/lib/go/src/net/http/server.go:2741\nnet/http.(*conn).serve\n\t/usr/lib/go/src/net/http/server.go:1847\nruntime.goexit\n\t/usr/lib/go/src/runtime/asm_amd64.s:1333"
}

This is the output I see from the console where buffalo dev is running

DEBU[2018-09-28T11:56:19+02:00] SELECT accounts.created_at, accounts.id, accounts.password, accounts.updated_at, accounts.username FROM accounts AS accounts LIMIT 20 OFFSET 0
DEBU[2018-09-28T11:56:19+02:00] SELECT COUNT(*) AS row_count FROM (SELECT accounts.created_at, accounts.id, accounts.password, accounts.updated_at, accounts.username FROM accounts AS accounts) a
INFO[2018-09-28T11:56:19+02:00] /accounts/ content_type=application/json db=2.079848ms duration=5.388324ms human_size="0 B" method=GET params="{}" path=/accounts/ render="178.278µs" request_id=fMkuJmWMhE-dfUTVmqkqs size=0 status=0
ERRO[2018-09-28T11:56:19+02:00] stat accounts/index.html: no such file or directory content_type=application/json db=2.079848ms duration=5.388324ms human_size="0 B" method=GET params="{}" path=/accounts/ render="178.278µs" request_id=fMkuJmWMhE-dfUTVmqkqs size=0 status=0

Info

``` [POP] 2018/09/28 12:04:25 info - Loading config file from /home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug/database.yml ### Buffalo Version v0.13.0-beta.2 ### App Information Pwd=/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug Root=/home/cippaciong/.go/src/github.com/middleware2018-PSS/json_bug GoPath=/home/cippaciong/.go Name=json_bug Bin=bin/json_bug PackagePkg=github.com/middleware2018-PSS/json_bug ActionsPkg=github.com/middleware2018-PSS/json_bug/actions ModelsPkg=github.com/middleware2018-PSS/json_bug/models GriftsPkg=github.com/middleware2018-PSS/json_bug/grifts VCS=git WithPop=true WithSQLite=false WithDep=true WithWebpack=false WithYarn=false WithDocker=true WithGrifts=true WithModules=false ### Go Version go version go1.11 linux/amd64 ### Go Env GOARCH="amd64" GOBIN="" GOCACHE="/home/cippaciong/.cache/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/cippaciong/.go" GOPROXY="" GORACE="" GOROOT="/usr/lib/go" GOTMPDIR="" GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build031662131=/tmp/go-build -gno-record-gcc-switches" ### Node Version v10.11.0 ### NPM Version 6.4.1 ### Yarn Version 1.9.4 ### PostgreSQL Version PostgreSQL Not Found ### MySQL Version MySQL Not Found ### SQLite Version 3.25.1 2018-09-18 20:20:44 2ac9003de44da7dafa3fbb1915ac5725a9275c86bf2f3b7aa19321bf1460b386 ### Dep Version dep: version : devel build date : git hash : go version : go1.10.3 go compiler : gc platform : linux/amd64 features : ImportDuringSolve=false ### Dep Status Gopkg.lock is out of sync with imports and/or Gopkg.toml. Run `dep check` for details. PROJECT MISSING PACKAGES input-digest mismatch Usage: buffalo info [flags] Flags: -h, --help help for info ERRO[0003] Error: exit status 1 ```

Additional Notes

If I understood correctly, the way render.Options#DefaultContentType works is by checking from Context the the request contenType is empty. In my case, when I try to print that value from Context upon receiving a request I get */* and not an empty string. Checking the request header from httpie that value is not explicitly set for contenType but is used for Accept instead

$ http localhost:3000/accounts -p H # This prints the request header
GET /accounts HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:3000
User-Agent: HTTPie/0.9.8

If I explicitly set the content type running http --json localhost:3000/accounts I get the correct response

$ http --json localhost:3000/accounts
HTTP/1.1 200 OK
Content-Length: 3
Content-Type: application/json
Date: Fri, 28 Sep 2018 10:13:06 GMT
Vary: Origin
X-Pagination: {"page":1,"per_page":20,"offset":0,"total_entries_size":0,"current_entries_size":0,"total_pages":0}

[]
cippaciong commented 6 years ago

Thanks :)