Bug Report Checklist
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
-
Save the OpenAPI definition above as json-header-content.yaml.
-
Run the validation and generation commands above.
-
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, "", "")
-
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:
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.
Bug Report Checklist
Description
The Go client does not JSON-serialize a header parameter declared with
content: application/json. A generated object model implementsMappedNullable, so the generic header formatter expands its fields into bracketed header names. Withpath: /x, the request containsX-Json-Arg[path]: /xrather thanX-Json-Arg: {"path":"/x"}.openapi-generator version
Reproduced with a local
7.26.0-SNAPSHOTCLI. Whether this is a regression is unknown. The spec below passesopenapi-generator-cli validatewith no validation issues.OpenAPI declaration file content or url
Generation Details
Generator:
go, with default options.Steps to reproduce
Save the OpenAPI definition above as
json-header-content.yaml.Run the validation and generation commands above.
Inspect
/tmp/go-json-header/api_default.go. It sends the JSON-content header through the generic formatter:Inspect
/tmp/go-json-header/client.go. The formatter callsToMap()for the generatedHeaderArgand recursively expands the map into bracketed header names. In a generated Go client withHeaderArg.Pathset to/x, the outgoing request has:There is no
X-Json-Argheader. This was verified with a localhttp.RoundTripperthat captured the generated request headers; no server was needed.Expected behavior
The outgoing request should contain one header:
The ordinary
X-Plain-Argheader 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.contentTypeto identify JSON-content header parameters. Marshal those values withencoding/jsonbefore adding them to the header, return marshaling errors, and retain the existing formatter for ordinary headers. Emit the JSON header helper only when needed.