Bug Report Checklist
Description
When a header parameter is declared with content: application/json, the generated C# client serializes it through ClientUtils.ParameterToString(...). For a model object, ParameterToString falls through to Convert.ToString(obj, CultureInfo.InvariantCulture), which calls the model's ToString() — the generated debug representation — instead of JSON.
This is the C# counterpart of the Java issue #25055 and the Python issue #25067. It affects all C# client libraries, since they share this serialization path for header parameters: restsharp (default), httpclient, generichost, and unityWebRequest.
openapi-generator version
7.25.0 and master (7.26.0-SNAPSHOT). The code path has existed for a long time.
OpenAPI declaration file content or url
openapi: 3.1.0
info:
title: JSON header repro
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'
responses:
'204':
description: OK
components:
schemas:
HeaderArg:
type: object
properties:
path:
type: string
Generation Details
openapi-generator-cli generate -i json-header-content.yaml -g csharp --additional-properties=library=restsharp -o /tmp/csharp-json-header
Steps to reproduce
- Generate the client from the spec above.
- Inspect the generated
DefaultApi.cs. The header is added as:
localVarRequestOptions.HeaderParameters.Add("X-Json-Arg", Org.OpenAPITools.Client.ClientUtils.ParameterToString(xJsonArg)); // header parameter
- Call the operation with
new HeaderArg { Path = "/x" }.
Expected: X-Json-Arg: {"path":"/x"}
Actual: the model's debug ToString() output, e.g.:
class HeaderArg {
Path: /x
}
Reproducible without making any HTTP request (the serialized value is wrong at construction time).
Root cause
DefaultCodegen already preserves the media type in CodegenParameter.contentType, and there is an existing isJsonMimeType(...) helper plus the precedent of queryIsJsonMimeType for query parameters. Header parameters have no equivalent handling, so JSON-content headers fall back to ParameterToString → Convert.ToString → ToString().
Suggested fix
Mirror the existing queryIsJsonMimeType mechanism: add a shared headerIsJsonMimeType flag (set in DefaultCodegen.fromParameter) and, in the C# templates, serialize JSON-content headers as JSON (ASCII-safe) instead of via ParameterToString, keeping existing behavior for regular headers.
Related: #25055 (Java), #25056 (Java PR), #25067 (Python), #25068 (Python PR).
Bug Report Checklist
Description
When a header parameter is declared with
content: application/json, the generated C# client serializes it throughClientUtils.ParameterToString(...). For a model object,ParameterToStringfalls through toConvert.ToString(obj, CultureInfo.InvariantCulture), which calls the model'sToString()— the generated debug representation — instead of JSON.This is the C# counterpart of the Java issue #25055 and the Python issue #25067. It affects all C# client libraries, since they share this serialization path for header parameters:
restsharp(default),httpclient,generichost, andunityWebRequest.openapi-generator version
7.25.0 and master (7.26.0-SNAPSHOT). The code path has existed for a long time.
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
DefaultApi.cs. The header is added as:new HeaderArg { Path = "/x" }.Expected:
X-Json-Arg: {"path":"/x"}Actual: the model's debug
ToString()output, e.g.:Reproducible without making any HTTP request (the serialized value is wrong at construction time).
Root cause
DefaultCodegenalready preserves the media type inCodegenParameter.contentType, and there is an existingisJsonMimeType(...)helper plus the precedent ofqueryIsJsonMimeTypefor query parameters. Header parameters have no equivalent handling, so JSON-content headers fall back toParameterToString→Convert.ToString→ToString().Suggested fix
Mirror the existing
queryIsJsonMimeTypemechanism: add a sharedheaderIsJsonMimeTypeflag (set inDefaultCodegen.fromParameter) and, in the C# templates, serialize JSON-content headers as JSON (ASCII-safe) instead of viaParameterToString, keeping existing behavior for regular headers.Related: #25055 (Java), #25056 (Java PR), #25067 (Python), #25068 (Python PR).