Skip to content

Commit 82daa90

Browse files
authored
codegen: fix client init for interceptor-only services (#3906)
* codegen: make union collection traversal deterministic Sort object attributes before collecting union types in service and HTTP codegen so NameScope-assigned union identifiers do not oscillate across runs. Add regression tests that assert union naming remains stable when object field declaration order is reversed. * codegen: fix client init for no-method services When a service has a client interceptor but no methods, client codegen emitted an unnamed goa.Endpoint parameter. Generate a zero-endpoint NewClient signature and add a regression test.
1 parent 68c77a7 commit 82daa90

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

codegen/service/client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestClient(t *testing.T) {
3333
{"client-bidirectional-streaming", testdata.BidirectionalStreamingMethodDSL, testdata.BidirectionalStreamingMethodClient},
3434
{"client-bidirectional-streaming-no-payload", testdata.BidirectionalStreamingNoPayloadMethodDSL, testdata.BidirectionalStreamingNoPayloadMethodClient},
3535
{"client-interceptor", testdata.EndpointWithClientInterceptorDSL, testdata.InterceptorClient},
36+
{"client-interceptor-no-method", testdata.NoMethodClientInterceptorDSL, testdata.NoMethodInterceptorClient},
3637
}
3738
for _, c := range cases {
3839
t.Run(c.Name, func(t *testing.T) {

codegen/service/templates/service_client_init.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{ printf "New%s initializes a %q service client given the endpoints." .ClientVarName .Name | comment }}
2-
func New{{ .ClientVarName }}({{ .ClientInitArgs }} goa.Endpoint{{ if .HasClientInterceptors }}, ci ClientInterceptors{{ end }}) *{{ .ClientVarName }} {
2+
func New{{ .ClientVarName }}({{ if .ClientInitArgs }}{{ .ClientInitArgs }} goa.Endpoint{{ if .HasClientInterceptors }}, ci ClientInterceptors{{ end }}{{ else }}{{ if .HasClientInterceptors }}ci ClientInterceptors{{ end }}{{ end }}) *{{ .ClientVarName }} {
33
return &{{ .ClientVarName }}{
44
{{- range .Methods }}
55
{{ .EndpointField }}: {{ if .ClientInterceptors }}Wrap{{ .VarName }}ClientEndpoint({{ end }}{{ .ArgName }}{{ if .ClientInterceptors }}, ci){{ end }},

codegen/service/testdata/client_code.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,14 @@ func (c *Client) Method(ctx context.Context, p string) (res string, err error) {
346346
return ires.(string), nil
347347
}
348348
`
349+
350+
const NoMethodInterceptorClient = `// Client is the "NoMethodClientInterceptor" service client.
351+
type Client struct {
352+
}
353+
354+
// NewClient initializes a "NoMethodClientInterceptor" service client given the
355+
// endpoints.
356+
func NewClient(ci ClientInterceptors) *Client {
357+
return &Client{}
358+
}
359+
`

codegen/service/testdata/service_dsls.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,13 @@ var EndpointWithClientInterceptorDSL = func() {
10411041
})
10421042
}
10431043

1044+
var NoMethodClientInterceptorDSL = func() {
1045+
Interceptor("tracing")
1046+
Service("NoMethodClientInterceptor", func() {
1047+
ClientInterceptor("tracing")
1048+
})
1049+
}
1050+
10441051
var MultipleAPIKeySecurityDSL = func() {
10451052
var APIKeyAuth = APIKeySecurity("api_key")
10461053
var TenantKeyAuth = APIKeySecurity("tenant")

0 commit comments

Comments
 (0)