Skip to content

Commit 8298a2c

Browse files
authored
fix: error parsing GET request (#108)
1 parent cebe2c2 commit 8298a2c

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

request.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@ import (
1111
type requestDecoder[V any] func(v *V, ctx *fasthttp.RequestCtx, p httprouter.Params) error
1212

1313
func newRequestDecoder[V any](v V) requestDecoder[V] {
14+
path, _ := decoder.NewCached(v, "path")
1415
query, _ := decoder.NewCached(v, "query")
15-
params, _ := decoder.NewCached(v, "path")
1616
header, _ := decoder.NewCached(v, "header")
1717

18-
if query == nil && params == nil && header == nil {
18+
if path == nil && query == nil && header == nil {
1919
return decodeBody[V]()
2020
}
2121

22-
return decodeRequest(query, header, params)
22+
return decodeRequest(path, query, header)
2323
}
2424

25-
func decodeRequest[V any](query, header, params *decoder.CachedDecoder[V]) requestDecoder[V] {
26-
dec := decodeBody[V]()
27-
25+
func decodeRequest[V any](path, query, header *decoder.CachedDecoder[V]) requestDecoder[V] {
26+
body := decodeBody[V]()
2827
return func(v *V, ctx *fasthttp.RequestCtx, p httprouter.Params) error {
28+
if err := body(v, ctx, nil); err != nil {
29+
return err
30+
}
31+
2932
val := reflect.ValueOf(v).Elem()
3033

34+
if path != nil && len(p) > 0 {
35+
if err := path.DecodeValue((decoder.Params)(p), val); err != nil {
36+
return ErrNotFound
37+
}
38+
}
39+
3140
if query != nil {
3241
if q := ctx.Request.URI().QueryArgs(); q.Len() > 0 {
3342
if err := query.DecodeValue((*decoder.Args)(q), val); err != nil {
@@ -36,25 +45,19 @@ func decodeRequest[V any](query, header, params *decoder.CachedDecoder[V]) reque
3645
}
3746
}
3847

39-
if params != nil && len(p) > 0 {
40-
if err := params.DecodeValue((decoder.Params)(p), val); err != nil {
41-
return ErrNotFound
42-
}
43-
}
44-
4548
if header != nil {
4649
if err := header.DecodeValue((*decoder.Header)(&ctx.Request.Header), val); err != nil {
4750
return err
4851
}
4952
}
5053

51-
return dec(v, ctx, p)
54+
return nil
5255
}
5356
}
5457

5558
func decodeBody[V any]() requestDecoder[V] {
56-
return func(v *V, ctx *fasthttp.RequestCtx, p httprouter.Params) error {
57-
if ctx.Request.Header.ContentLength() == 0 {
59+
return func(v *V, ctx *fasthttp.RequestCtx, _ httprouter.Params) error {
60+
if ctx.Request.Header.ContentLength() == 0 || ctx.IsGet() || ctx.IsHead() {
5861
return nil
5962
}
6063

0 commit comments

Comments
 (0)