Skip to content

[BUG][csharp] Header parameter with content: application/json is serialized via ParameterToString() instead of JSON #25082

Description

@cguldner

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?

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

  1. Generate the client from the spec above.
  2. Inspect the generated DefaultApi.cs. The header is added as:
    localVarRequestOptions.HeaderParameters.Add("X-Json-Arg", Org.OpenAPITools.Client.ClientUtils.ParameterToString(xJsonArg)); // header parameter
  3. 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).

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