OpenAPITools / OpenAPITools/openapi-generator

[BUG][NODEJS-EXPRESS] collectRequestParams case sensitive comparison missing parameter on nodejs-express

Open
#6,200 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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 (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I am generating my node-express stub and requesting my endpoints which require a Host header to be preset.

My request looks like

GET http://localhost:8081/meta
accept: application/json
Host: malbork.localhost

MDN and RFC 7230, section 5.4: Host make me understand that the header name is capitalized.

The problem is that collectRequestParams method is using index access with param.name which holds Host value while request.headers contains host value.

  static collectRequestParams(request) {
    const requestParams = {};
...
    request.openapi.schema.parameters.forEach((param) => {
      if (param.in === 'path') {
        requestParams[param.name] = request.openapi.pathParams[param.name];
      } else if (param.in === 'query') {
        requestParams[param.name] = request.query[param.name];
      } else if (param.in === 'header') {
        requestParams[param.name] = request.headers[param.name];
      }
    });
    return requestParams;
  }
openapi-generator version

1.0.13-4.3.1

OpenAPI declaration file content or url
openapi: 3.0.3
...
paths:
  /profile:
    parameters:
      - $ref: '#/components/parameters/HostHeader'
    get:
      responses:
        '200':
          description: OK
...
components:
  parameters:
    HostHeader:
      in: header
      name: Host
      required: true
      schema:
        type: string
Command line used for generation
npx openapi-generator generate -i openapi.yaml -g nodejs-express-server -o ./fake-api
Steps to reproduce
  • Generate the api having an endpoint with the header requirement defined in the yaml file
  • Make a post with the Host header with a capital letter
  • The generated collectRequestParams({headers}) is receiving all headers with initial lower case letter (host instead of Host)
Suggest a fix

I don't know about how express works, so I guess it is not respecting the headers RFCs and converting everything to small case?
I tried just using

requestParams[param.name] = request.headers[param.name.toLowerCase()];

but another problem will emerge down the line. On my generated controller/services, they're expecting a host variable, but then a Host would be available.

So I changed to

requestParams[param.name.toLowerCase()] = request.headers[param.name.toLowerCase()];

and it seemed to work, but I don't know if other places would be compromised by it. My use case is very simple and I don't have much experience yet with OpenApi and the generator.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the generated nodejs-express collectRequestParams method and reproduce the issue with the provided OpenAPI YAML and npx openapi-generator command. Trace how the Host header flows into the generated controllers and services, then verify that the required header is collected consistently without breaking the parameter name expected downstream.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, node.js, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.