Skip to content

[BUG][TypeScript] JSON-content header parameter is not serialized as JSON #25086

Description

@AndreyVMarkelov

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

TypeScript clients do not correctly serialize a header parameter declared with Parameter.content: application/json. The required representation is one header containing JSON, using the model’s OpenAPI wire property names:

X-Json-Arg: {"file_path":"/x"}

typescript-fetch: the generated header assignment uses the ordinary string path:

headerParameters['X-Json-Arg'] = String(requestParameters['xJsonArg']);

For an object this produces [object Object]. Even direct JSON.stringify of a generated model with filePath: "/x" would produce {"filePath":"/x"}; the generator’s existing model-to-wire conversion is needed to produce file_path.

typescript-node: the generated assignment passes the serialized model object as the header value without converting it to a JSON string:

localVarHeaderParams['X-Json-Arg'] = ObjectSerializer.serialize(xJsonArg, "HeaderArg");

The same problem applies to a present optional JSON-content header and to application/vnd.example+json. An ordinary schema-based string header should retain its current behavior.

typescript-axios was also checked. Its generated model uses the wire member name file_path, and its header path already uses JSON.stringify; this reproduction did not show the bug there.

openapi-generator version

7.26.0-SNAPSHOT, reproduced from master commit 271e4dd6e6d03ee456d8acef6542617371ef827c.

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: JSON header serialization
  version: 1.0.0
paths:
  /json:
    get:
      operationId: getJson
      parameters:
        - name: X-Json-Arg
          in: header
          required: true
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HeaderArg'
        - name: X-Optional-Json-Arg
          in: header
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HeaderArg'
        - name: X-Plain-Arg
          in: header
          required: true
          schema:
            type: string
      responses:
        '204':
          description: No content
  /vendor:
    get:
      operationId: getVendor
      parameters:
        - name: X-Vendor-Arg
          in: header
          required: true
          content:
            application/vnd.example+json:
              schema:
                $ref: '#/components/schemas/HeaderArg'
      responses:
        '204':
          description: No content
components:
  schemas:
    HeaderArg:
      type: object
      properties:
        file_path:
          type: string
Generation Details

Generate clients from the YAML above with the default options for typescript-fetch and typescript-node, for example:

openapi-generator-cli generate -i json-header.yaml -g typescript-fetch -o /tmp/json-header-fetch
openapi-generator-cli generate -i json-header.yaml -g typescript-node -o /tmp/json-header-node
Steps to reproduce
  1. Generate either client from the specification above.
  2. Call getJson with X-Json-Arg represented by a generated model value equivalent to { filePath: "/x" } and X-Plain-Arg set to "plain". Optionally provide the same model value for X-Optional-Json-Arg.
  3. Inspect the outgoing headers or the generated assignments shown above. Also call getVendor with the same model value.

Expected: exactly one X-Json-Arg header with the value {"file_path":"/x"}. The present optional and vendor JSON headers should have the same JSON value under their declared names. X-Plain-Arg should remain plain.

Actual: typescript-fetch stringifies the object as [object Object]; typescript-node assigns a serialized object as the header value rather than a JSON string.

Related issues/PRs

The same OpenAPI parameter serialization problem has been reported for other client generators:

Suggest a fix

Use the common CodegenParameter.headerIsJsonMimeType metadata populated by DefaultCodegen to identify JSON-content headers, including application/json and JSON structured-suffix media types such as application/vnd.example+json.

For affected TypeScript clients, serialize using their existing model-to-wire paths before forming the JSON header string: typescript-fetch should apply its model ToJSON conversion before JSON.stringify; typescript-node should apply ObjectSerializer.serialize before JSON.stringify. Preserve ordinary schema-based header handling.

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