Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Commit 87694df

Browse files
mromaszewiczclaude
andcommitted
Fix ParamsTypeName template context bug and normalize variable access
Save ParamsTypeName as a local variable before entering nested range .Bodies loops in client and initiator interface templates, matching the pattern used by all other templates. Without this, $.ParamsTypeName fails because $ refers to the root context (a slice or struct) which has no such field, causing a template error when generating client code for operations with both params and a body. Also normalize all initiator templates to consistently save $.Prefix and $.PrefixLower as $prefix/$prefixLower at the top of the range .Operations loop and use the saved variables throughout, instead of mixing direct root context access ($.Prefix) with saved variables. Fixes #1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 37b100d commit 87694df

5 files changed

Lines changed: 28 additions & 21 deletions

File tree

experimental/internal/codegen/templates/files/client/interface.go.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ type ClientInterface interface {
55
{{- range . }}
66
{{- $opid := .GoOperationID }}
77
{{- $hasParams := .HasParams }}
8+
{{- $paramsTypeName := .ParamsTypeName }}
89
{{- $pathParams := .PathParams }}
910
// {{ $opid }}{{ if .HasBody }}WithBody{{ end }} makes a {{ .Method }} request to {{ .Path }}
10-
{{ $opid }}{{ if .HasBody }}WithBody{{ end }}(ctx context.Context{{ range $pathParams }}, {{ .GoVariableName }} {{ .TypeDecl }}{{ end }}{{ if $hasParams }}, params *{{ .ParamsTypeName }}{{ end }}{{ if .HasBody }}, contentType string, body io.Reader{{ end }}, reqEditors ...RequestEditorFn) (*http.Response, error)
11+
{{ $opid }}{{ if .HasBody }}WithBody{{ end }}(ctx context.Context{{ range $pathParams }}, {{ .GoVariableName }} {{ .TypeDecl }}{{ end }}{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, contentType string, body io.Reader{{ end }}, reqEditors ...RequestEditorFn) (*http.Response, error)
1112
{{- range .Bodies }}
1213
{{- if .IsJSON }}
13-
{{ $opid }}{{ .FuncSuffix }}(ctx context.Context{{ range $pathParams }}, {{ .GoVariableName }} {{ .TypeDecl }}{{ end }}{{ if $hasParams }}, params *{{ $.ParamsTypeName }}{{ end }}, body {{ .GoTypeName }}, reqEditors ...RequestEditorFn) (*http.Response, error)
14+
{{ $opid }}{{ .FuncSuffix }}(ctx context.Context{{ range $pathParams }}, {{ .GoVariableName }} {{ .TypeDecl }}{{ end }}{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}, body {{ .GoTypeName }}, reqEditors ...RequestEditorFn) (*http.Response, error)
1415
{{- end }}
1516
{{- end }}
1617
{{- end }}

experimental/internal/codegen/templates/files/initiator/interface.go.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ type {{ .Prefix }}InitiatorInterface interface {
66
{{- range .Operations }}
77
{{- $opid := .GoOperationID }}
88
{{- $hasParams := .HasParams }}
9-
// {{ $opid }}{{ if .HasBody }}WithBody{{ end }} sends a {{ .Method }} {{ $.PrefixLower }} request
10-
{{ $opid }}{{ if .HasBody }}WithBody{{ end }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ .ParamsTypeName }}{{ end }}{{ if .HasBody }}, contentType string, body io.Reader{{ end }}, reqEditors ...RequestEditorFn) (*http.Response, error)
9+
{{- $paramsTypeName := .ParamsTypeName }}
10+
{{- $prefixLower := $.PrefixLower }}
11+
// {{ $opid }}{{ if .HasBody }}WithBody{{ end }} sends a {{ .Method }} {{ $prefixLower }} request
12+
{{ $opid }}{{ if .HasBody }}WithBody{{ end }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, contentType string, body io.Reader{{ end }}, reqEditors ...RequestEditorFn) (*http.Response, error)
1113
{{- range .Bodies }}
1214
{{- if .IsJSON }}
13-
{{ $opid }}{{ .FuncSuffix }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $.ParamsTypeName }}{{ end }}, body {{ .GoTypeName }}, reqEditors ...RequestEditorFn) (*http.Response, error)
15+
{{ $opid }}{{ .FuncSuffix }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}, body {{ .GoTypeName }}, reqEditors ...RequestEditorFn) (*http.Response, error)
1416
{{- end }}
1517
{{- end }}
1618
{{- end }}

experimental/internal/codegen/templates/files/initiator/methods.go.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
{{- $hasParams := .HasParams }}
88
{{- $paramsTypeName := .ParamsTypeName }}
99
{{- $prefix := $.Prefix }}
10+
{{- $prefixLower := $.PrefixLower }}
1011

11-
// {{ $opid }}{{ if .HasBody }}WithBody{{ end }} sends a {{ .Method }} {{ $.PrefixLower }} request
12+
// {{ $opid }}{{ if .HasBody }}WithBody{{ end }} sends a {{ .Method }} {{ $prefixLower }} request
1213
{{ if .Summary }}// {{ .Summary }}{{ end }}
1314
func (p *{{ $prefix }}Initiator) {{ $opid }}{{ if .HasBody }}WithBody{{ end }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, contentType string, body io.Reader{{ end }}, reqEditors ...RequestEditorFn) (*http.Response, error) {
14-
req, err := New{{ $opid }}{{ $.Prefix }}Request{{ if .HasBody }}WithBody{{ end }}(targetURL{{ if $hasParams }}, params{{ end }}{{ if .HasBody }}, contentType, body{{ end }})
15+
req, err := New{{ $opid }}{{ $prefix }}Request{{ if .HasBody }}WithBody{{ end }}(targetURL{{ if $hasParams }}, params{{ end }}{{ if .HasBody }}, contentType, body{{ end }})
1516
if err != nil {
1617
return nil, err
1718
}
@@ -24,7 +25,7 @@ func (p *{{ $prefix }}Initiator) {{ $opid }}{{ if .HasBody }}WithBody{{ end }}(c
2425
{{- range .Bodies }}
2526
{{- if .IsJSON }}
2627

27-
// {{ $opid }}{{ .FuncSuffix }} sends a {{ $op.Method }} {{ $.PrefixLower }} request with JSON body
28+
// {{ $opid }}{{ .FuncSuffix }} sends a {{ $op.Method }} {{ $prefixLower }} request with JSON body
2829
func (p *{{ $prefix }}Initiator) {{ $opid }}{{ .FuncSuffix }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}, body {{ .GoTypeName }}, reqEditors ...RequestEditorFn) (*http.Response, error) {
2930
req, err := New{{ $opid }}{{ $prefix }}Request{{ .FuncSuffix }}(targetURL{{ if $hasParams }}, params{{ end }}, body)
3031
if err != nil {

experimental/internal/codegen/templates/files/initiator/request_builders.go.tmpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
{{- $headerParams := .HeaderParams }}
1111
{{- $cookieParams := .CookieParams }}
1212
{{- $prefix := $.Prefix }}
13+
{{- $prefixLower := $.PrefixLower }}
1314

1415
{{- /* Generate typed body request builders for JSON bodies */ -}}
1516
{{- range .Bodies }}
1617
{{- if .IsJSON }}
1718

18-
// New{{ $opid }}{{ $prefix }}Request{{ .FuncSuffix }} creates a {{ $op.Method }} request for the {{ $.PrefixLower }} with {{ .ContentType }} body
19+
// New{{ $opid }}{{ $prefix }}Request{{ .FuncSuffix }} creates a {{ $op.Method }} request for the {{ $prefixLower }} with {{ .ContentType }} body
1920
func New{{ $opid }}{{ $prefix }}Request{{ .FuncSuffix }}(targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}, body {{ .GoTypeName }}) (*http.Request, error) {
2021
var bodyReader io.Reader
2122
buf, err := json.Marshal(body)
@@ -28,7 +29,7 @@ func New{{ $opid }}{{ $prefix }}Request{{ .FuncSuffix }}(targetURL string{{ if $
2829
{{- end }}
2930
{{- end }}
3031

31-
// New{{ $opid }}{{ $prefix }}Request{{ if .HasBody }}WithBody{{ end }} creates a {{ .Method }} request for the {{ $.PrefixLower }}{{ if .HasBody }} with any body{{ end }}
32+
// New{{ $opid }}{{ $prefix }}Request{{ if .HasBody }}WithBody{{ end }} creates a {{ .Method }} request for the {{ $prefixLower }}{{ if .HasBody }} with any body{{ end }}
3233
func New{{ $opid }}{{ $prefix }}Request{{ if .HasBody }}WithBody{{ end }}(targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, contentType string, body io.Reader{{ end }}) (*http.Request, error) {
3334
var err error
3435

experimental/internal/codegen/templates/files/initiator/simple.go.tmpl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ func NewSimple{{ .Prefix }}Initiator(opts ...{{ .Prefix }}InitiatorOption) (*Sim
3434
{{- $opid := .GoOperationID }}
3535
{{- $hasParams := .HasParams }}
3636
{{- $paramsTypeName := .ParamsTypeName }}
37+
{{- $prefix := $.Prefix }}
38+
{{- $prefixLower := $.PrefixLower }}
3739

3840
{{- /* Determine if this operation is "simple" - single success content type, single JSON success response */}}
3941
{{- $simpleOp := isSimpleOperation . }}
@@ -43,19 +45,19 @@ func NewSimple{{ .Prefix }}Initiator(opts ...{{ .Prefix }}InitiatorOption) (*Sim
4345
{{- $successType := goTypeForContent $successContent }}
4446
{{- $errorResponse := errorResponseForOperation . }}
4547

46-
// {{ $opid }} sends a {{ .Method }} {{ $.PrefixLower }} request and returns the parsed response.
48+
// {{ $opid }} sends a {{ .Method }} {{ $prefixLower }} request and returns the parsed response.
4749
{{ if .Summary }}// {{ .Summary }}{{ end }}
4850
{{- if $errorResponse }}
4951
{{- $errorContent := index $errorResponse.Contents 0 }}
5052
{{- $errorType := goTypeForContent $errorContent }}
51-
// On success, returns the response body. On HTTP error, returns *{{ $.Prefix }}HttpError[{{ $errorType }}].
52-
func (p *Simple{{ $.Prefix }}Initiator) {{ $opid }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, body {{ (index .Bodies 0).GoTypeName }}{{ end }}, reqEditors ...RequestEditorFn) ({{ $successType }}, error) {
53+
// On success, returns the response body. On HTTP error, returns *{{ $prefix }}HttpError[{{ $errorType }}].
54+
func (p *Simple{{ $prefix }}Initiator) {{ $opid }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, body {{ (index .Bodies 0).GoTypeName }}{{ end }}, reqEditors ...RequestEditorFn) ({{ $successType }}, error) {
5355
var result {{ $successType }}
5456
{{- if .HasBody }}
5557
{{- $defaultBody := index .Bodies 0 }}
56-
resp, err := p.{{ $.Prefix }}Initiator.{{ $opid }}{{ $defaultBody.FuncSuffix }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, body, reqEditors...)
58+
resp, err := p.{{ $prefix }}Initiator.{{ $opid }}{{ $defaultBody.FuncSuffix }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, body, reqEditors...)
5759
{{- else }}
58-
resp, err := p.{{ $.Prefix }}Initiator.{{ $opid }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, reqEditors...)
60+
resp, err := p.{{ $prefix }}Initiator.{{ $opid }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, reqEditors...)
5961
{{- end }}
6062
if err != nil {
6163
return result, err
@@ -77,21 +79,21 @@ func (p *Simple{{ $.Prefix }}Initiator) {{ $opid }}(ctx context.Context, targetU
7779
// Parse error response
7880
var errBody {{ $errorType }}
7981
_ = json.Unmarshal(rawBody, &errBody) // Best effort parse
80-
return result, &{{ $.Prefix }}HttpError[{{ $errorType }}]{
82+
return result, &{{ $prefix }}HttpError[{{ $errorType }}]{
8183
StatusCode: resp.StatusCode,
8284
Body: errBody,
8385
RawBody: rawBody,
8486
}
8587
}
8688
{{- else }}
87-
// On success, returns the response body. On HTTP error, returns *{{ $.Prefix }}HttpError[struct{}].
88-
func (p *Simple{{ $.Prefix }}Initiator) {{ $opid }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, body {{ (index .Bodies 0).GoTypeName }}{{ end }}, reqEditors ...RequestEditorFn) ({{ $successType }}, error) {
89+
// On success, returns the response body. On HTTP error, returns *{{ $prefix }}HttpError[struct{}].
90+
func (p *Simple{{ $prefix }}Initiator) {{ $opid }}(ctx context.Context, targetURL string{{ if $hasParams }}, params *{{ $paramsTypeName }}{{ end }}{{ if .HasBody }}, body {{ (index .Bodies 0).GoTypeName }}{{ end }}, reqEditors ...RequestEditorFn) ({{ $successType }}, error) {
8991
var result {{ $successType }}
9092
{{- if .HasBody }}
9193
{{- $defaultBody := index .Bodies 0 }}
92-
resp, err := p.{{ $.Prefix }}Initiator.{{ $opid }}{{ $defaultBody.FuncSuffix }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, body, reqEditors...)
94+
resp, err := p.{{ $prefix }}Initiator.{{ $opid }}{{ $defaultBody.FuncSuffix }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, body, reqEditors...)
9395
{{- else }}
94-
resp, err := p.{{ $.Prefix }}Initiator.{{ $opid }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, reqEditors...)
96+
resp, err := p.{{ $prefix }}Initiator.{{ $opid }}(ctx, targetURL{{ if $hasParams }}, params{{ end }}, reqEditors...)
9597
{{- end }}
9698
if err != nil {
9799
return result, err
@@ -111,7 +113,7 @@ func (p *Simple{{ $.Prefix }}Initiator) {{ $opid }}(ctx context.Context, targetU
111113
}
112114

113115
// No typed error response defined
114-
return result, &{{ $.Prefix }}HttpError[struct{}]{
116+
return result, &{{ $prefix }}HttpError[struct{}]{
115117
StatusCode: resp.StatusCode,
116118
RawBody: rawBody,
117119
}

0 commit comments

Comments
 (0)