[csharp] Serialize header parameters with content: application/json as JSON
Fixes #25082
Problem
A header parameter declared with content: application/json was serialized in the generated C# client through ClientUtils.ParameterToString(...). For a model object, ParameterToString falls through to Convert.ToString(obj, CultureInfo.InvariantCulture), which emits the model's debug ToString():
class HeaderArg {
Path: /x
}
instead of JSON ({"path":"/x"}). This affects all C# client libraries, since they share this header serialization path: restsharp (default), httpclient, generichost, and unityWebRequest.
This is the C# counterpart of the Java bug #25055 (PR #25056) and the Python bug #25067 (PR #25068).
Fix
Mirror the existing queryIsJsonMimeType mechanism (which already does exactly this for query params):
- Engine: add a shared
CodegenParameter.headerIsJsonMimeTypeflag, set inDefaultCodegen.fromParametervia the already-existingisJsonMimeType(contentType)helper. (This is the same small engine addition proposed in #25056; if that merges first, this can be de-duped.) - Templates: the C#
api.mustachetemplates serialize JSON-content headers as JSON instead of viaParameterToString, keeping existing behavior for regular headers:restsharp,httpclient,unityWebRequest(Newtonsoft): a new ASCII-safeClientUtils.ParameterToJsonString(...)helper (usesStringEscapeHandling.EscapeNonAscii).generichost(System.Text.Json):JsonSerializer.Serialize(value, _jsonSerializerOptions), consistent with how it already serializes request bodies.
Generated code for a JSON-content header now looks like:
// restsharp / httpclient / unityWebRequest
localVarRequestOptions.HeaderParameters.Add("X-Json-Arg", Org.OpenAPITools.Client.ClientUtils.ParameterToJsonString(xJsonArg)); // header parameter
// generichost
httpRequestMessageLocalVar.Headers.Add("X-Json-Arg", JsonSerializer.Serialize(xJsonArg, _jsonSerializerOptions));
while a regular header is unchanged (ParameterToString(xPlainArg)).
Tests
- Added
3_1/csharp/json-header-content.yaml(a JSON-content header + a plain header). - Added
testJsonContentHeaderUsesJsonSerialization(restsharp) andtestJsonContentHeaderUsesJsonSerializationGenericHosttoCSharpClientCodegenTest, asserting the JSON header is JSON-serialized and the plain header is not.
Verification
CSharpClientCodegenTest— both new tests pass (Tests run: 2, Failures: 0).- Generated a client from the fixture for all four libraries and confirmed the header serialization code;
restsharp,httpclient, andgenerichostoutputs compile withdotnet build(0 errors).unityWebRequestoutput generates the expected code (requires the Unity SDK to compile). - Regenerated samples: the only change is the new
ClientUtils.ParameterToJsonStringhelper added to the Newtonsoft-based generated clients.
PR checklist
- Read the contribution guidelines.
- Ran
./mvnw clean packagefor the generator and regenerated the affected samples with./bin/generate-samples.sh bin/configs/csharp*.yaml; committed all changed files. - Targeting
master.
cc C# technical committee: @mandrean @shibayan @Blackclaws @lucamazzanti
Summary by cubic
Fixes the C# generator so header parameters declared with content: application/json serialize as JSON instead of the model's debug ToString() output, which affected all C# client libraries.
Adds a headerIsJsonMimeType flag to CodegenParameter, set in DefaultCodegen.fromParameter for both JSON and vendor +json media types, mirroring the existing query-parameter mechanism.
- The
restsharp,httpclient, andunityWebRequesttemplates use a newClientUtils.ParameterToJsonStringhelper for JSON-content headers;generichostusesJsonSerializer.Serialize(value, _jsonSerializerOptions). Regular headers keep usingParameterToString. ParameterToJsonStringserializes directly withJsonConvert.SerializeObjectand escapes every character below U+0020 or at/above U+007F as\uXXXXto keep header values free of control characters.- Adds a 3.1 fixture and regression tests for the
restsharpandgenerichostbackends covering JSON, vendor+json, and plain headers.