Skip to content

Add option to use http.Header.Set when setting headers in Go client - #24791

Merged
wing328 merged 5 commits into
masterfrom
followup-24766
Aug 27, 2026
Merged

Add option to use http.Header.Set when setting headers in Go client#24791
wing328 merged 5 commits into
masterfrom
followup-24766

Conversation

@wing328

@wing328 wing328 commented Aug 27, 2026

Copy link
Copy Markdown
Member

a follow up pr to #24766

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds a useHttpHeaderSet option to the Go client generator that changes how prepareRequest assigns headers. The new default preserves header-name casing from the API spec instead of canonicalizing; set useHttpHeaderSet=true to restore the previous http.Header.Set behavior.

Migration

  • Existing generated clients that relied on canonicalized headers must regenerate with useHttpHeaderSet=true.

Written for commit c0146b6. Summary will update on new commits.

Review in cubic

@wing328
wing328 marked this pull request as ready for review August 27, 2026 08:38

@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.

2 issues found across 17 files

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/openapi3/client/petstore/go/go-petstore/client.go">

<violation number="1" location="samples/openapi3/client/petstore/go/go-petstore/client.go:426">
P2: When HTTP signature auth signs `x-api-key`, this assignment stores that casing, but `SignRequest` indexes `X-Api-Key` and returns an error. Use a case-insensitive lookup there or preserve a canonical key for signing.</violation>
</file>

<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java:281">
P2: The new useHttpHeaderSet option has no test coverage. Every other Go generator option is exercised in GoClientOptionsTest via GoClientOptionsProvider, but this one is skipped, so a regression in wiring (e.g. the option not being parsed or written back) would pass the build silently. Add USE_HTTP_HEADER_SET to GoClientOptionsProvider, verify setUseHttpHeaderSet in GoClientOptionsTest, and add a test asserting the generated prepareRequest uses headers.Set when true and direct assignment when false.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

headers := http.Header{}
for h, v := range headerParams {
headers.Set(h, v)
headers[h] = []string{v}

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: When HTTP signature auth signs x-api-key, this assignment stores that casing, but SignRequest indexes X-Api-Key and returns an error. Use a case-insensitive lookup there or preserve a canonical key for signing.

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

<comment>When HTTP signature auth signs `x-api-key`, this assignment stores that casing, but `SignRequest` indexes `X-Api-Key` and returns an error. Use a case-insensitive lookup there or preserve a canonical key for signing.</comment>

<file context>
@@ -423,7 +423,7 @@ func (c *APIClient) prepareRequest(
 		headers := http.Header{}
 		for h, v := range headerParams {
-			headers.Set(h, v)
+		    headers[h] = []string{v}
 		}
 		localVarRequest.Header = headers
</file context>

additionalProperties.put(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, useDefaultValuesForRequiredVars);
}

if (additionalProperties.containsKey(USE_HTTP_HEADER_SET)) {

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: The new useHttpHeaderSet option has no test coverage. Every other Go generator option is exercised in GoClientOptionsTest via GoClientOptionsProvider, but this one is skipped, so a regression in wiring (e.g. the option not being parsed or written back) would pass the build silently. Add USE_HTTP_HEADER_SET to GoClientOptionsProvider, verify setUseHttpHeaderSet in GoClientOptionsTest, and add a test asserting the generated prepareRequest uses headers.Set when true and direct assignment when false.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java, line 281:

<comment>The new useHttpHeaderSet option has no test coverage. Every other Go generator option is exercised in GoClientOptionsTest via GoClientOptionsProvider, but this one is skipped, so a regression in wiring (e.g. the option not being parsed or written back) would pass the build silently. Add USE_HTTP_HEADER_SET to GoClientOptionsProvider, verify setUseHttpHeaderSet in GoClientOptionsTest, and add a test asserting the generated prepareRequest uses headers.Set when true and direct assignment when false.</comment>

<file context>
@@ -276,6 +278,11 @@ public void processOpts() {
             additionalProperties.put(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, useDefaultValuesForRequiredVars);
         }
 
+        if (additionalProperties.containsKey(USE_HTTP_HEADER_SET)) {
+            setUseHttpHeaderSet(Boolean.parseBoolean(additionalProperties.get(USE_HTTP_HEADER_SET).toString()));
+            additionalProperties.put(USE_HTTP_HEADER_SET, useHttpHeaderSet);
</file context>

Comment thread modules/openapi-generator/src/main/resources/go/client.mustache Outdated

@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 2 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="modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java:73">
P3: The new `USE_HTTP_HEADER_SET` constant is never referenced in `createOptions()`, which hardcodes the literal "true" instead. The test asserts `setUseHttpHeaderSet(USE_HTTP_HEADER_SET)`, so if the constant value is ever changed the option map and the test silently diverge. Use `String.valueOf(USE_HTTP_HEADER_SET)` (or the key constant `GoClientCodegen.USE_HTTP_HEADER_SET`) to keep them in sync.</violation>
</file>

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

Re-trigger cubic

.put("structPrefix", "true")
.put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true")
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
.put("useHttpHeaderSet", "true")

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.

P3: The new USE_HTTP_HEADER_SET constant is never referenced in createOptions(), which hardcodes the literal "true" instead. The test asserts setUseHttpHeaderSet(USE_HTTP_HEADER_SET), so if the constant value is ever changed the option map and the test silently diverge. Use String.valueOf(USE_HTTP_HEADER_SET) (or the key constant GoClientCodegen.USE_HTTP_HEADER_SET) to keep them in sync.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java, line 73:

<comment>The new `USE_HTTP_HEADER_SET` constant is never referenced in `createOptions()`, which hardcodes the literal "true" instead. The test asserts `setUseHttpHeaderSet(USE_HTTP_HEADER_SET)`, so if the constant value is ever changed the option map and the test silently diverge. Use `String.valueOf(USE_HTTP_HEADER_SET)` (or the key constant `GoClientCodegen.USE_HTTP_HEADER_SET`) to keep them in sync.</comment>

<file context>
@@ -68,6 +70,7 @@ public Map<String, String> createOptions() {
                 .put("structPrefix", "true")
                 .put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true")
                 .put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
+                .put("useHttpHeaderSet", "true")
                 .build();
     }
</file context>
Suggested change
.put("useHttpHeaderSet", "true")
.put("useHttpHeaderSet", String.valueOf(USE_HTTP_HEADER_SET))

@wing328
wing328 merged commit 5cbdf63 into master Aug 27, 2026
27 checks passed
@wing328
wing328 deleted the followup-24766 branch August 27, 2026 10:07
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.

1 participant