Skip to content

[BUG][Go] JSON-content header parameter is expanded into bracketed headers instead of JSON #25084

Description

@AndreyVMarkelov

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Go client does not JSON-serialize a header parameter declared with content: application/json. A generated object model implements MappedNullable, so the generic header formatter expands its fields into bracketed header names. With path: /x, the request contains X-Json-Arg[path]: /x rather than X-Json-Arg: {"path":"/x"}.

openapi-generator version

Reproduced with a local 7.26.0-SNAPSHOT CLI. Whether this is a regression is unknown. The spec below passes openapi-generator-cli validate with no validation issues.

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: JSON header test
  version: 1.0.0
paths:
  /test:
    post:
      operationId: testJsonHeader
      parameters:
        - name: X-Json-Arg
          in: header
          required: true
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HeaderArg'
        - name: X-Plain-Arg
          in: header
          schema:
            type: string
      responses:
        '204':
          description: No content
components:
  schemas:
    HeaderArg:
      type: object
      properties:
        path:
          type: string
Generation Details

Generator: go, with default options.

openapi-generator-cli validate -i json-header-content.yaml
openapi-generator-cli generate -i json-header-content.yaml -g go -o /tmp/go-json-header
Steps to reproduce
  1. Save the OpenAPI definition above as json-header-content.yaml.

  2. Run the validation and generation commands above.

  3. Inspect /tmp/go-json-header/api_default.go. It sends the JSON-content header through the generic formatter:

    parameterAddToHeaderOrQuery(localVarHeaderParams, "X-Json-Arg", r.xJsonArg, "", "")
  4. Inspect /tmp/go-json-header/client.go. The formatter calls ToMap() for the generated HeaderArg and recursively expands the map into bracketed header names. In a generated Go client with HeaderArg.Path set to /x, the outgoing request has:

    X-Json-Arg[path]: /x
    

    There is no X-Json-Arg header. This was verified with a local http.RoundTripper that captured the generated request headers; no server was needed.

Expected behavior

The outgoing request should contain one header:

X-Json-Arg: {"path":"/x"}

The ordinary X-Plain-Arg header should keep its current string formatting.

Related issues/PRs

This is the Go counterpart of the Java issue/PR #25055 / #25056 and Python issue/PR #25067 / #25068.

Suggest a fix

Use CodegenParameter.contentType to identify JSON-content header parameters. Marshal those values with encoding/json before adding them to the header, return marshaling errors, and retain the existing formatter for ordinary headers. Emit the JSON header helper only when needed.

Activity

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

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