这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,14 @@ func setFields(ctx *hcontext, req *http.Request, input reflect.Value, t reflect.
if name, ok := f.Tag.Lookup(locationPath); ok {
pname = name
location = locationPath
if v := chi.URLParam(req, name); v != "" {
v := chi.URLParam(req, name)
if v == "" {
ctx.AddError(&ErrorDetail{
Message: fmt.Sprintf("%s is required", name),
Location: location + "." + name,
Value: v,
})
} else {
pv = v
}
}
Expand Down
21 changes: 21 additions & 0 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,24 @@ func TestRawBody(t *testing.T) {

assert.Equal(t, http.StatusUnprocessableEntity, w.Result().StatusCode)
}

type PathParamTestModel struct {
TestParam string `path:"test-id"`
}

func TestPathParamAlwaysRequired(t *testing.T) {
app := newTestRouter()

app.Resource("/test/{test-id}/foo").Get("test", "Test",
NewResponse(http.StatusOK, "desc"),
).Run(func(ctx Context, input PathParamTestModel) {
ctx.WriteHeader(http.StatusOK)
})

w := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "/test//foo", nil)
app.ServeHTTP(w, r)

assert.Equal(t, http.StatusBadRequest, w.Result().StatusCode)
assert.Contains(t, w.Body.String(), "test-id is required")
}