Skip to content

Commit 71e0f48

Browse files
committed
fix: find HAL links in array responses
1 parent a088e40 commit 71e0f48

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

cli/links.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,27 @@ type HALParser struct{}
9191

9292
// ParseLinks processes the links in a parsed response.
9393
func (h HALParser) ParseLinks(resp *Response) error {
94-
hal := halBody{}
95-
if err := mapstructure.Decode(resp.Body, &hal); err == nil {
96-
for rel, link := range hal.Links {
97-
if rel == "curies" {
98-
// TODO: handle curies at some point?
99-
continue
100-
}
94+
entries := []interface{}{}
95+
if l, ok := resp.Body.([]interface{}); ok {
96+
entries = l
97+
} else {
98+
entries = append(entries, resp.Body)
99+
}
101100

102-
resp.Links[rel] = append(resp.Links[rel], &Link{
103-
Rel: rel,
104-
URI: link.Href,
105-
})
101+
for _, entry := range entries {
102+
hal := halBody{}
103+
if err := mapstructure.Decode(entry, &hal); err == nil {
104+
for rel, link := range hal.Links {
105+
if rel == "curies" {
106+
// TODO: handle curies at some point?
107+
continue
108+
}
109+
110+
resp.Links[rel] = append(resp.Links[rel], &Link{
111+
Rel: rel,
112+
URI: link.Href,
113+
})
114+
}
106115
}
107116
}
108117

cli/links_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ func TestHALParser(t *testing.T) {
7070
assert.Equal(t, r.Links["item"][0].URI, "/item")
7171
}
7272

73+
func TestHALParserArray(t *testing.T) {
74+
r := &Response{
75+
Links: Links{},
76+
Body: []interface{}{
77+
map[string]interface{}{
78+
"_links": map[string]interface{}{
79+
"self": map[string]interface{}{
80+
"href": "/one",
81+
},
82+
},
83+
},
84+
map[string]interface{}{
85+
"_links": map[string]interface{}{
86+
"self": map[string]interface{}{
87+
"href": "/two",
88+
},
89+
},
90+
},
91+
},
92+
}
93+
94+
p := HALParser{}
95+
err := p.ParseLinks(r)
96+
assert.NoError(t, err)
97+
assert.Equal(t, r.Links["self"][0].URI, "/one")
98+
assert.Equal(t, r.Links["self"][1].URI, "/two")
99+
}
100+
73101
func TestTerrificallySimpleJSONParser(t *testing.T) {
74102
r := &Response{
75103
Links: Links{},

0 commit comments

Comments
 (0)