Skip to content

Commit e5f1d6c

Browse files
Dragomir-Ivanovsmyrman
authored andcommitted
Fix creating sub-resource items deeper than 2nd level
1 parent c3ebffd commit e5f1d6c

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

rest/resource_path.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77

88
"github.com/rs/rest-layer/resource"
9+
"github.com/rs/rest-layer/schema"
910
"github.com/rs/rest-layer/schema/query"
1011
)
1112

@@ -152,10 +153,28 @@ func (p ResourcePath) Path() string {
152153

153154
// Values returns all the key=value pairs defined by the resource path.
154155
func (p ResourcePath) Values() map[string]interface{} {
156+
path := p.Path()
157+
d := strings.LastIndexAny(path, ".")
158+
if d > 0 {
159+
path = path[0:d]
160+
} else {
161+
path = ""
162+
}
163+
targetResource := p[len(p)-1].Resource
164+
targetFields := targetResource.Schema().Fields
165+
155166
v := map[string]interface{}{}
156167
for _, rp := range p {
168+
include := false
157169
if _, found := v[rp.Field]; !found && rp.Value != nil {
158-
v[rp.Field] = rp.Value
170+
if def, ok := targetFields[rp.Field]; ok {
171+
if ref, ok := def.Validator.(*schema.Reference); ok && ref.Path == path {
172+
include = true
173+
}
174+
}
175+
if include == true || rp.Field == "id" {
176+
v[rp.Field] = rp.Value
177+
}
159178
}
160179
}
161180
return v

rest/resource_path_test.go

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,83 @@ import (
1010
)
1111

1212
func TestResourcePathValues(t *testing.T) {
13+
index := resource.NewIndex()
14+
users := index.Bind("users", schema.Schema{
15+
Fields: schema.Fields{
16+
"id": {
17+
Validator: &schema.String{},
18+
},
19+
},
20+
}, mem.NewHandler(), resource.DefaultConf)
21+
posts := users.Bind("posts", "user", schema.Schema{
22+
Fields: schema.Fields{
23+
"id": {
24+
Validator: &schema.Integer{},
25+
},
26+
"user": {
27+
Validator: &schema.Reference{Path: "users"},
28+
},
29+
},
30+
}, mem.NewHandler(), resource.DefaultConf)
1331
p := ResourcePath{
1432
&ResourcePathComponent{
15-
Name: "users",
16-
Field: "user",
17-
Value: "john",
33+
Name: "users",
34+
Field: "user",
35+
Value: "john",
36+
Resource: users,
1837
},
1938
&ResourcePathComponent{
20-
Name: "posts",
21-
Field: "id",
22-
Value: "123",
39+
Name: "posts",
40+
Field: "id",
41+
Value: "123",
42+
Resource: posts,
2343
},
2444
}
2545
assert.Equal(t, map[string]interface{}{"id": "123", "user": "john"}, p.Values())
2646
}
2747

48+
func TestResourcePathInvalidValues(t *testing.T) {
49+
index := resource.NewIndex()
50+
users := index.Bind("users", schema.Schema{
51+
Fields: schema.Fields{
52+
"id": {
53+
Validator: &schema.String{},
54+
},
55+
},
56+
}, mem.NewHandler(), resource.DefaultConf)
57+
posts := users.Bind("posts", "user", schema.Schema{
58+
Fields: schema.Fields{
59+
"id": {
60+
Validator: &schema.Integer{},
61+
},
62+
"user": {
63+
Validator: &schema.Reference{Path: "users"},
64+
},
65+
},
66+
}, mem.NewHandler(), resource.DefaultConf)
67+
p := ResourcePath{
68+
&ResourcePathComponent{
69+
Name: "users",
70+
Field: "user",
71+
Value: "john",
72+
Resource: users,
73+
},
74+
&ResourcePathComponent{
75+
Name: "posts",
76+
Field: "id",
77+
Value: "123",
78+
Resource: posts,
79+
},
80+
&ResourcePathComponent{
81+
Name: "users1",
82+
Field: "user1",
83+
Value: "john1",
84+
Resource: posts,
85+
},
86+
}
87+
assert.Equal(t, map[string]interface{}{"id": "123"}, p.Values())
88+
}
89+
2890
func TestResourcePathAppend(t *testing.T) {
2991
index := resource.NewIndex()
3092
users := index.Bind("users", schema.Schema{

0 commit comments

Comments
 (0)