Skip to content

fix(go): fix Go client to normalize request header keys using Header.Set - #24766

Merged
wing328 merged 5 commits into
OpenAPITools:masterfrom
Wuchieh:master
Aug 27, 2026
Merged

fix(go): fix Go client to normalize request header keys using Header.Set#24766
wing328 merged 5 commits into
OpenAPITools:masterfrom
Wuchieh:master

Conversation

@Wuchieh

@Wuchieh Wuchieh commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Use http.Header.Set when writing headerParams to http.Header in the generated Go client.

Previously, generated code directly assigned values to the underlying http.Header map:

headers[h] = []string{v}

This bypasses the header name canonicalization provided by http.Header.Set and may result in inconsistent header casing or duplicate logical headers.

This change replaces direct map assignment with:

headers.Set(h, v)

Changes

  • Use http.Header.Set when writing headerParams to http.Header.
  • Ensure request header names are canonicalized consistently.
  • Avoid inconsistent casing or duplicate logical headers caused by direct map assignment.
  • Add/update regression tests to cover the expected header handling behavior.

Related Issue

Fixes #24765

PR checklist


Summary by cubic

Normalizes request header keys in generated Go clients by using http.Header.Set instead of direct map assignment. This canonicalizes names and prevents duplicate logical headers.

  • If headerParams contains case-variant duplicates, only one canonical header is sent; which value is kept depends on map iteration order.
  • Regenerates Go samples and updates sample dependency snapshots (normalize go.mod Go versions to X.Y.0 and add missing go.sum entries like terraform-plugin-testing).

Written for commit 8e27634. Summary will update on new commits.

Review in cubic

- Use Set when writing headerParams to http.Header to automatically apply canonical key formatting.
- Avoid inconsistent casing or duplicate header issues caused by direct map assignment.
@Wuchieh

Wuchieh commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

@lwj5 Please review it.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Re-trigger cubic

@wing328

wing328 commented Aug 25, 2026

Copy link
Copy Markdown
Member

please follow step 3 to update the samples so that CI can verify the change

cc @antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @jirikuncar (2021/01) @ph4r5h4d (2021/04) @lwj5 (2023/04)

@wing328

wing328 commented Aug 25, 2026

Copy link
Copy Markdown
Member

please review the build failures when you've time, e.g. https://github.com/OpenAPITools/openapi-generator/actions/runs/32814668511/job/97700780542?pr=24766

@Wuchieh

Wuchieh commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

please review the build failures when you've time, e.g. https://github.com/OpenAPITools/openapi-generator/actions/runs/32814668511/job/97700780542?pr=24766

Okay, I'll take a look later to see what the issue is.

Wuchieh added 2 commits August 25, 2026 14:53
…templates

- Update the go.mod Go version format for multiple Go client/server templates (e.g., 1.23 → 1.23.0, 1.25 → 1.25.0).
- Add missing terraform-plugin-testing checksums to the Terraform templates' go.sum.
- Sync checksums for indirect dependencies such as testify, x/sys, and x/text.
- Add go.sum files for templates including echo-server, chi-server, and gin-api-server-interface-only.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 24 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/echo_api/go/client.go">

<violation number="1">
P2: This PR changes properly gofmt-formatted Go code into gofmt-invalid code: spaces inside call/signature parens, `var param,ok`, `dataMap,err :=`, `for i:=0;i<len;...`, and an over-indented `case`. The same non-gofmt text is now in the `client.mustache` template, so every generated Go client ships this. Keep generated Go output gofmt-clean (run `gofmt -w` on the template and regenerate, or write the template in gofmt style) or CI/`go vet`/gofmt checks and downstream users on the Go codebase will hit formatting failures.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@@ -420,7 +420,7 @@ func (c *APIClient) prepareRequest(
if len(headerParams) > 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This PR changes properly gofmt-formatted Go code into gofmt-invalid code: spaces inside call/signature parens, var param,ok, dataMap,err :=, for i:=0;i<len;..., and an over-indented case. The same non-gofmt text is now in the client.mustache template, so every generated Go client ships this. Keep generated Go output gofmt-clean (run gofmt -w on the template and regenerate, or write the template in gofmt style) or CI/go vet/gofmt checks and downstream users on the Go codebase will hit formatting failures.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/echo_api/go/client.go, line 144:

<comment>This PR changes properly gofmt-formatted Go code into gofmt-invalid code: spaces inside call/signature parens, `var param,ok`, `dataMap,err :=`, `for i:=0;i<len;...`, and an over-indented `case`. The same non-gofmt text is now in the `client.mustache` template, so every generated Go client ships this. Keep generated Go output gofmt-clean (run `gofmt -w` on the template and regenerate, or write the template in gofmt style) or CI/`go vet`/gofmt checks and downstream users on the Go codebase will hit formatting failures.</comment>

<file context>
@@ -140,19 +141,19 @@ func typeCheckParameter(obj interface{}, expected string, name string) error {
 }
 
-func parameterValueToString(obj interface{}, key string) string {
+func parameterValueToString( obj interface{}, key string ) string {
 	if reflect.TypeOf(obj).Kind() != reflect.Ptr {
 		if actualObj, ok := obj.(interface{ GetActualInstanceValue() interface{} }); ok {
</file context>
Suggested change
if len(headerParams) > 0 {
func parameterValueToString(obj interface{}, key string) string {

@Wuchieh

Wuchieh commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

@antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @jirikuncar (2021/01) @ph4r5h4d (2021/04) @lwj5 (2023/04)

Please review it.

@Wuchieh

Wuchieh commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

@wing328 Hi, I found that the go test configuration in this project was incorrect, which caused the previous CI checks to fail. Could you please review and approve it? Thank you.

Incorrect files:

  • samples/openapi3/client/petstore/go/auth_test.go
  • samples/client/petstore/go/auth_test.go

Comment thread samples/client/petstore/go/auth_test.go
@wing328 wing328 added this to the 7.26.0 milestone Aug 27, 2026
@wing328
wing328 merged commit 091a02f into OpenAPITools:master Aug 27, 2026
40 checks passed
@wing328

wing328 commented Aug 27, 2026

Copy link
Copy Markdown
Member

UPDATE: merged #24791 to add an option useHttpHeaderSet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][GO] Generated client does not use http.Header.Set for request headers

2 participants