Skip to content

Commit 393cf48

Browse files
tchsskRaphaël Simon
authored and
Raphaël Simon
committed
Fix gen_client to not crash when an array parameter is defined in an action using websocket (goadesign#1054)
* Add tests with an action using websocket in gen_client. * Add a missing brace bracket in gen_client This fixes gen_client to not crash when an array parameter is defined in an action using websocket.
1 parent 75ad5f2 commit 393cf48

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

goagen/gen_client/generator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ func (c *Client) {{ $funcName }}(ctx context.Context, path string{{ if .Params }
940940
{{ if .MustToString }}{{ $tmp := tempvar }} {{ toString "p" $tmp .ElemAttribute }}
941941
values.Add("{{ .Name }}", {{ $tmp }})
942942
{{ else }} values.Add("{{ .Name }}", {{ .ValueName }})
943-
{{ end }}{{/*
943+
{{ end }}}{{/*
944944
945945
// NON STRING
946946
*/}}{{ else if .MustToString }}{{ $tmp := tempvar }} {{ toString .ValueName $tmp .Attribute }}

goagen/gen_client/generator_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,80 @@ var _ = Describe("Generate", func() {
296296
})
297297
})
298298

299+
Context("with an action using websocket", func() {
300+
BeforeEach(func() {
301+
codegen.TempCount = 0
302+
o := design.Object{
303+
"fields[foo]": &design.AttributeDefinition{Type: design.String},
304+
"fields[bar]": &design.AttributeDefinition{Type: &design.Array{ElemType: &design.AttributeDefinition{Type: design.String}}},
305+
"fields[baz]": &design.AttributeDefinition{Type: &design.Array{ElemType: &design.AttributeDefinition{Type: design.Integer}}},
306+
"fields[bat]": &design.AttributeDefinition{Type: design.DateTime},
307+
}
308+
design.Design = &design.APIDefinition{
309+
Name: "testapi",
310+
Resources: map[string]*design.ResourceDefinition{
311+
"foo": {
312+
Name: "foo",
313+
Actions: map[string]*design.ActionDefinition{
314+
"show": {
315+
Name: "show",
316+
Schemes: []string{"ws"},
317+
Routes: []*design.RouteDefinition{
318+
{
319+
Verb: "GET",
320+
Path: "",
321+
},
322+
},
323+
QueryParams: &design.AttributeDefinition{Type: o},
324+
},
325+
},
326+
},
327+
},
328+
}
329+
fooRes := design.Design.Resources["foo"]
330+
showAct := fooRes.Actions["show"]
331+
showAct.Parent = fooRes
332+
showAct.Routes[0].Parent = showAct
333+
})
334+
335+
It("generates param initialization code that uses the param name given in the design", func() {
336+
Ω(genErr).Should(BeNil())
337+
Ω(files).Should(HaveLen(9))
338+
c, err := ioutil.ReadFile(filepath.Join(outDir, "client", "foo.go"))
339+
Ω(err).ShouldNot(HaveOccurred())
340+
content := string(c)
341+
Ω(content).Should(ContainSubstring("func ShowFooPath("))
342+
Ω(content).Should(ContainSubstring(`values.Set("fields[foo]", *fieldsFoo)`))
343+
Ω(content).Should(ContainSubstring(` if fieldsBar != nil {
344+
for _, p := range fieldsBar {
345+
tmp3 := p
346+
values.Add("fields[bar]", tmp3)
347+
}
348+
}
349+
`))
350+
Ω(content).Should(ContainSubstring(` if fieldsBaz != nil {
351+
for _, p := range fieldsBaz {
352+
tmp5 := strconv.Itoa(p)
353+
values.Add("fields[baz]", tmp5)
354+
}
355+
}
356+
`))
357+
Ω(content).Should(ContainSubstring(` tmp4 := fieldsBat.Format(time.RFC3339)
358+
values.Set("fields[bat]", tmp4)`))
359+
})
360+
361+
Context("with --notool", func() {
362+
BeforeEach(func() {
363+
os.Args = append(os.Args, "--notool")
364+
})
365+
366+
It("should not return an error", func() {
367+
Ω(genErr).Should(BeNil())
368+
Ω(files).Should(HaveLen(5)) // 9, minus 4 entries for tool paths
369+
})
370+
})
371+
})
372+
299373
Context("with an action with multiple routes", func() {
300374
BeforeEach(func() {
301375
design.Design = &design.APIDefinition{

0 commit comments

Comments
 (0)