Skip to content

[BUG] [java][google-api-client] Header parameters are accepted but not added to HTTP requests #22457

@artnan

Description

@artnan

Description

This bug was discovered by Claude (Anthropic's AI assistant) while I was investigating why header parameters were being silently ignored in a generated Java goolge-api-client.

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?

OpenAPI Generator Version

7.15.0

OpenAPI Declaration File Content

openapi: 3.1.0
info:
  title: Sample API
  version: '1.0'
paths:
  /v1/resource:
    post:
      operationId: createResource
      parameters:
        - name: X-Request-ID
          in: header
          description: Unique request identifier for tracing
          required: false
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateRequest'
        required: true
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateResponse'
components:
  schemas:
    CreateRequest:
      type: object
      properties:
        name:
          type: string
    CreateResponse:
      type: object
      properties:
        id:
          type: string

Generation Details

openapi-generator generate \
  -g java \
  --library google-api-client \
  -i spec.yaml \
  -o ./generated

Actual Behavior (Bug)

The generated DefaultApi.java accepts the header parameter xRequestID but never adds it to the HTTP request:

public HttpResponse createResourceForHttpResponse(
    @javax.annotation.Nullable String xRequestID,  // Parameter accepted
    @javax.annotation.Nonnull CreateRequest createRequest
) throws IOException {
    // ... validation code ...

    // create a map of path variables
    final Map<String, Object> uriVariables = new HashMap<String, Object>();
    UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/v1/resource");

    String localVarUrl = uriBuilder.build().toString();
    GenericUrl genericUrl = new GenericUrl(localVarUrl);

    HttpContent content = apiClient.new JacksonJsonHttpContent(createRequest);

    // BUG: xRequestID is completely ignored here!
    // The header is never added to the request
    return apiClient.getHttpRequestFactory()
        .buildRequest(HttpMethods.POST, genericUrl, content)
        .execute();
}

The same issue exists in:

  1. The main ForHttpResponse method with typed parameters
  2. The InputStream variant method
  3. The Map<String, Object> params variant method

Expected Behavior

Header parameters should be added to the HttpRequest before calling execute():

public HttpResponse createResourceForHttpResponse(
    @javax.annotation.Nullable String xRequestID,
    @javax.annotation.Nonnull CreateRequest createRequest
) throws IOException {
    // ... validation code ...

    HttpContent content = apiClient.new JacksonJsonHttpContent(createRequest);

    // Build the request
    com.google.api.client.http.HttpRequest httpRequest = apiClient
        .getHttpRequestFactory()
        .buildRequest(HttpMethods.POST, genericUrl, content);

    // ADD HEADER PARAMETERS (this is missing!)
    if (xRequestID != null) {
        httpRequest.getHeaders().set("X-Request-ID", xRequestID);
    }

    return httpRequest.execute();
}

Root Cause

In the template file modules/openapi-generator/src/main/resources/Java/libraries/google-api-client/api.mustache, the {{#headerParams}} block is never used to add headers to the request.

Suggested Fix

Update api.mustache to include header parameter handling. Replace:

HttpContent content = {{#isBodyAllowed}}...{{/isBodyAllowed}};
return apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content).execute();

With:

HttpContent content = {{#isBodyAllowed}}...{{/isBodyAllowed}};
com.google.api.client.http.HttpRequest httpRequest = apiClient.getHttpRequestFactory().buildRequest(HttpMethods.{{httpMethod}}, genericUrl, content);
{{#headerParams}}
if ({{paramName}} != null) {
    httpRequest.getHeaders().set("{{baseName}}", {{paramName}});
}
{{/headerParams}}
return httpRequest.execute();

This fix needs to be applied in 3 locations within the template:

  1. Main ForHttpResponse method (~line 126)
  2. InputStream variant method (~line 162)
  3. Params map variant method (~line 204)

Related Issues

None found - this appears to be an unreported bug.

Impact

Any OpenAPI spec with in: header parameters will generate non-functional code when using the google-api-client library. The generated methods accept the header parameters but silently discard them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions