Repository navigation
[BUG][Go] JSON-content header parameter is expanded into bracketed headers instead of JSON #25084
Description
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: stringGeneration 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-headerSteps 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 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:X-Json-Arg[path]: /xThere 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:
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
[-][BUG][Go] JSON-content header parameter is formatted as a struct placeholder[/-][+][BUG][Go] JSON-content header parameter is expanded into bracketed headers instead of JSON[/+]