feat: panic on invalid path param name

This commit is contained in:
Daniel G. Taylor 2020-09-04 21:10:40 -07:00
parent b53adad969
commit 035afde66a
No known key found for this signature in database
GPG key ID: 7BD6DC99C9A87E22
2 changed files with 26 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"reflect"
"strings"
"time"
"github.com/Jeffail/gabs/v2"
@ -146,6 +147,15 @@ func (o *Operation) Run(handler interface{}) {
// Get parameters
o.params = getParamInfo(input)
for k, v := range o.params {
if v.In == inPath {
// Confirm each declared input struct path parameter is actually a part
// of the declared resource path.
if !strings.Contains(o.resource.path, "{"+k+"}") {
panic(fmt.Errorf("Parameter '%s' not in URI path: %s", k, o.resource.path))
}
}
}
// Get body if present.
if body, ok := input.FieldByName("Body"); ok {

View file

@ -221,3 +221,19 @@ func TestErrorHandlers(t *testing.T) {
assert.Equal(t, "application/problem+json", w.Header().Get("Content-Type"))
assert.Contains(t, w.Body.String(), "PUT")
}
func TestInvalidPathParam(t *testing.T) {
type Input struct {
ThingID string `path:"thing-if"`
}
app := newTestRouter()
assert.Panics(t, func() {
app.Resource("/things", "thing-id").Get("get", "Test",
NewResponse(http.StatusNoContent, "desc"),
).Run(func(ctx Context, input Input) {
// Do nothing
})
})
}