Skip to content

Commit

Permalink
Delay router creation
Browse files Browse the repository at this point in the history
fix npe when optional auth
  • Loading branch information
casualjim committed Apr 6, 2015
1 parent 937f8b8 commit fcac9d2
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 38 deletions.
38 changes: 19 additions & 19 deletions devtools.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/bin/sh

go get -u github.com/golang/lint/golint
go get -u golang.org/x/tools/cmd/...
go get -u github.com/tools/godep
go get -u github.com/jteeuwen/go-bindata/...
go get -u github.com/elazarl/go-bindata-assetfs/...
go get -u github.com/redefiance/go-find-references
go get -u github.com/sqs/goreturns
go get -u code.google.com/p/gomock/gomock
go get -u code.google.com/p/gomock/mockgen
go get -u github.com/axw/gocov/gocov
go get -u gopkg.in/matm/v1/gocov-html
go get -u github.com/AlekSi/gocov-xml
go get -u github.com/nsf/gocode
go get -u github.com/kisielk/errcheck
go get -u github.com/jstemmer/gotags
go get -u github.com/smartystreets/goconvey
go get -u github.com/rogpeppe/godef
go get -u github.com/pquerna/ffjson
go get -u github.com/clipperhouse/gen
go get -u -v github.com/golang/lint/golint
go get -u -v golang.org/x/tools/cmd/...
go get -u -v github.com/tools/godep
go get -u -v github.com/jteeuwen/go-bindata/...
go get -u -v github.com/elazarl/go-bindata-assetfs/...
go get -u -v github.com/redefiance/go-find-references
go get -u -v github.com/sqs/goreturns
go get -u -v code.google.com/p/gomock/gomock
go get -u -v code.google.com/p/gomock/mockgen
go get -u -v github.com/axw/gocov/gocov
go get -u -v gopkg.in/matm/v1/gocov-html
go get -u -v github.com/AlekSi/gocov-xml
go get -u -v github.com/nsf/gocode
go get -u -v github.com/kisielk/errcheck
go get -u -v github.com/jstemmer/gotags
go get -u -v github.com/smartystreets/goconvey
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/pquerna/ffjson
go get -u -v github.com/clipperhouse/gen
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ However extra files you create won't be lost so they are safe to use for customi
To generate a server for a swagger spec document:
swagger generate all -f ./swagger.json -A [application-name] [--principal [principal-name]] --with-ui
swagger generate server -f ./swagger.json -A [application-name] [--principal [principal-name]] --with-ui
There are several other sub commands available for the generate command
Expand Down
27 changes: 19 additions & 8 deletions generator/bindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func templates_model_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/model.gotmpl", size: 629, mode: os.FileMode(420), modTime: time.Unix(1425268900, 0)}
info := bindata_file_info{name: "templates/model.gotmpl", size: 629, mode: os.FileMode(436), modTime: time.Unix(1427662469, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand All @@ -96,7 +96,7 @@ func templates_modelvalidator_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/modelvalidator.gotmpl", size: 4303, mode: os.FileMode(420), modTime: time.Unix(1425268913, 0)}
info := bindata_file_info{name: "templates/modelvalidator.gotmpl", size: 4303, mode: os.FileMode(436), modTime: time.Unix(1427662469, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand All @@ -116,7 +116,7 @@ func templates_server_builder_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/server/builder.gotmpl", size: 8438, mode: os.FileMode(420), modTime: time.Unix(1425268664, 0)}
info := bindata_file_info{name: "templates/server/builder.gotmpl", size: 8438, mode: os.FileMode(436), modTime: time.Unix(1427662469, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand All @@ -136,7 +136,7 @@ func templates_server_configureapi_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/server/configureapi.gotmpl", size: 2411, mode: os.FileMode(420), modTime: time.Unix(1425268862, 0)}
info := bindata_file_info{name: "templates/server/configureapi.gotmpl", size: 2411, mode: os.FileMode(436), modTime: time.Unix(1427662469, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand All @@ -156,12 +156,12 @@ func templates_server_main_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/server/main.gotmpl", size: 1302, mode: os.FileMode(420), modTime: time.Unix(1425268872, 0)}
info := bindata_file_info{name: "templates/server/main.gotmpl", size: 1302, mode: os.FileMode(436), modTime: time.Unix(1427662469, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}

var _templates_server_operation_gotmpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xdc\x56\x4d\x6f\xe3\x36\x10\xbd\xeb\x57\x4c\x8d\xed\xd6\x0e\x54\xe5\x9e\x62\x0f\x6d\xb6\x45\xf6\xd0\xed\x22\x09\xda\x63\xc1\x50\x23\x8b\x8d\x4c\x2a\xfc\x88\xe3\x0a\xfa\xef\x1d\x7e\x48\x91\x1d\xd9\x28\xda\x6e\x50\x2c\x60\xc0\x92\x38\x7c\xc3\xf7\xe6\xcd\x48\x2d\xe3\xf7\x6c\x8d\xd0\x75\xc5\xa7\x78\xd9\xf7\x59\x76\x7e\x0e\xb7\xb5\x30\x50\x89\x06\x61\xcb\x0c\xac\x51\xa2\x66\x16\x4b\xb8\xdb\x81\xad\x11\xcc\x96\xad\xd7\xa8\xc1\x2a\xd5\x14\x3e\xfe\xc7\x52\x58\x21\xd7\xb4\x38\xec\xdb\x88\x75\x6d\xa1\xd5\xea\x11\xa1\x72\x36\x40\xd5\x28\x61\xa7\x1c\x68\xfc\x56\x3b\x19\x90\x06\x68\xe0\x6a\xb3\x61\xb2\xcc\x32\xb1\x69\x95\xb6\xb0\xcc\x00\x16\x12\xed\x79\x6d\x6d\xbb\xc8\xe8\xae\xeb\x34\x93\x74\xd8\xe2\x3d\x56\xcc\x35\xf6\x43\x08\x34\x7d\xdf\x75\xad\x16\xd2\x56\xb0\xf8\xfa\x61\x01\x05\x51\xf0\xc1\x28\xcb\x74\x15\xb7\xbd\xb9\xc7\x5d\x0e\x6f\x1e\x59\xe3\x10\x2e\xde\x41\x31\xd9\xef\xd7\xfa\x9e\x42\x61\x8a\x14\x63\xf7\xe0\x56\x41\x1d\x92\xeb\xb2\x61\xc6\x7c\x64\x1b\x5a\xbe\xa2\x63\x37\xa8\x7f\x72\x92\x83\x75\x5a\x1a\x60\xc4\x58\x72\x2b\x94\x84\xad\xb0\x75\x20\xaa\x83\x1e\x46\xac\x25\xa3\x20\x04\x4a\xa3\x28\x90\xa0\xae\x1c\x11\x9f\xe0\x41\x1d\x01\x33\xbb\x6b\xf1\x44\x2e\x9f\x63\xd9\x75\xa2\x02\x2a\x9e\x66\x9b\xc0\x64\x1a\x1c\x9f\xa6\xa3\x87\x40\xda\x0d\xc5\xf7\xce\xd6\x4a\x8b\x3f\xa9\x9c\xe3\xc6\x1c\xa6\x61\x93\x90\xbe\x3f\xf3\xe6\x20\x55\xb8\x68\x59\xe3\x03\x42\xdc\x0a\x52\xea\x1b\xc7\x39\x1a\xf3\xb3\x2a\xb1\x19\xb6\x5f\x63\xd0\xe1\x52\x6d\xda\x06\x9f\x7e\xb9\xfb\x03\xb9\x0d\x40\x29\xc5\xc1\xa6\x31\x39\x6a\xad\x34\x49\xec\x99\xc1\xb2\x92\xc7\xc9\xaf\x20\xde\x1c\xf0\x6f\xc3\x3f\x7c\x0e\x19\xda\x41\x02\x78\x75\x41\xa0\x23\x07\xea\x00\x01\x95\x9c\xa5\xfc\x9f\xb1\x1b\xd8\x64\xfd\x71\xa7\x7b\xef\xa2\xae\x18\xa7\xce\x56\x34\x04\x6a\x66\x81\x33\x99\x7c\x0b\xd4\x35\xa2\x9c\x35\x76\x3c\xeb\x09\x5f\x4f\x90\x3d\xe7\xd9\x1a\x7f\x19\x1e\x8f\xf2\x7e\xc4\xed\x3e\x1d\xe0\x1a\x69\x1a\xfa\x11\x22\x71\x0b\x7e\xf6\x15\x83\x36\x51\x6b\x9c\x55\x56\xb5\x7e\x8a\xd2\xc0\x89\xbd\xf3\x02\x77\xc9\xed\x13\x9c\x6d\x44\x49\x48\x5b\xa6\xb1\xb8\x54\xa4\xf4\x93\xcd\x87\x61\x33\x5f\x8f\x55\x30\xfb\x34\xd1\xc4\x8a\x6f\xf7\x97\xba\x04\x79\x01\x94\x2b\x4f\xb5\xd3\x17\x43\x82\xde\x53\x8e\xd2\xbd\x57\xfc\xc6\x92\xda\xeb\xa0\xd3\xde\x5d\x9c\xb2\x33\x06\x01\x63\xb5\xe3\x36\xe4\x4f\x89\xe6\xf8\x84\x51\x3d\x75\x4b\xfc\x87\xd9\x91\xf0\x3c\xd7\xaf\x4e\x89\xe0\x0f\x1e\x27\x12\x2d\x5f\x23\x47\xf1\x88\x3a\x9d\xea\x40\x9e\x15\xdc\xa0\x7e\xc4\xab\xdb\xdb\x4f\x4b\x9d\xca\x77\x8d\xa6\x55\xd2\xe0\x6f\x5a\x90\xb7\x73\xd0\x70\x96\x9e\x3f\x38\x34\x36\x75\xb7\x72\x16\x73\xf8\xdd\xbf\x98\x5e\x64\x19\xc8\x15\xd7\x3e\xea\x83\xac\xd4\xd2\x4f\xc9\x81\xea\xd4\xc8\x2e\x34\x72\x0e\xe4\xb2\xd3\x50\xe3\xa6\xa5\x3f\x92\xc7\x5d\x11\x20\xc1\xf9\x9d\x5f\xbd\x03\x29\x9a\x70\x30\x38\x75\x9c\xc0\xac\x24\xa6\x04\x91\x50\xa8\x8d\x54\xe9\xc8\xf8\xf9\xc0\x89\x00\x57\x01\x28\xda\x86\x2e\xfd\x0b\xf5\x79\x9c\x86\x63\xfa\xc4\x0f\xf0\xdc\x83\xb0\x18\x27\x41\xd7\x2f\x06\x5e\x54\xad\xc6\xe0\x70\x57\x2c\x0f\xda\x76\x05\xd4\x52\xc2\x7e\x63\x40\xdd\xc7\x2f\x11\xfa\x51\x3f\x35\xcd\x2e\xbe\x69\x5f\xb6\x78\xb6\xf7\xa9\x90\xe8\x9f\x14\xee\x07\x21\xcb\x5f\xfd\x84\x4b\xf5\x1b\xf5\xcb\x0f\x9c\xf7\xf6\x25\xc6\x38\xac\x02\x0b\x92\x78\x18\x34\xdf\xed\xc9\xee\x69\xdc\x51\x9a\x61\x5e\x7e\xae\x2a\xcc\x3a\x68\x7c\xb8\x3f\xbf\xb4\xc7\x7a\x1e\x61\xb3\x31\x17\x69\x7d\x4e\xbe\xd4\x4b\xc5\xb1\x91\x3e\xab\xd4\x98\x71\x74\x4b\x28\x31\xe3\xd6\x85\xa2\xa6\x17\x4e\xf8\xbc\x8a\xd5\x78\x45\x0f\x67\xff\x1e\xf6\x88\xd0\x33\x06\x89\x36\x0d\x4f\xff\x66\x81\x66\x4d\xfc\x8f\xaa\x30\xbe\x0d\xff\x37\xd2\xbf\xbe\xf2\x61\x40\xf4\xd9\x5f\x01\x00\x00\xff\xff\xf0\x4d\x7f\xbe\x30\x0d\x00\x00")
var _templates_server_operation_gotmpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xdc\x56\xdf\x6f\xe4\x34\x10\x7e\xcf\x5f\x31\xac\x8e\x63\x53\x85\xf4\xbd\xa8\x0f\xd0\x03\xf5\x1e\x38\x4e\x6d\x05\x8f\xc8\x4d\x26\x1b\xd3\xc4\x4e\x1d\x7b\xb7\x4b\x94\xff\x9d\xf1\x8f\x64\x93\x6d\xba\x42\x40\x2b\x74\x52\xa5\x6e\xe2\xf1\x37\xfe\xbe\x99\xf9\x9c\x86\x65\x0f\x6c\x83\xd0\x75\xe9\x67\xff\xb3\xef\xa3\xe8\xfc\x1c\xee\x4a\xde\x42\xc1\x2b\x84\x1d\x6b\x61\x83\x02\x15\xd3\x98\xc3\xfd\x1e\x74\x89\xd0\xee\xd8\x66\x83\x0a\xb4\x94\x55\x6a\xe3\x7f\xcc\xb9\xe6\x62\x43\x8b\xc3\xbe\x9a\x6f\x4a\x0d\x8d\x92\x5b\x84\xc2\x68\x07\x55\xa2\x80\xbd\x34\xa0\xf0\x5b\x65\x84\x43\x1a\xa0\x21\x93\x75\xcd\x44\x1e\x45\xbc\x6e\xa4\xd2\xb0\x8e\x00\x56\x02\xf5\x79\xa9\x75\xb3\x8a\xe8\xa9\xeb\x14\x13\x74\xd8\xf4\x03\x16\xcc\x54\xfa\xa3\x0b\x6c\xfb\xbe\xeb\x1a\xc5\x85\x2e\x60\xf5\xf5\xe3\x0a\x52\xa2\x60\x83\x51\xe4\xe1\x97\xdf\xf6\xee\x01\xf7\x09\xbc\xdb\xb2\xca\x20\x5c\x5c\x42\x3a\xd9\x6f\xd7\xfa\x9e\x42\x61\x8a\xe4\x63\x67\x70\xb1\x53\x87\xe4\xba\xaa\x58\xdb\x7e\x62\x35\x2d\x5f\xd3\xb1\x2b\x54\x3f\x19\x91\x81\x36\x4a\xb4\xc0\x88\xb1\xc8\x34\x97\x02\x76\x5c\x97\x8e\xa8\x72\x7a\xb4\x7c\x23\x18\x05\x21\x50\x1a\x49\x81\x04\x75\x6d\x88\xf8\x04\x0f\x4a\x0f\x18\xe9\x7d\x83\x27\x72\xd9\x1c\xeb\xae\xe3\x05\x50\xf1\x14\xab\x1d\x93\x69\xb0\x7f\x1b\x8e\xee\x02\x69\x37\xa4\xdf\x1b\x5d\x4a\xc5\xff\xa4\x72\x8e\x1b\x13\x98\x86\x4d\x42\xfa\xfe\xcc\x36\x07\xa9\x92\xf1\x86\x55\x36\xc0\xc5\xc5\x10\x52\xdf\x9a\x2c\xc3\xb6\xfd\x59\xe6\x58\x0d\xdb\x6f\xd0\xe9\x70\x25\xeb\xa6\xc2\xa7\x5f\xee\xff\xc0\x4c\x3b\xa0\x90\xe2\x68\xd3\x98\x1c\x95\x92\x8a\x24\xb6\xcc\x60\x5d\x88\x97\xc9\xc7\xe0\x1f\x8e\xf8\x37\xee\x3f\xbc\x86\x0c\xcd\x20\x01\xbc\xb9\x20\xd0\x51\x07\x2a\x07\x01\x85\x58\xa4\xfc\x9f\xb1\x1b\xd8\x44\xfd\xcb\x9d\x6e\x7b\x17\x55\xc1\x32\x9a\x6c\x49\x26\x50\x32\x0d\x19\x13\xa1\x6f\x81\xa6\x86\xe7\x8b\x8d\xed\xcf\x7a\xa2\xaf\x27\xc8\x96\xf3\x62\x8d\xbf\x8c\x1e\xf7\xf2\x7e\xc2\xdd\x9c\x0e\x64\x0a\xc9\x0d\xad\x85\x08\xdc\x81\xf5\xbe\x74\xd0\xc6\x6b\x8d\x8b\xca\xca\xc6\xba\x28\x19\x8e\x9f\x9d\x67\xb8\xeb\x4c\x3f\xc1\x59\xcd\x73\x42\xda\x31\x85\xe9\x95\x24\xa5\x9f\x74\x32\x98\xcd\x72\x3d\x62\xd7\xec\xd3\x44\x93\x56\x7c\x3f\x5f\xea\x02\xe4\x05\x50\xae\x24\xd4\x4e\x5d\x0c\x09\x7a\x4b\xd9\x4b\xf7\x41\x66\xb7\x9a\xd4\xde\x38\x9d\x66\x4f\xde\x65\x17\x1a\x04\x5a\xad\x4c\xa6\x5d\xfe\x90\x68\x89\x8f\xb3\xea\x69\xb7\xf8\xff\xb0\x68\x09\x07\x5f\xbf\x3e\x25\x82\x3d\xb8\x77\x24\x5a\xbe\xc1\x0c\xf9\x16\x55\x38\xd5\x91\x3c\x31\xdc\xa2\xda\xe2\xf5\xdd\xdd\xe7\xb5\x0a\xe5\xbb\xc1\xb6\x91\xa2\xc5\xdf\x14\xa7\xde\x4e\x40\xc1\x59\x78\xff\x68\xb0\xd5\x61\xba\xa5\xd1\x98\xc0\xef\xf6\x62\x7a\x96\x65\x20\x97\xde\xd8\xa8\x8f\xa2\x90\x6b\xeb\x92\x03\xd5\x69\x23\x1b\x37\xc8\x09\x50\x97\x9d\x86\x1a\x37\xad\xed\x91\x2c\x6e\x4c\x80\x04\x67\x77\x7e\x75\x09\x82\x57\xee\x60\x70\xea\x38\x8e\x59\x4e\x4c\x09\x22\xa0\xd0\x18\xc9\xdc\x50\xe3\x27\x03\x27\x02\x8c\x1d\x90\x6f\x1b\xfa\x69\x2f\xd4\x2d\x53\x70\xb0\x54\x47\x44\x48\xba\xf5\xf1\x11\x0e\x93\x08\xab\xd1\x0f\xba\x7e\x15\xcf\xc6\x6b\x32\xae\xe0\x4f\xee\xb9\xcf\x0f\x7f\x48\x71\xe9\x93\x9c\xc0\x1f\xd4\xa3\x1c\x55\x8b\xc3\x53\xba\x3e\x32\x87\x18\x68\x70\xb9\xfe\xa6\x05\xf9\xe0\xbf\x77\xe8\x8f\xa6\xb6\xaa\xf6\xfe\x3e\x7f\x6e\x24\x8e\xf3\xec\xa3\x24\x08\x7d\xb2\x44\x3f\x70\x91\xff\x6a\xbd\x34\x74\xca\x58\xa9\xe4\xa8\xc7\xdf\x3f\xc7\x18\x6d\xd1\x31\x21\x3d\x06\x4b\xfb\x6e\x56\x60\x4b\xe5\x9e\xd2\x0c\xce\xfc\x5a\xf5\x5e\xec\xd5\xf1\xe5\xdc\x29\x95\xc5\x3a\x98\xe5\x62\xcc\x45\x58\x5f\x92\x2f\x4c\x6d\xfa\xd2\xe5\xb1\xa8\xd4\x98\x71\x6c\x18\x57\x66\x96\x69\xe3\x0a\x1b\xae\x36\xf7\x21\xe7\xab\xf1\x86\xd3\x12\xfd\x7b\xd8\x17\x84\x5e\x68\x10\xdf\xa6\xee\xed\xdf\x2c\xd0\x62\x13\xff\xa3\x2a\x8c\xf7\xee\xff\x46\xfa\xb7\x57\xde\x19\x44\x1f\xfd\x15\x00\x00\xff\xff\x9f\x00\x11\xa3\x9a\x0d\x00\x00")

func templates_server_operation_gotmpl_bytes() ([]byte, error) {
return bindata_read(
Expand All @@ -176,7 +176,7 @@ func templates_server_operation_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/server/operation.gotmpl", size: 3376, mode: os.FileMode(420), modTime: time.Unix(1425268881, 0)}
info := bindata_file_info{name: "templates/server/operation.gotmpl", size: 3482, mode: os.FileMode(436), modTime: time.Unix(1428281804, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand All @@ -196,7 +196,7 @@ func templates_server_parameter_gotmpl() (*asset, error) {
return nil, err
}

info := bindata_file_info{name: "templates/server/parameter.gotmpl", size: 8796, mode: os.FileMode(420), modTime: time.Unix(1425268892, 0)}
info := bindata_file_info{name: "templates/server/parameter.gotmpl", size: 8796, mode: os.FileMode(436), modTime: time.Unix(1427662469, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
Expand All @@ -216,6 +216,17 @@ func Asset(name string) ([]byte, error) {
return nil, fmt.Errorf("Asset %s not found", name)
}

// MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables.
func MustAsset(name string) []byte {
a, err := Asset(name)
if (err != nil) {
panic("asset: Asset(" + name + "): " + err.Error())
}

return a
}

// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
Expand Down
5 changes: 4 additions & 1 deletion generator/templates/server/operation.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func ({{.ReceiverName}} *{{.ClassName}}) ServeHTTP(rw http.ResponseWriter, r *ht
{{.ReceiverName}}.Context.Respond(rw, r, route.Produces, route, err)
return
}
principal := {{if eq .Principal "interface{}"}}uprinc{{else}}uprinc.(*{{.Principal}}) // it's ok this is really a {{.Principal}}{{end}}
var principal {{if not (eq .Principal "interface{}")}}*{{end}}{{.Principal}}
if uprinc != nil {
principal = {{if eq .Principal "interface{}"}}uprinc{{else}}uprinc.(*{{.Principal}}) // it's ok this is really a {{.Principal}}{{end}}
}

{{end}}
if err := {{.ReceiverName}}.Context.BindValidRequest(r, route, {{if .Params}}&{{.ReceiverName}}.Params{{else}}nil{{end}}); err != nil { // bind params
Expand Down
10 changes: 1 addition & 9 deletions middleware/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,13 @@ func (r *routableUntypedAPI) DefaultConsumes() string {
// NewRoutableContext creates a new context for a routable API
func NewRoutableContext(spec *spec.Document, routableAPI RoutableAPI, routes Router) *Context {
ctx := &Context{spec: spec, api: routableAPI}
if routes == nil {
routes = DefaultRouter(spec, routableAPI)
}
ctx.router = routes
return ctx
}

// NewContext creates a new context wrapper
func NewContext(spec *spec.Document, api *untyped.API, routes Router) *Context {
ctx := &Context{spec: spec}
if routes == nil {
ctx.api = newRoutableUntypedAPI(spec, api, ctx)
routes = DefaultRouter(spec, ctx.api)
}
ctx.router = routes
ctx.api = newRoutableUntypedAPI(spec, api, ctx)
return ctx
}

Expand Down
11 changes: 11 additions & 0 deletions middleware/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestServe(t *testing.T) {
func TestContextAuthorize(t *testing.T) {
spec, api := petstore.NewAPI(t)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := httputils.JSONRequest("GET", "/pets", nil)

Expand Down Expand Up @@ -88,6 +89,7 @@ func TestContextAuthorize(t *testing.T) {
func TestContextBindAndValidate(t *testing.T) {
spec, api := petstore.NewAPI(t)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := http.NewRequest("POST", "/pets", nil)
request.Header.Add("Accept", "*/*")
Expand All @@ -114,9 +116,12 @@ func TestContextBindAndValidate(t *testing.T) {
func TestContextRender(t *testing.T) {
ct := httputils.JSONMime
spec, api := petstore.NewAPI(t)

assert.NotNil(t, spec)
assert.NotNil(t, api)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := http.NewRequest("GET", "pets", nil)
request.Header.Set(httputils.HeaderAccept, ct)
ri, _ := ctx.RouteInfo(request)
Expand Down Expand Up @@ -168,6 +173,7 @@ func TestContextValidResponseFormat(t *testing.T) {
ct := "application/json"
spec, api := petstore.NewAPI(t)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := http.NewRequest("GET", "http://localhost:8080", nil)
request.Header.Set(httputils.HeaderAccept, ct)
Expand Down Expand Up @@ -196,6 +202,7 @@ func TestContextInvalidResponseFormat(t *testing.T) {
other := "application/sgml"
spec, api := petstore.NewAPI(t)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := http.NewRequest("GET", "http://localhost:8080", nil)
request.Header.Set(httputils.HeaderAccept, ct)
Expand All @@ -222,6 +229,8 @@ func TestContextInvalidResponseFormat(t *testing.T) {
func TestContextValidRoute(t *testing.T) {
spec, api := petstore.NewAPI(t)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := http.NewRequest("GET", "/pets", nil)

// check there's nothing there
Expand All @@ -244,6 +253,8 @@ func TestContextValidRoute(t *testing.T) {
func TestContextInvalidRoute(t *testing.T) {
spec, api := petstore.NewAPI(t)
ctx := NewContext(spec, api, nil)
ctx.router = DefaultRouter(spec, ctx.api)

request, _ := http.NewRequest("DELETE", "pets", nil)

// check there's nothing there
Expand Down
2 changes: 2 additions & 0 deletions middleware/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestOperationExecutor(t *testing.T) {
}))

context := NewContext(spec, api, nil)
context.router = DefaultRouter(spec, context.api)
mw := newOperationExecutor(context)

recorder := httptest.NewRecorder()
Expand All @@ -36,6 +37,7 @@ func TestOperationExecutor(t *testing.T) {
}))

context = NewContext(spec, api, nil)
context.router = DefaultRouter(spec, context.api)
mw = newOperationExecutor(context)

recorder = httptest.NewRecorder()
Expand Down
3 changes: 3 additions & 0 deletions middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (r RouteParams) Get(name string) string {
}

func newRouter(ctx *Context, next http.Handler) http.Handler {
if ctx.router == nil {
ctx.router = DefaultRouter(ctx.spec, ctx.api)
}
isRoot := ctx.spec.BasePath() == "" || ctx.spec.BasePath() == "/"

return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions middleware/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func TestSecurityMiddleware(t *testing.T) {
spec, api := petstore.NewAPI(t)
context := NewContext(spec, api, nil)
context.router = DefaultRouter(spec, context.api)
mw := newSecureAPI(context, http.HandlerFunc(terminator))

recorder := httptest.NewRecorder()
Expand Down
2 changes: 2 additions & 0 deletions middleware/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func TestContentTypeValidation(t *testing.T) {
spec, api := petstore.NewAPI(t)
context := NewContext(spec, api, nil)
context.router = DefaultRouter(spec, context.api)
mw := newValidation(context, http.HandlerFunc(terminator))

recorder := httptest.NewRecorder()
Expand Down Expand Up @@ -44,6 +45,7 @@ func TestContentTypeValidation(t *testing.T) {
func TestResponseFormatValidation(t *testing.T) {
spec, api := petstore.NewAPI(t)
context := NewContext(spec, api, nil)
context.router = DefaultRouter(spec, context.api)
mw := newValidation(context, http.HandlerFunc(terminator))

recorder := httptest.NewRecorder()
Expand Down

0 comments on commit fcac9d2

Please sign in to comment.