Skip to content

Commit 04ec84d

Browse files
committed
fix(binding): bug
1. when there is unexportable and no tags, omit the field 2. raw_body Change-Id: I8d3ad16b2a6bcc26c523144c34c42b261516d14a
1 parent 12f24da commit 04ec84d

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

binding/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ The parameter position in HTTP request:
100100
- Expression `required` or `req` indicates that the parameter is required
101101
- `default:"$value"` defines the default value for fallback when no binding is successful
102102
- If no position is tagged, try bind parameters from the body when the request has body,
103-
<br>otherwise try bind from the URL query
104-
- When there are multiple tags or no tags, the order in which to try to bind is:
103+
<br>otherwise try bind from the URL query
104+
- When there is unexportable and no tags, omit the field
105+
- When there are multiple tags, or exportable and no tags, the order in which to try to bind is:
105106
1. path
106107
2. form
107108
3. query

binding/bind.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,12 @@ func (b *Binding) getOrPrepareReceiver(value reflect.Value) (*receiver, error) {
314314
break
315315
}
316316
}
317-
// Support default binding order when there is no valid tag in the superior fields
317+
if canDefault {
318+
if !goutil.IsExportedName(p.structField.Name) {
319+
canDefault = false
320+
}
321+
}
322+
// Supports the default binding order when there is no valid tag in the superior field of the exportable field
318323
if canDefault {
319324
for _, i := range sortedDefaultIn {
320325
if p.omitIns[i] {

binding/bind_test.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,16 @@ import (
2020

2121
func TestRawBody(t *testing.T) {
2222
type Recv struct {
23-
raw_body **struct {
24-
A []byte `raw_body:""`
25-
B *[]byte `raw_body:",required"`
26-
C **[]byte `raw_body:"required"`
27-
D string `raw_body:""`
28-
E *string `raw_body:""`
29-
F **string `raw_body:"" vd:"@:len($)<3; msg:'f too long'"`
30-
}
31-
S string `raw_body:""`
23+
S []byte `raw_body:""`
24+
F **string `raw_body:"" vd:"@:len($)<3; msg:'f too long'"`
3225
}
3326
bodyBytes := []byte("raw_body.............")
3427
req := newRequest("", nil, nil, bytes.NewReader(bodyBytes))
3528
recv := new(Recv)
3629
binder := binding.New(nil)
3730
err := binder.BindAndValidate(recv, req, nil)
38-
assert.EqualError(t, err, "validating: expr_path=raw_body.F, cause=f too long")
39-
for _, v := range []interface{}{
40-
(**recv.raw_body).A,
41-
*(**recv.raw_body).B,
42-
**(**recv.raw_body).C,
43-
[]byte((**recv.raw_body).D),
44-
[]byte(*(**recv.raw_body).E),
45-
[]byte(**(**recv.raw_body).F),
46-
[]byte(recv.S),
47-
} {
48-
assert.Equal(t, bodyBytes, v)
49-
}
31+
assert.EqualError(t, err, "validating: expr_path=F, cause=f too long")
32+
assert.Equal(t, bodyBytes, []byte(recv.S))
5033
bodyCopied, err := binding.GetBody(req)
5134
assert.NoError(t, err)
5235
assert.Equal(t, bodyBytes, bodyCopied.Bytes())
@@ -887,3 +870,23 @@ func TestQueryTypes(t *testing.T) {
887870
assert.Equal(t, metric("e-from-cookie"), recv.E)
888871
assert.Equal(t, filter{Col1: "abc"}, recv.F)
889872
}
873+
874+
func TestNoTagIssue(t *testing.T) {
875+
type x int
876+
type T struct {
877+
x
878+
x2 x
879+
a int
880+
B int
881+
}
882+
req := newRequest("http://localhost:8080/?x=11&x2=12&a=1&B=2", nil, nil, nil)
883+
recv := new(T)
884+
binder := binding.New(nil)
885+
binder.SetLooseZeroMode(true)
886+
err := binder.BindAndValidate(recv, req, nil)
887+
assert.NoError(t, err)
888+
assert.Equal(t, x(0), recv.x)
889+
assert.Equal(t, x(0), recv.x2)
890+
assert.Equal(t, 0, recv.a)
891+
assert.Equal(t, 2, recv.B)
892+
}

binding/receiver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (r *receiver) assginIn(i in, v bool) {
6464
r.hasPath = r.hasPath || v
6565
case query:
6666
r.hasQuery = r.hasQuery || v
67-
case form, json, protobuf:
67+
case form, json, protobuf, raw_body:
6868
r.hasBody = r.hasBody || v
6969
case cookie:
7070
r.hasCookie = r.hasCookie || v

0 commit comments

Comments
 (0)