Skip to content

[BUG] [typescript-nestjs-server] Header parameters generated without @Headers() decorator; values never bound #22989

@Prakash-86

Description

@Prakash-86

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)

Issue title: [BUG][typescript-nestjs-server] Header parameters generated without @Headers() decorator; values never bound at runtime


Description

The typescript-nestjs-server generator does not emit NestJS @Headers() decorators for OpenAPI parameters with in: header. The generated controller has plain method parameters (e.g. xRequestId: string, xIdempotencyKey: string) with no decorator. In NestJS, only parameters with a decorator (@Body(), @Param(), @Query(), @Headers(), etc.) are bound from the request. As a result, header parameter values are always undefined at runtime even when the client sends the headers.

The generator's documentation lists "Header" as a supported parameter feature (OAS2/OAS3), but the generated code does not fulfill this for NestJS.


openapi-generator version
  • Version used: 7.17.0 (via @openapitools/openapi-generator-cli 2.28.x)
  • Regression: Unknown; not tested on earlier versions.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Example API
  version: 1.0.0
paths:
  /items:
    post:
      operationId: createItem
      parameters:
        - name: x-request-id
          in: header
          required: false
          schema:
            type: string
        - name: x-idempotency-key
          in: header
          required: false
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
      responses:
        '201':
          description: Created

Generation Details

CLI command:

npx @openapitools/openapi-generator-cli generate \
  -i openapi.yaml \
  -g typescript-nestjs-server \
  -o ./out \
  --additional-properties=useSingleRequestParameter=true,importFileExtension=.js,supportsES6=true
  • Generator: typescript-nestjs-server
  • Options: useSingleRequestParameter=true (and commonly used .js/supportsES6 for Node)

Steps to reproduce
  1. Save the YAML above as openapi.yaml.
  2. Run the generation command above.
  3. Open out/controllers/Default.controller.ts (or the generated controller for the operation).
  4. Observe that the createItem method has parameters xRequestId: string and xIdempotencyKey: string with no @Headers('...') decorator.

What's the actual output vs expected output?

Actual (excerpt): Header parameters are plain arguments; Nest does not bind them.

createItem(
  @Body() body: object,
  xRequestId: string,
  xIdempotencyKey: string,
  @Req() request: Request
): SomeResponse | Promise<SomeResponse> | Observable<SomeResponse> {
  return this.api.createItem({ body, xRequestId, xIdempotencyKey }, request);
}

Expected (excerpt): Header parameters should have @Headers() so Nest binds values from the request.

createItem(
  @Body() body: object,
  @Headers('x-request-id') xRequestId: string,
  @Headers('x-idempotency-key') xIdempotencyKey: string,
  @Req() request: Request
): SomeResponse | Promise<SomeResponse> | Observable<SomeResponse> {
  return this.api.createItem({ body, xRequestId, xIdempotencyKey }, request);
}

The controller should also import Headers from @nestjs/common.


Related issues/PRs

Suggest a fix

The generator's NestJS controller template (likely under modules/openapi-generator/src/main/resources/typescript-nestjs-server/) should emit @Headers('{{paramName}}') (or the appropriate mustache variable for the header name) for parameters where param.isHeader or equivalent is true, and include Headers in the @nestjs/common import. This would align the generated code with Nest's parameter binding and the documented Header parameter support.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions