Skip to content

Commit e5aeb52

Browse files
committed
Fix validator not called in id fields during resource path lookup
1 parent 5e9ff7b commit e5aeb52

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

rest/resource_path.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,24 @@ func (p *ResourcePath) Prepend(rsrc *resource.Resource, field string, value inte
4949
*p = append(ResourcePath{rp}, *p...)
5050
}
5151

52-
func (p *ResourcePath) append(rsrc *resource.Resource, field string, value interface{}, name string) {
52+
func (p *ResourcePath) append(rsrc *resource.Resource, field string, value interface{}, name string) (err error) {
53+
if field != "" && value != nil {
54+
if f, found := rsrc.Schema().Fields["id"]; found {
55+
if f.Validator != nil {
56+
value, err = f.Validator.Validate(value)
57+
if err != nil {
58+
return
59+
}
60+
}
61+
}
62+
}
5363
rp := resourcePathComponentPool.Get().(*ResourcePathComponent)
5464
rp.Name = name
5565
rp.Field = field
5666
rp.Value = value
5767
rp.Resource = rsrc
5868
*p = append(*p, rp)
69+
return
5970
}
6071

6172
func (p *ResourcePath) clear() {

rest/resource_path_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package rest
33
import (
44
"testing"
55

6+
mem "github.com/rs/rest-layer-mem"
7+
"github.com/rs/rest-layer/resource"
8+
"github.com/rs/rest-layer/schema"
69
"github.com/stretchr/testify/assert"
710
)
811

@@ -22,6 +25,37 @@ func TestResourcePathValues(t *testing.T) {
2225
assert.Equal(t, map[string]interface{}{"id": "123", "user": "john"}, p.Values())
2326
}
2427

28+
func TestResourcePathAppend(t *testing.T) {
29+
index := resource.NewIndex()
30+
users := index.Bind("users", schema.Schema{
31+
Fields: schema.Fields{
32+
"id": {
33+
Validator: &schema.String{},
34+
},
35+
},
36+
}, mem.NewHandler(), resource.DefaultConf)
37+
posts := users.Bind("posts", "user", schema.Schema{
38+
Fields: schema.Fields{
39+
"id": {
40+
Validator: &schema.Integer{},
41+
},
42+
"user": {
43+
Validator: &schema.Reference{Path: "users"},
44+
},
45+
},
46+
}, mem.NewHandler(), resource.DefaultConf)
47+
p := ResourcePath{}
48+
err := p.append(users, "user", 123, "users")
49+
assert.EqualError(t, err, "not a string")
50+
err = p.append(users, "user", "john", "users")
51+
assert.NoError(t, err)
52+
err = p.append(posts, "id", "123", "posts")
53+
assert.EqualError(t, err, "not an integer")
54+
err = p.append(posts, "id", 123, "posts")
55+
assert.Equal(t, map[string]interface{}{"id": 123, "user": "john"}, p.Values())
56+
57+
}
58+
2559
func TestResourcePathPrepend(t *testing.T) {
2660
p := ResourcePath{
2761
&ResourcePathComponent{

rest/routing.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ func findRoute(path string, index resource.Index, route *RouteMatch) error {
102102
subResourcePath := resourcePath + "." + subPathComp
103103
if subResource, found := index.GetResource(subResourcePath, nil); found {
104104
// Append the intermediate resource path
105-
route.ResourcePath.append(rsrc, subResource.ParentField(), id, name)
105+
if err := route.ResourcePath.append(rsrc, subResource.ParentField(), id, name); err != nil {
106+
return err
107+
}
106108
// Recurse to match the sub-path
107109
if err := findRoute(path, index, route); err != nil {
108110
return err
@@ -124,13 +126,11 @@ func findRoute(path string, index resource.Index, route *RouteMatch) error {
124126
}
125127
} else {
126128
// Set the id route field
127-
route.ResourcePath.append(rsrc, "id", id, name)
128-
return nil
129+
return route.ResourcePath.append(rsrc, "id", id, name)
129130
}
130131
}
131132
// Set the collection resource
132-
route.ResourcePath.append(rsrc, "", nil, name)
133-
return nil
133+
return route.ResourcePath.append(rsrc, "", nil, name)
134134
}
135135
route.ResourcePath.clear()
136136
return errResourceNotFound

0 commit comments

Comments
 (0)