这是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
2 changes: 1 addition & 1 deletion patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (r *Router) AutoPatch() {
case http.MethodPut:
put = op
_, reqDef := put.requestForContentType("application/json")
if reqDef.model != nil {
if reqDef != nil && reqDef.model != nil {
kind = reqDef.model.Kind()
if kind == reflect.Ptr {
kind = reqDef.model.Elem().Kind()
Expand Down
30 changes: 30 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,33 @@ func TestPatch(t *testing.T) {
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnsupportedMediaType, w.Code, w.Body.String())
}

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

things := app.Resource("/things/{thing-id}")

things.Get("get-thing", "docs",
NewResponse(http.StatusOK, "OK").Model(&ThingModel{}),
).Run(func(ctx Context, input struct {
ThingIDParam
}) {
ctx.WriteModel(http.StatusOK, &ThingModel{})
})

things.Put("put-thing", "docs",
NewResponse(http.StatusNoContent, "No Content"),
).Run(func(ctx Context, input struct {
ThingIDParam
// Note: no body!
}) {
ctx.WriteHeader(http.StatusNoContent)
})

// There should be no generated PATCH since there is nothing to
// write in the PUT!
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodPatch, "/things/test", nil)
app.ServeHTTP(w, req)
assert.Equal(t, http.StatusMethodNotAllowed, w.Code, w.Body.String())
}
2 changes: 1 addition & 1 deletion resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func setFields(ctx *hcontext, req *http.Request, input reflect.Value, t reflect.
continue
}

if reqDef.schema != nil && reqDef.schema.HasValidation() {
if reqDef != nil && reqDef.schema != nil && reqDef.schema.HasValidation() {
if !validAgainstSchema(ctx, locationBody+".", reqDef.schema, data) {
continue
}
Expand Down