Skip to content

Commit 5cbdf63

Browse files
authored
Add option to use http.Header.Set when setting headers in Go client (#24791)
* add option to use http.Header.Set when setting headers in go client * upadte samples * update tests * use tabs * update tests
1 parent 1edd5f2 commit 5cbdf63

20 files changed

Lines changed: 36 additions & 16 deletions

File tree

‎docs/generators/go.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
3232
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
3333
|useDefaultValuesForRequiredVars|Use default values for required variables when available| |false|
34+
|useHttpHeaderSet|When setting HTTP request headers, use http.Header.Set with canonicalized header names| |false|
3435
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
3536
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
3637
|withGoMod|Generate go.mod and go.sum| |true|

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
6363
protected boolean generateUnmarshalJSON = true;
6464
@Setter
6565
protected boolean useDefaultValuesForRequiredVars = false;
66+
@Setter
67+
protected boolean useHttpHeaderSet = false;
6668

6769
@Setter
6870
protected String packageName = "openapi";

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
6565
public static final String MODEL_FILE_FOLDER = "modelFileFolder";
6666
public static final String WITH_GO_MOD = "withGoMod";
6767
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
68+
public static final String USE_HTTP_HEADER_SET = "useHttpHeaderSet";
6869
public static final String IMPORT_VALIDATOR = "importValidator";
6970
@Setter protected String goImportAlias = "openapiclient";
7071
protected boolean isGoSubmodule = false;
@@ -138,6 +139,7 @@ public GoClientCodegen() {
138139
cliOptions.add(CliOption.newBoolean(WITH_AWSV4_SIGNATURE, "whether to include AWS v4 signature support"));
139140
cliOptions.add(CliOption.newBoolean(GENERATE_INTERFACES, "Generate interfaces for api classes"));
140141
cliOptions.add(CliOption.newBoolean(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "Use default values for required variables when available"));
142+
cliOptions.add(CliOption.newBoolean(USE_HTTP_HEADER_SET, "When setting HTTP request headers, use http.Header.Set with canonicalized header names"));
141143

142144
// option to change the order of form/body parameter
143145
cliOptions.add(CliOption.newBoolean(
@@ -276,6 +278,11 @@ public void processOpts() {
276278
additionalProperties.put(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, useDefaultValuesForRequiredVars);
277279
}
278280

281+
if (additionalProperties.containsKey(USE_HTTP_HEADER_SET)) {
282+
setUseHttpHeaderSet(Boolean.parseBoolean(additionalProperties.get(USE_HTTP_HEADER_SET).toString()));
283+
additionalProperties.put(USE_HTTP_HEADER_SET, useHttpHeaderSet);
284+
}
285+
279286
// Generate the 'signing.py' module, but only if the 'HTTP signature' security scheme is specified in the OAS.
280287
Map<String, SecurityScheme> securitySchemeMap = openAPI != null ?
281288
(openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null;

‎modules/openapi-generator/src/main/resources/go/client.mustache‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,12 @@ func (c *APIClient) prepareRequest(
416416
if len(headerParams) > 0 {
417417
headers := http.Header{}
418418
for h, v := range headerParams {
419+
{{#useHttpHeaderSet}}
419420
headers.Set(h, v)
421+
{{/useHttpHeaderSet}}
422+
{{^useHttpHeaderSet}}
423+
headers[h] = []string{v}
424+
{{/useHttpHeaderSet}}
420425
}
421426
localVarRequest.Header = headers
422427
}

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ protected void verifyOptions() {
5656
verify(clientCodegen).setGenerateUnmarshalJSON(GoClientOptionsProvider.GENERATE_UNMARSHAL_JSON_VALUE);
5757
verify(clientCodegen).setUseDefaultValuesForRequiredVars(GoClientOptionsProvider.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE);
5858
verify(clientCodegen).setEnumUnknownDefaultCase(Boolean.parseBoolean(GoClientOptionsProvider.ENUM_UNKNOWN_DEFAULT_CASE_VALUE));
59+
verify(clientCodegen).setUseHttpHeaderSet(GoClientOptionsProvider.USE_HTTP_HEADER_SET);
60+
5961
}
6062
}

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class GoClientOptionsProvider implements OptionsProvider {
4141
public static final boolean GENERATE_UNMARSHAL_JSON_VALUE = true;
4242
public static final boolean USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE = true;
4343
public static final String ENUM_UNKNOWN_DEFAULT_CASE_VALUE = "false";
44+
public static final boolean USE_HTTP_HEADER_SET = true;
45+
4446

4547
@Override
4648
public String getLanguage() {
@@ -68,6 +70,7 @@ public Map<String, String> createOptions() {
6870
.put("structPrefix", "true")
6971
.put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true")
7072
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
73+
.put("useHttpHeaderSet", "true")
7174
.build();
7275
}
7376

‎samples/client/echo_api/go-external-refs/client.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎samples/client/echo_api/go/client.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎samples/client/others/go/allof_multiple_ref_and_discriminator/client.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎samples/client/others/go/issue_20079_go_regex_wrongly_translated/client.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)