OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript-nestjs-server] Header parameters generated without @Headers() decorator; values never bound
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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-cli2.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
- Save the YAML above as
openapi.yaml. - Run the generation command above.
- Open
out/controllers/Default.controller.ts(or the generated controller for the operation). - Observe that the
createItemmethod has parametersxRequestId: stringandxIdempotencyKey: stringwith 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in modules/openapi-generator/src/main/resources/typescript-nestjs-server/ and inspect the NestJS controller template, then reproduce the issue with the provided openapi.yaml and generation command. Compare the generated out/controllers/Default.controller.ts with the expected output; done means header parameters are bound and the required NestJS import is present.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100