Skip to content

Commit 2e1ed1c

Browse files
artnanArt Nanakorn
authored andcommitted
fix(java): add header parameter handling for google-api-client
Header parameters defined in OpenAPI specs were being accepted as method parameters but never actually added to the HTTP request. Fixes #22457
1 parent 08858a9 commit 2e1ed1c

File tree

1 file changed

+27
-3
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/google-api-client

1 file changed

+27
-3
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/google-api-client/api.mustache

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ public class {{classname}} {
123123
GenericUrl genericUrl = new GenericUrl(localVarUrl);
124124

125125
HttpContent content = {{#isBodyAllowed}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
126-
return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
126+
com.google.api.client.http.HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content);
127+
{{#headerParams}}
128+
if ({{paramName}} != null) {
129+
httpRequest.getHeaders().set("{{baseName}}", {{paramName}});
130+
}
131+
{{/headerParams}}
132+
return httpRequest.execute();
127133
}{{#bodyParam}}
128134

129135
{{#isDeprecated}}
@@ -159,7 +165,13 @@ public class {{classname}} {
159165
HttpContent content = {{#bodyParam}}{{paramName}} == null ?
160166
apiClient.new JacksonJsonHttpContent(null) :
161167
new InputStreamContent(mediaType == null ? Json.MEDIA_TYPE : mediaType, {{paramName}}){{/bodyParam}};
162-
return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
168+
com.google.api.client.http.HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content);
169+
{{#headerParams}}
170+
if ({{paramName}} != null) {
171+
httpRequest.getHeaders().set("{{baseName}}", {{paramName}});
172+
}
173+
{{/headerParams}}
174+
return httpRequest.execute();
163175
}{{/bodyParam}}
164176

165177
{{#isDeprecated}}
@@ -201,7 +213,19 @@ public class {{classname}} {
201213
GenericUrl genericUrl = new GenericUrl(localVarUrl);
202214

203215
HttpContent content = {{#isBodyAllowed}}{{#bodyParam}}apiClient.new JacksonJsonHttpContent({{paramName}}){{/bodyParam}}{{^bodyParam}}new EmptyContent(){{/bodyParam}}{{/isBodyAllowed}}{{^isBodyAllowed}}null{{/isBodyAllowed}};
204-
return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();
216+
com.google.api.client.http.HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content);
217+
// Note: Header params passed via 'params' map are handled below
218+
for (Map.Entry<String, Object> entry: params.entrySet()) {
219+
String key = entry.getKey();
220+
Object value = entry.getValue();
221+
// Check if this is a header parameter by name
222+
{{#headerParams}}
223+
if ("{{baseName}}".equals(key) && value != null) {
224+
httpRequest.getHeaders().set(key, value);
225+
}
226+
{{/headerParams}}
227+
}
228+
return httpRequest.execute();
205229
}
206230

207231

0 commit comments

Comments
 (0)