Skip to content

Commit c966b37

Browse files
mromaszewiczclaude
andauthored
fix: use RequiresNilCheck for all params (#2263)
PR #2237 fixed query parameters to use RequiresNilCheck instead of HasOptionalPointer for nil-check guards, but the same bug remained in the header and cookie parameter sections of client.tmpl. This caused optional array params (e.g. []string) to be sent even when nil, because prefer-skip-optional-pointer removes the pointer wrapper and HasOptionalPointer returns false, skipping the nil check entirely. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3a5f803 commit c966b37

File tree

6 files changed

+354
-4
lines changed

6 files changed

+354
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package: issue2238
2+
generate:
3+
models: true
4+
client: true
5+
output-options:
6+
prefer-skip-optional-pointer: true
7+
output: issue2238.gen.go
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package issue2238
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml openapi.yaml

internal/test/issues/issue-2238/issue2238.gen.go

Lines changed: 262 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package issue2238
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestNewGetTestRequest(t *testing.T) {
11+
t.Run("nil header array param is not sent", func(t *testing.T) {
12+
params := GetTestParams{
13+
XTags: nil,
14+
}
15+
16+
req, err := NewGetTestRequest("https://localhost", &params)
17+
require.NoError(t, err)
18+
19+
assert.Empty(t, req.Header.Values("X-Tags"))
20+
})
21+
22+
t.Run("non-nil header array param is sent", func(t *testing.T) {
23+
params := GetTestParams{
24+
XTags: []string{"a", "b"},
25+
}
26+
27+
req, err := NewGetTestRequest("https://localhost", &params)
28+
require.NoError(t, err)
29+
30+
assert.NotEmpty(t, req.Header.Values("X-Tags"))
31+
})
32+
33+
t.Run("nil cookie array param is not sent", func(t *testing.T) {
34+
params := GetTestParams{
35+
Tags: nil,
36+
}
37+
38+
req, err := NewGetTestRequest("https://localhost", &params)
39+
require.NoError(t, err)
40+
41+
assert.Empty(t, req.Cookies())
42+
})
43+
44+
t.Run("non-nil cookie array param is sent", func(t *testing.T) {
45+
params := GetTestParams{
46+
Tags: []string{"a", "b"},
47+
}
48+
49+
req, err := NewGetTestRequest("https://localhost", &params)
50+
require.NoError(t, err)
51+
52+
cookies := req.Cookies()
53+
require.Len(t, cookies, 1)
54+
assert.Equal(t, "tags", cookies[0].Name)
55+
})
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Issue 2238
5+
paths:
6+
/test:
7+
get:
8+
parameters:
9+
- name: X-Tags
10+
in: header
11+
schema:
12+
type: array
13+
items:
14+
type: string
15+
required: false
16+
- name: tags
17+
in: cookie
18+
schema:
19+
type: array
20+
items:
21+
type: string
22+
required: false

pkg/codegen/templates/client.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
236236
{{ if .HeaderParams }}
237237
if params != nil {
238238
{{range $paramIdx, $param := .HeaderParams}}
239-
{{if .HasOptionalPointer}} if params.{{.GoName}} != nil { {{end}}
239+
{{if .RequiresNilCheck}} if params.{{.GoName}} != nil { {{end}}
240240
var headerParam{{$paramIdx}} string
241241
{{if .IsPassThrough}}
242242
headerParam{{$paramIdx}} = {{if .HasOptionalPointer}}*{{end}}params.{{.GoName}}
@@ -256,15 +256,15 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
256256
}
257257
{{end}}
258258
req.Header.Set("{{.ParamName}}", headerParam{{$paramIdx}})
259-
{{if .HasOptionalPointer}}}{{end}}
259+
{{if .RequiresNilCheck}}}{{end}}
260260
{{end}}
261261
}
262262
{{- end }}{{/* if .HeaderParams */}}
263263

264264
{{ if .CookieParams }}
265265
if params != nil {
266266
{{range $paramIdx, $param := .CookieParams}}
267-
{{if .HasOptionalPointer}} if params.{{.GoName}} != nil { {{end}}
267+
{{if .RequiresNilCheck}} if params.{{.GoName}} != nil { {{end}}
268268
var cookieParam{{$paramIdx}} string
269269
{{if .IsPassThrough}}
270270
cookieParam{{$paramIdx}} = {{if .HasOptionalPointer}}*{{end}}params.{{.GoName}}
@@ -288,7 +288,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr
288288
Value:cookieParam{{$paramIdx}},
289289
}
290290
req.AddCookie(cookie{{$paramIdx}})
291-
{{if .HasOptionalPointer}}}{{end}}
291+
{{if .RequiresNilCheck}}}{{end}}
292292
{{ end -}}
293293
}
294294
{{- end }}{{/* if .CookieParams */}}

0 commit comments

Comments
 (0)